c

Finding locations in machine code (gcc/objdump -d)

If you have a particular line of C code in mind to examine in the machine output, how would you locate it in objdump output. Here is an example if (cond) foo; bar(); and I want to see if bar was inlined as I'd like. Or would you use some alternative tool instead of objdump? ...

Is there a need to close file descriptors before exit?

Of course, the immediate answer for most situations is "yes", and I am a firm believer that a process should correctly cleanup any resources it has allocated, but what I have in my situation is a long-running system daemon that opens a fixed number of file descriptors at the startup, and closes them all before exiting. This is an embedd...

Easy way to convert a struct tm (expressed in UTC) to time_t type

How do I do the above? There is mktime function but that treats the input as expressed in local time but how do i perform the conversion if my input tm variable happens to be in UTC. ...

About interrupts?

Hi all , I am coding a basic traffic light control which will be displayed on the monitor. In that i need an external interrupt (event ) to happen and change the state of the current traffic light. I am trying to do it without any board or external peripheral. Now I think i have the following choices . tell me which is the best. 1. One ...

What was the most dangerous programming mistake you have made in C?

I am an intermediate C programmer. If you have made any coding mistake that you came to know later that it was the most hazardous / harmful to the total application please share that code or description. I want to know this because in future I may come across such situations and I want to have your advice to avoid such mistakes. ...

SendMessage API in 64 bit

According to MSDN The return value specifies the result of the message processing; it depends on the message sent. I know it is defined as typedef LONG_PTR LRESULT; Meaning it will be 8 bytes on 64bit machine but (!) .... Does anyone know if it is safe to assume that only the lower 4 bytes are used and store it as an IN...

function returns and dynamic memory question

I have a dll that must be useable from C etc, so I cant use string objects etc as a normal would, but I'm not sure on how to do this safely.. const char *GetString() { std::stringstream ss; ss << "The random number is: " << rand(); return ss.str().c_str(); } could the c string be destroyed when ss falls off the stack? I'm ...

C And C++ Coding Standards

What are best practices with regards to C and C++ coding standards? Should developers be allowed to willy-nilly mix them together. Are there any complications when linking C and C++ object files. Should things like socket libraries that traditionally is written in C remain in C and kept in seperate source files? That is keeping c code...

How to make child process die after parent exits?

Suppose I have a process which spawns exactly one child process. Now when the parent process exits for whatever reason (normally or abnormally, by kill, ^C, assert failure or anything else) I want the child process to die. How to do that correctly? Some similar question on stackoverflow: (asked earlier) http://stackoverflow.com/ques...

Can I allocate a specific number of bits in C?

Hello, I am trying to store a large amount of boolean information that is determined at run-time. I was wondering what the best method might be. I have currently been trying to allocate the memory using: pStatus = malloc((<number of data points>/8) + 1); thinking that this will give me enough bits to work with. I could then refer...

How do I write a dispatcher, if my compiler's support for pointers-to-functions is broken?

I am working on an embedded application where the device is controlled through a command interface. I mocked the command dispatcher in VC and had it working to my satisfaction; but when I then moved the code over to the embedded environment, I found out that the compiler has a broken implementation of pointer-to-func's. Here's how I or...

sleep() is stalling my program too early. What am I doing wrong?

I want to write a small program that should print something like testing CPU... done testing RAM... done and so on. I wrote the following program in C: printf( "testing RAM...\t\t" ); sleep( sleep_time ); printf( "done\n\n" ); printf( "testing HDD...\t\t" ); sleep( sleep_time ); printf( "done\n\n" ); where sleep_time i...

C MySQL client library behaviour

I have a client application that connects to the MySQL database 4 server using stock libraries on SuSE SLES 9. However, at times when processing a particular reset set from the server, iterating throw the results does not allow me to process all the results that is in the database. This issue happens sometimes, mostly when servers hav...

How to access a Python global variable from C?

I have some global variables in a Python script. Some functions in that script call into C - is it possible to set one of those variables while in C and if so, how? I appreciate that this isn't a very nice design in the first place, but I need to make a small change to existing code, I don't want to embark on major refactoring of existi...

Calling two functions with the same name from two different C DLLs

I have two C DLLs that I need to access in the same executable. I have header files and .LIB files for both libraries. Unfortunately a subset of the functions that I need to access have the exact same names. The best solution I have been able to come up with so far is to use LoadLibrary to load one of the DLLs and explicitly call its met...

irritating select() behaviour in c

while (xxx) { timeout.tv_sec=TIMEOUT; timeout.tv_usec=0; FD_ZERO(&set); FD_SET(sd,&set); switch (select(FD_SETSIZE,&set,NULL,NULL,&timeout)) xxxxx } works fine, however FD_ZERO(&set); FD_SET(sd,&set); while (xxx) { timeout.tv_sec=TIMEOUT; timeout.tv_usec=0; switch (select(FD_SETSIZE,&set,NULL,N...

Stack Overflow Exploit in C

Hey there guys, the question is actually about stack overflows in C. I have an assigment that I can not get done for the life of me, i've looked at everything in the gdb and I just cant figure it. The question is the following: int i,n; void confused() { printf("who called me"); exit(0); } void shell_call(char *c) { prin...

Passing a value with struct type into a function in C

typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void push(element e, queue s) { nat lt = s->length; if (lt == max_length - 1) { printf("Error in push: Queue is full.\n"); return; } else { s->c...

How to synchronize C & C++ libraries with minimal performance penalty?

I have a C library with numerous math routines for dealing with vectors, matrices, quaternions and so on. It needs to remain in C because I often use it for embedded work and as a Lua extension. In addition, I have C++ class wrappers to allow for more convenient object management and operator overloading for math operations using the C A...

How to read a line from file?

I have to read a txt file with lines formated like this: 1: (G, 2), (F, 3) 2: (G, 2), (F, 3) 3: (F, 4), (G, 5) 4: (F, 4), (G, 5) 5: (F, 6), (c, w) 6: (p, f), (G, 7) 7: (G, 7), (G, 7) w: (c, w), (c, w) Each line will feed a struct with its data (the 5 numbers or letters in it). What's the best way to read the line and get the strings ...