c

"register" keyword in C?

What does the register keyword do in C? I have read that it is used for optimizing but is not clearly defined in any standard. Is it still relevant and if so, when would you use it? ...

How would you shorten this so that action1 and action2 only show up once in code?

if (x() > 10) { if (y > 5) action1(p1, p2, p3, p4); else action2(p1, p2); } else { if (z > 2) action1(p1, p2, p3, p4); else action2(p1, p2); } I real project on mine, action1 and action2 are actually 2-3 lines of code and those functions that are invoked take 6-8 parameters in total, so w...

Binding a module's interface requirement either at run-time or compile/link time, which is better?

Hello, I have a question or best approach of connecting interface either during run-time or compile/link time. For my embedded projects the device drivers and I/O have interfaces that need to be bound (i.e. glued) to their corresponding interface dependencies. For example, for the analog device driver it requires an interface to the dis...

How do I cross-compile C code on Windows for a binary to also be run on Unix (Solaris/HPUX/Linux)?

I been looking into Cygwin/Mingw/lcc and I liked to be able to compile perl native C extensions on my windows(preferably under cygwin) and then run them on Solaris and HP unix without any further fuss, is this possible? This all stems from my original perl cross-platform question here. ...

Assigning strings to arrays of characters

I am a little surprised by the following. Example 1: char s[100] = "abcd"; // declare and initialize - WORKS Example 2: char s[100]; // declare s = "hello"; // initalize - DOESN'T WORK ('lvalue required' error) I'm wondering why the second approach doesn't work. It seems natural that it should (it works with other data types)? Cou...

Examples of code that compiles but executes differently in C versus C++

I am looking for more examples that compile under both C (any standard) and C++, but run differently. As an example, I found this in comp.lang.c, which produces 8 for C, and 512 for C++: #include <stdio.h> struct A { char block[8]; }; int main(void) { struct B { struct A { char block[512]; } a; }; ...

How do I perform a non-blocking fopen on a named pipe (mkfifo)?

If I have a program which creates and attempts to open a named pipe using mkfifo, how can I open a pipe for reading or writing without blocking? Specifically, I'm writing a C program which can be run with or without a gui (written in Java). In the C program, I successfully create the named pipes using mkfifo, however when I do FILE* i...

Adding an int member to a C struct causes a segfault

I'm still learning C, and have started using it to generate images. I can't figure out why one of my programs is segfaulting. Here's the source code, cut down to 40 lines: #include <stdio.h> #include <stdlib.h> struct color { unsigned char r, g, b; }; struct image { int w, h/*, o*/; struct color **data; }; int ma...

Removing dependancies when unit testing procedural code

In an object-oriented language with inheritence and virtual functions, removing dependancies (e.g. database, API calls, etc) from your unit testing code can be as simple as encapsulating those dependancies in their own methods and then overriding those methods in a test class inheriting from the class to be tested. However, I've run int...

Binary Trees in C

I'm trying to insert nodes into a tree in order. My function works fine... when there's only three nodes. I have this code: typedef struct _Tnode Tnode; struct _Tnode { char* data; Tnode* left; Tnode* right; }; Along with this: Tnode* add_tnode(Tnode* current_node, char* value) { Tnode* ret_value; if(current_node == N...

function name scoping in c

how does function name scoping work across multiple c files? i'm porting a standard gnu toolchain project to iPhone OS, and using XCode to do it. the code builds through make, but not through xcode. when building through xcode, the linker complains that the same symbol (function) is defined in two objects. the code has two distinct sou...

How to keep process from exiting when a pthread exits?

I am writing a server program in C wherein every time a client connects, I create a new pthread to handle the client's requests. When all of the threads exit, however, then my program exits, as though a call to exit() has been made. This is a problem - how can I overcome it? Lets say the server is running, and 2 clients connect. Once t...

what is a good c compiler

i'm using windows xp what would be a good c compiler? i want to learn c so that i can better understand the objective-c language for the iphone. i have a great background in php so hopefully something will make sense ps: what sections should i put more focus on when learning c in getting prepared for objective-c? thanks Duplicate: ...

Python C-API Object Initialisation

What is the correct way to initialise a python object into already existing memory (like the inplace new in c++) I tried this code however it causes an access violation with a debug build because the _ob_prev and _ob_next are not set.. //PyVarObject *mem; -previously allocated memory Py_INCREF(type); //couldnt get PyObject_HEAD_INIT o...

Why do I have to write *myPointerVar only SOMETIMES in Objective-C?

That's an issue I still don't understand. Sometimes I have to write: NSString* myVariable; myVariable = @"Hey!"; Then, for example I define a Structure "DemoStruct" and get an Variable that uses it. Lets say I have a Structure that has x and y vars from type double. I want to pass this var to a method which then manipulates my var, ...

ThreadPool Best Practices, Correctness

Hey everyone. I've been working on some code for a while that would spawn threads when needed, but decided a simpler and more effective solution would be to create a thread pool. It's implemented with a queue I made that has conditional waits on queuing and dequeuing. The only reason I'm posting this, is because I'm getting some weird er...

Is there a cheaper way to find the depth of the call stack than using backtrace()?

My logging code uses the return value of backtrace() to determine the current stack depth (for pretty printing purposes), but I can see from profiling that this is a pretty expensive call. I don't suppose there's a cheaper way of doing this? Note that I don't care about the frame addresses, just how many of them there are. edit: These...

When is "inline" ineffective? (in C)

Some people love using inline keyword in C, and put big functions in headers. When do you consider this to be ineffective? I consider it sometime even annoying, because it is unusual. My principle is that inline should be used for small functions accessed very frequently, or in order to have real type checking. Anyhow, my taste guide me...

memcmp sort

I have a single buffer, and several pointers into it. I want to sort the pointers based upon the bytes in the buffer they point at. qsort() and stl::sort() can be given custom comparision functions. For example, if the buffer was zero-terminated I could use strcmp: int my_strcmp(const void* a,const void* b) { const char* const one ...

Open and read Excel from a Linux based C program?

I am trying to locate a set of source code that would allow me to open and read the contents of an Excel file on Linux from within a C program. I dont really want to link it to OpenOffice SDK if I can find something that just does these two things. carl ...