c

signed to unsigned conversion in C - is it always safe?

Suppose I have the following C code: unsigned int u = 1234; int i = -5678; unsigned int result = u + i; What implicit conversions are going on here, and is this code safe for all values of u and i? (safe, in the sense that even though result in this example will overflow to some huge positive number, I could cast it back to an int an...

Is there a difference between foo(void) and foo() in C++ or C

Consider these two function definitions void foo(){} void foo(void){} Is there any difference between these two? If not, why is the void argument there? Aesthetic reasons? ...

Checking network status and controlling PPP in a program

So I'm running PPP under linux with a cellular modem. The program I'm writing needs to know if the link is active before sending any data. What are my options to check if the link is available if it routes to a server I control (it doesn't go to the internet as I said earlier) Also, what is the best way to restart it - I'd like to ...

How do I automate finding unused #include directives?

Typically when writing new code you discover that you are missing a #include because the file doesn't compile. Simple enough, you add the required #include. But later you refactor the code somehow and now a couple of #include directives are no longer needed. How do I discover which ones are no longer needed? Of course I can manually re...

Is there a need to destroy char * = "string" or char * = new char[6]

I assume when I do char* = "string" its the same thing as char* = new char[6]. I believe these strings are created on the heap instead of the stack. So do I need to destroy them or free their memory when I'm done using them or do they get destroyed by themselves. ...

File Descriptor Assignment in C

When sockets are created or files are opened/created in C, is the file descriptor that's assigned to the socket/file guaranteed to be the lowest-valued descriptor available? What does the C spec say about file descriptor assignment in this regard, if anything? ...

Best Tips for documenting code using doxygen?

My team is starting to document our C code using doxygen, paying particular attention to our public API headers. There appears to be a lot of flexibility and different special commands in doxygen, which is great, but it's not clear what's a good thing and what's a bad thing without trial and error. What are your favourite ways to mark ...

Are there C library resources similar to C++'s Boost library?

I don't ever recall coming across anything for C that's as extensive as C++'s Boost library. ...

What does the ',' operator do in C?

What does the ',' operator do in C? ...

profile-guided optimization (C)

Anyone know this compiler feature? It seems GCC support that. How does it work? What is the potential gain? In which case it's good? Inner loops? (this question is specific, not about optimization in general, thanks) ...

Find the highest order bit in C

Hi, what I'm after is something I can feed a number into and it will return the highest order bit. I'm sure there's a simple way. Below is an example output (left is the input) 1 -> 1 2 -> 2 3 -> 2 4 -> 4 5 -> 4 6 -> 4 7 -> 4 8 -> 8 9 -> 8 ... 63 -> 32 ...

Memory leak detectors for C?

What memory leak detectors have people had a good experience with? Here is a summary of the answers so far: Valgrind - Instrumentation framework for building dynamic analysis tools. Electric Fence - A tool that works with GDB Splint - Annotation-Assisted Lightweight Static Checking Glow Code - This is a complete real-time performanc...

Which compiles to faster code: "n * 3" or "n+(n*2)"?

Which compiles to faster code: "ans = n * 3" or "ans = n+(n*2)"? Assuming that n is either an int or a long, and it is is running on a modern Win32 Intel box. Would this be different if there was some dereferencing involved, that is, which of these would be faster? long a; long *pn; long ans; ... *pn = some_number; ans = ...

How do you normally set up your compiler's optimization settings?

Do you normally set your compiler to optimize for maximum speed or smallest code size? or do you manually configure individual optimization settings? Why? I notice most of the time people tend to just leave compiler optimization settings to their default state, which with visual c++ means max speed. I've always felt that the default set...

Checking available stack size in C

I'm using MinGW with GCC 3.4.5 (mingw-special vista r3). My C application uses a lot of stack so I was wondering is there any way I can tell programatically how much stack is remaining so I can cleanly handle the situation if I find that I'm about to run out. If not what other ways would you work around the problem of potentially runni...

Have you successfully used a GPGPU?

I am interested to know whether anyone has written an application that takes advantage of a GPGPU by using, for example, nVidia CUDA. If so, what issues did you find and what performance gains did you achieve compared with a standard CPU? ...

When do function-level static variables get allocated/initialized?

I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time. int globalgarbage; unsigned int anumber = 42; But what about static ones defined within a function? void doSomething() { static bool globalish = true; // ... } When is the space for globalish allocated? I'm...

Reset screen point to the top of screen in Windows & Linux console

I have a little routine that's run under Linux and Windows written in C and displays output on the console. I'm not linking in any form of curses or anything like that. Currently I clear the screen using #ifdef __WIN32 system( "cls" ); #else system( "clear" ); #endif Then I have a bunch of printf statements to update the status. ...

Can't access variable in C++ DLL from a C app

I'm stuck on a fix to a legacy Visual C++ 6 app. In the C++ DLL source I have put extern "C" _declspec(dllexport) char* MyNewVariable = 0; which results in MyNewVariable showing up (nicely undecorated) in the export table (as shown by dumpbin /exports blah.dll). However, I can't figure out how to declare the variable so that I can acc...

How do you profile your code?

I hope not everyone is using Rational Purify. So what do you do when you want to measure time taken by a function peak memory usage code coverage At the moment, we do manually [using log statements with timestamps and another script to parse the log and output to excel. phew...) What would you recommend? pointing to tools or any ...