c

Unit testing C library, memory management.

I am working on a quite large C library that doesn't have any tests now. As the API starts to be final, I'd like to start writing unit tests. Nearly all my functions acts on the first parameter (a structure). The naive approach to unit test is to have the pre function call structure in a known state, call the function, and then compare...

What C99 features are considered harmful or unsupported.

I usually write C code in C89, now some features of C99 (like intxx_t or __VA_ARGS__ or snprintf) are very useful, and can be even vital. Before I more my requirements from C89 to C99 I wanted to know which of C99 features were widely supported and which ones were not widely supported or even considered harmful. I know we could just ch...

Question about passing a variable created in a function

Suppose there exists a function which returns a message say of the following format: struct message { void* data; }msgG; Which would be the best way to extract the data (i.e. Get the message accessible to fun1 in the code): 1- using a global variable 2- Using double pointers(pointer to a pointer) //Note: msgG is the global variable ...

Check for the right typecasting

Is there a safer way to typecast data from a generic pointer.? More specifically , is there a way to check if the type casting is safe or not. Suppose void*data we receive from the recv function in netwrking code. Suppose there are two structures: struct data1 { int val; double val1; } struct data2 { char str[100]; long double val3;...

How to find memory leaks in source code

If it is known that an application leaks memory (when executed), what are the various ways to locate such memory leak bugs in the source code of the application. I know of certain parsers/tools (which probably do static analysis of the code) which can be used here but are there any other ways/techniques to do that, specific to the langua...

Variable arguments in a Macro

I have a function which takes variable arguments, something like the following int log_data (LOG_TYPE eType, ...) { /** some logging related stuff here **/ } In the header file, I use something like #ifdef LOGGING_ENABLED int log_data (int nType, ...); #else #define log_data(_x_, ...) #endif Basically, the idea is to SWITCH deb...

#define statement explained

Hello, gcc 4.4.1 I am maintaining someone's code and I have come across something that I don't understand. #define RES_API(name, func) name##_##func Can anyone explain? Many thanks, ...

What naming convention for a C API.

We are working on a game engine that is written in C and we are currently using the following naming convention: ABClass object; ABClassMethod(object, args) AB Being our prefix. Our API, even if working on objects, has no inheritance, polymorphism or anything, we just have datatypes and methods working on them. Our Constants are nam...

Genericity vs type-safety? Using void* in C

Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get stuck into C, I'm aware that I have to make a compromise and I'd like it to be the right one...

dynamic memory created inside a function

I would like to know the technical reason(in terms of memory) why this piece of code will not work: #include <stdio.h> #include <stdlib.h> int* fun(int*); int main() { int a=5; int* ptr; // ptr=(int*)malloc(sizeof(int)); fun(ptr); a=*ptr; printf("\n the val of a is:%d",a); return 0; } void fun(int* ptr) { ptr = (int...

Is automatic bracket indenting in Gedit possible?

Hi, May anyone please help me if there are any auto indenting plugins/functions for the brackets in gedit(v2.22.3, Ubuntu 8.04) - specifically for C? I've tried the 'csmartindent' plugin however that doesn't seem to be working. On the href="http://live.gnome.org/Gedit/Plugins?action=AttachFile&amp;do=view&amp;target=csmartindent.tar.gz"t...

expected expression before return

Hi! all, the following c statement is not passing through compiler .error being "expected expression before return". int max( int a,int b) { a>b?return a:return b; } and yeah ,i know i can write this for finding max as return a>b?a: b; which is quite okay and will run...

How to tunnel TCP over reliable UDP?

Assume I have a reliable UDP library, and want to tunnel arbitrary TCP connections over it. This is my current approach to doing so, but I feel that it may not be very efficient. Any suggestions are very welcome. Client establishes a reliable UDP connection to server. Client runs a local SOCKS5 proxy, which receives data from any app t...

Source code for Xiaolin Wu's line algorithm in C?

Hi, I'm looking for a nice and efficient implementation of Xiaolin Wu's anti-aliased line drawing algorithm in C, does anyone have this code they could share with me? Thanks ...

The following code in python (and why)

I am learning python because it looks very nice and i know a bit of ruby and perl. The following is a C program to plot 3d points onto the screen. I used this as an example because it uses arrays, structures and function calls. I left out classes because i believe that sort of (OOP) thing is used simularly in all languages (correct me i...

Calling PHP From C

I am trying to write an application that will use PHP as a scripting language. The application is a CGI handler, and I want to be able to call PHP pages from it. I am looking for code that will let me initialize PHP inside the C program and then pass it either a buffer containing the php code, or a filename, for it to parse. I want to...

Finding out no bits set in a variable in faster manner

Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? Finding out the no. bits sets in a variable is easier. But how could we perform the same operation in fastest method ? ...

shifting right / what I am doing wrong?

Does not work as expected becuase it does not set the MSB bit correct. I am using metrowerks compiler. //shifting right 5 characters char * buffer; buffer=global_buffer; for(i=0;i<5;i++) //shift right for 1; { buffer[17-i]=(buffer[17-i]>>1)|(buffer[17-i-1]<<7); } EDIT input buffer (just before for loop) 0x00,0x00,0x00,0x00,0x00,0x...

Optimal way to perform a shift operation on an array

Suppose I have an array unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9}; Is there a way to perform shift operation on them besides just copying them all into another array. We can easily do it using linked lists but I was wondering if we can use a shift operator and get work done faster. Note: The data in this question is just an example....

What's the best way to get "numbered" config options from the C preprocessor?

We have a library that provides access to buttons on a device. These buttons are enumerated to the rest of the system as things like "power button" and so on. This way, the rest of the applications don't have to worry about how the "power button" is implemented. We build this library using a few configuration directives that look like...