c

returning reference of local variable

Hello, gcc 4.4.4 c89 In main I call a function to pass a line of text to a function. I want to perform some operation on it. However, that would mean that line is of no use. So in my get_string function I copy the contents and return the result. The only problem, is that the memory to that result would be lost and pointing to something...

How to take a moving mean of an array

Can some one please help me on this issue as I have spent time going around it without making any headway. I have data in an array of size say 3O. I want to take the first five elements of the array, find their mean value. Store the value in another array Then move to the second element of the array,from their find the mean value of t...

How to get a call stack backtrace? (deeply embedded, no library support)

I want my exception handlers and debug functions to be able to print call stack backtraces, basically just like the backtrace() library function in glibc. Unfortunately, my C library (Newlib) doesn't provide such a call. I've got something like this: #include <unwind.h printf("\t#%d: program counter at %08x\n", *depth, _Unwind_Get...

Howto check if a char* points to a string literal in C

I have a struct struct request { int code; char *message; }; that I'd like to free properly. I have the following function to do that: void free_request(struct request *req) { if (req->message != NULL) { free(req->message); } free(req); req = NULL; } The problem is that I get an "free(): invalid pointer"/segfaul...

error LNK2019 unexplainable when trying to port openldap to windows

Hi , I m working on windows XP with visual studio 2005. My project is a Cmake project created after creating an LDAP abstraction API on linux.I m trying somehow to make it work on windows. I've got an unusual linking error of type LNK2019 code : main.obj : error LNK2019: symbole externe non résolu _strcpy référencé dans la fonction _m...

Alligned memory allocation

I have the following assignment: Write a function in C that allocates block of memory and returns a pointer to the start of the memory under these conditions: All addresses in the block are divisible by 32 Allocated at least the number of the bytes required Every cell in the block is initialized to zero No global varia...

Data allocation to pointers in C

char *p = "abc"; char *q = "abc"; if (p == q) printf ("equal"); else printf ("not equal"); Output: equal Is it compiler specific, or is it defined somewhere in the standards to be as expected behaviour. ...

How to specify timezone in linux using C

Hi, I was trying to set the timezone of my system, and was trying to use settimeofday(), which takes a timezone struct as an argument, but just read that that struct is now obsolete (http://linux.about.com/library/cmd/blcmdl2_settimeofday.htm) How could I go about doing this? Thanks in advance. EDIT: Ugh, I feel really stupid. I crea...

Starting vg dev, which language?

Hey, I know someone who is looking into developing simple video games on his pc, and then eventually, hopefully port or develop some on the XBOX 360 indie center, using XNA studio. So, I have heard about C#? How easy it for a beginner? C++ is pretty good isn't it? But I've heard it's QUITE deep, broad, and sounds pretty easy to get l...

Help with DLL to Lib

I have converted a dll to lib. I gave it the lib and dll file and told it to remove the unnecessary stuff. I #included the .h file it created, and called GLU_DLLMAIN() in InitInstance just like I saw in the samples, but it still crahes on start up when it tries to initialize my static GLU object. What am I doing wrong? what is the proper...

Disassemble HTTP Response (C)

Hi, In continuation of this question I ask the following question. I've managed to read and understand what D.Shawley has recommended and I already finished a lot of work. But a problem occured to me again. I've managed to get the Content-Length of the page out of the HTTP response, but now I'm experiencing some problems with getting th...

Can you capitalize a pasted token in a macro?

In a C macro, is it possible to capitalize a pasted-in token? For example, I currently have the following macro: #define TEST(name, keyword) \ test_##name: TEST_##keyword##_KEYWORD I would invoke this as follows: TEST(test1, TEST1) which would yield the following: test_test1: TEST_TEST1_KEYWORD Now, instead of h...

How do I emulate a dynamically sized C structure in Python using ctypes

I'm writing some python code to interact with a C DLL that uses structures extensively. One of those structures contains nested structures. I know that this is not a problem for the ctypes module. The problem is that there is an often used structure that, in C, is defined via macro because it contains an "static" length array that can...

Execution time limit for a Lua script called from the C API

luaL_loadfile(mState, path.c_str()); lua_pcall(mState, 0, 0, 0); Is there a way to put an execution time limit (say 10-20 seconds) for those two C++ statements, that load and then execute a lua file? Since the Lua file is untrusted I don't want a malicious user to hang the program indefinitely with an infinite loop in the Lua code. T...

Drastic CPU drop with C program

My program is experiencing a nasty drop in performance. It is basically a pair of nested for loops which do an operation of a pair of data sets and then writes the result. The problem is, after about 500 of the 300,000 pairs it slows from taking .07 seconds/pair to 5 seconds/pair and CPU usage drops from nearly 100% to ~4%. All memory us...

How do I retrieve an error string from WSAGetLastError()?

I'm porting some sockets code from Linux to Windows. In Linux, I could use strerror() to convert an errno code into a human-readable string. MSDN documentation shows equivalent strings for each error code returned from WSAGetLastError(), but I don't see anything about how to retrieve those strings. Will strerror() work here too? How ...

Address 0 being overwritten by NULL pointer on HCS08

On my 8-bit Freescale HCS08 micro, whenever I call a library function that returns values to pointers I pass it, and I don't really want them, I threw in a NULL, e.g. UART_SendBlock((char *)txstr, strlen(txstr), NULL); The type of the last argument is uint16_t * and returns the number of characters actually sent, a value I don't care...

Permutation of char array In C.

I have been working on an algorithm to find all permutations of the elements of a char array for a few days now and well it just doesn't seem to work. The char array is an **array, which I iterate through based on the number entered by the user and I then malloc space for each word(40 chars each). The number entered by the user is the...

What is the use of %n format specifier in C?

What is the use of "%n" format specifier in C. Could anyone explain with an example. Thanks in advance. ...

python c extension for standard deviation

I'm writing a c extension to calculate he standard deviation. Performance is important because it will be performed over large data sets. I'm having a hard time figuring out how to get the value of pyobject once I get the item from a list. This is my first time writing a c extension for python and any help is appreciated. Apparently ...