c

Should .NET developers *really* be spending time learning C for low-level exposure?

When Joel Spolsky and Jeff Atwood began the disagreement in their podcast over whether programmers should learn C, regardless of their industry and platform of delivery, it sparkled quite an explosive debate within the developer community that probably still rages amongst certain groups today. I have been reading a number of passages fro...

Best way to capture stdout from a system() command so it can be passed to another function

I'm trying to start an external application through system() - for example system("ls") - i would like to capture it's output as it happens so i can send it to another function for further processing. What's the best way to do that in C/C++ ? ...

Checking stack usage at compile time

Is there a way to know and output the stack size needed by a function at compile time in C ? Here is what I would like to know : Let's take some function : void foo(int a) { char c[5]; char * s; //do something return; } When compiling this function, I would like to know how much stack space it will consume whent it is...

Making something both a C identifier and a string?

Say you want to generate a matched list of identifiers and strings enum { NAME_ONE, NAME_TWO, NAME_THREE }; myFunction(NAME_ONE, "NAME_ONE"); myFunction(NAME_TWO, "NAME_TWO"); myFunction(NAME_THREE, "NAME_THREE"); ..without repeating yourself, and without auto-generating the code, using C/C++ macros Initial guess: You could add an ...

C99 stdint.h header and MS Visual Studio

To my amazement I just discovered that the C99 stdint.h is missing from MS Visual Studio 2003 upwards. I'm sure they have their reasons, but does anyone know where I can download a copy? Without this header I have no definitions for useful types such as uint32_t, etc. ...

How to check my byte flag?

I use a byte to store some flag like : 10101010 and I would like to know how to verify that a specific bit is at 1 or 0. ...

Is it possible to subclass a C struct in C++ and use pointers to the struct in C code?

Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct foo* f){ return f.k; } C++ code: class bar : public foo { int my_bar() { return ret_foo( (foo)this ); } }; There's an extern "C" around the C++ code and each code is inside its on compilation unit. Is this p...

Finding the name of a variable in C

I was asked a question in C last night and I did not know the answer since I have not used C much since college so I thought maybe I could find the answer here instead of just forgetting about it. If a person has a define such as: "#define count 1" Can that person find the variable name "count" using the 1 that is inside it? I did no...

Debugging with Events in Windows

If I create an event using CreateEvent in Windows, how can I check if that event is signaled or not using the debugger in Visual Studio? CreateEvent returns back a Handle, which doesn't give me access to much information. Before I call WaitForSingleObject(...), I want to check to see if the event is signaled before I step into the func...

Is there a way to have class variables with setter/getter like virtual variables?

I am embedding Ruby into my C project and want to load several files that define a class inherited from my own parent class. Each inherited class needs to set some variables on initialization and I don't want to have two different variables for Ruby and C. Is there a way to define a class variable that has an own custom setter/getter or...

How do I use scanf() with fopen

I'm writing a program and am having trouble using the scanf and fopen working together. From what I can tell my erroneous lines seems to be: FiLE * DataFile DataFile = fopen("StcWx.txt","r"); scanf(DataFile, "%i %i %i %.2f %i %i", &Year, &Month, &Day, &Precip, &High, &Low); The file it opens from has a list of weather data that look...

How can adding data to a segment in flash memory screw up a program's timing?

I have a real-time embedded app with the major cycle running at 10KHz. It runs on a TI TMS320C configured to boot from flash. I recently added an initialized array to a source file, and all of a sudden the timing is screwed up (in a way too complex to explain well - essentially a serial port write is no longer completing on time.) T...

Convert console exe to dll in C

I am interested in calling SoX, an open source console application, from another Windows GUI program (written in Delphi naturally). Instead of dealing with scraping and hiding the console window, I would like to just convert the application to a DLL that I can call from my application. Before I start down this path I am curious how m...

How can a C program produce a core dump of itself without terminating?

I want a C program to produce a core dump under certain circumstances. This is a program that runs in a production environment and isn't easily stopped and restarted to adjust other kinds of debugging code. Also, since it's in a production environment, I don't want to call abort(). The issues under investigation aren't easily replicated ...

unsigned int vs. size_t

I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL. I am curious as to the reason for this and the benefits it brings. ...

What makes a pthread defunct ?

i'm working with a multi-threaded program (using pthreads) that currently create a background thread (PTHREAD_DETACHED) and then invokes pthread_exit(0). My problem is that the process is then listed as "defunct" and curiously do not seems to "really exists" in /proc (which defeats my debugging strategies) I would like the following req...

Hidden features of C

I know there is a standard behind all C compiler implementations, so there should be no hidden features. Despite that, I am sure all C developers have hidden/secret tricks they use all the time. ...

Translate algorithmic C to Python

I would like to translate some C code to Python code or bytecode. The C code in question is what i'd call purely algorithmic: platform independent, no I/O, just algorithms and in-memory data structures. An example would be a regular expression library. Translation tool would process library source code and produce a functionally equival...

Is there a typical state machine implementation pattern?

We need to implement a simple state machine in C. Is a standard switch statement the best way to go? We have a current state (state) and a trigger for the transition. switch(state) { case STATE_1: state = DoState1(transition); break; case STATE_2: state = DoState2(transition); break; } ... DoState2(int transitio...

Is reading /dev/urandom thread-safe ?

This is the code: unsigned int number; FILE* urandom = fopen("/dev/urandom", "r"); if (urandom) { size_t bytes_read = fread(&number, 1, sizeof(number), urandom); DCHECK(bytes_read == sizeof(number)); fclose(urandom); } else { NOTREACHED(); } If not, how do I make it thread-safe? ...