c

Explicitly close file handles or let the OS close them in Unix C programming?

In Unix C programming, is it considered good practice to explicitly close file handles before the process exits, or is it instead good practice to let the OS close the file handles and thus avoid unnecessary code? Which of the two would generally be considered as the preferred alternative? Example: int main (int argc, char* argv[]) { ...

Needed: Wrappable Counter where < and > do "the right thing", language C

Hi Folks, I need the code to a couter that is allowed to overflow and where < > continue to tell earlier values from later values, for some defined interval. To clarify, one possible implementation would be: Consider two such counters cur and dut (device under test), consider two functions: bool isEarlier(cur, dut) // Is dut earl...

How do I convert a Win32 lParam to a C struct?

Say I have some windows method and a struct: struct SomeStruct{ int foo; int bar; int baz; int bat; } SomeMethod(int a,int wParam, int lParam) { SomeStruct s; // get lParam into SomeStruct } How to I get the lParam into a SomeStruct variable? I'm thinking that I need something like this (but feel free to point out my ignorance...

select on UDP socket doesn't end when socket is closed - what am I doing wrong?

I'm working on Linux system (Ubuntu 7.04 server with a 2.6.20 kernel). I've got a program that has a thread (thread1) waiting on a select for a UDP socket to become readable. I'm using the select (with my socket as the single readfd and the single exceptfd) instead of just calling recvfrom because I want a timeout. From another threa...

Is it overkill to run the unit test with Valgrind?

Hi Just some days ago I started looking into a unit test framework called check, and I intend to run the test on c code under Linux. Now check and some well designed code and some test code can help me to verify that the basic functionality is correct, I mean it is quite easy to just look at the variables in and response back and ...

What is xdoclet? (from a C-programmer point of view)

Question from a C-guy who has to work with some java code that is connected to my C-code via JNI. I have to work on the build-system, and I'm trying to change that from a shell-script to a proper makefile. For the C-part that's easy, but the java side somehow involves xdoclet stuff. I haven't yet found out what xdoclet is all about, a...

clear buffer cache on Mac OS X

Is there a way to programatically clear the buffer cache on the Mac, preferrably in C? Basically, I'm looking for the source of 10.5's purge command. EDIT: I now see this is part of the CHUD tools, for which it seems the source isn't directly available. However, I'm still looking for some code to do the same. ...

Best timing method in C?

What is the best way to time a code section with high resolution and portability? /* Time from here */ ProcessIntenseFunction(); /* to here. */ printf("Time taken %d seconds %d milliseconds", sec, msec); Is there a standard library that would have a cross-platform solution? ...

Is NULL always false?

Is it safe to assume that NULL always translates to false in C? void *somePtr = NULL; if (!somePtr) { /* This will always be executed? */ } Or should an explicit check against the value of NULL be made? ...

Copying lines from stdin to an array of character pointers

I would like to copy lines from stdin to an array of character pointers. For example if the user entered the following "the\nquick\nbrown\nfox" then I would like to have an array that looks like arr[0] = "the" arr[1] = "quick" arr[2] = "brown" arr[3] = "fox" Any pointers? ...

From C Source to Java Bytecode?

I'm looking for a way to compile C source code into high-performance Java bytecode. I've successfully used NestedVM, but the performance hit is not acceptable for a project I'm working on. I've also seen various open source projects aimed at this problem and a couple of commercial products. This SO question deals with general problem o...

Recommendations: Easy To Use/Low Dependency C/C++ RSS Library

Just like the title says. Anyone know of an RSS reading library for C/C++? Ideally: Minimal/no outside non-standard dependencies Easy to use API Cross platform would be ideal, but I'm OK with Win* only if necessary. I'd just rather not roll my own if necessary. :) ...

How to create a customized file descriptor on linux

I would like to create a file whose descriptor would have some customizable behavior. In particular, I'd like to create a file descriptor, which, when written to, would prefix every line, with name of the process and pid (and maybe time), but I can imagine it can be useful to do other things. I don't want to alter the writing program - ...

return statement vs exit() in main()

Should I use exit() or just return statements in main()? Personally I favor the 'return' statements 'cause I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to refactor the main() function, having 'return' seems like a better choice than exit(). Does ...

Dynamic Link Error

I am observing some behavior for which I am finding it tough to reason. I have a piece of code as follows: int timer_temp_var; if ((timer_temp_var/1000.0) > 5.0) { //Do something } This piece leads to link error. > > dld: warning: Undefined symbol _d_fle" > dld: no output written make[1]: *** > [app.elf] Error 1 But on repla...

Detecting that log file has been deleted or truncated on POSIX systems?

Suppose a long-running process writes to a log file. Suppose that log file is kept open indefinitely. Suppose that a careless system administrator deletes that log file. Can the program detect that this has happened? Is it safe to assume that fstat() will report a link count of zero for a deleted file? Truncation, it seems to me, is...

Format specifier for 'long long'

I declare a variable for a 64 bit counter as : long long call_count; What is the format specifier that I should use in print statements? I tried, %l, %ld, %ll. None seems to be correct. I use Diab C compiler for compiling my application code to run on pSOS operating system. ...

Improving/Fixing a Regex for C style block comments

I'm writing (in C#) a simple parser to process a scripting language that looks a lot like classic C. On one script file I have, the regular expression that I'm using to recognize /* block comments */ is going into some kind of infinite loop, taking 100% CPU for ages. The Regex I'm using is this: /\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*...

Are there any mature Binary Decision Diagram tools available?

Are there any Binary Decision Diagram (BDD) libraries for C, or other languages that are tested and have good documentation? ...

Putting a CGImageRef on the clipboard

I'm trying to copy a CGImageRef to the clipboard pasteboard. I found a function that claims it should do this by creating a destination from (zero sized), adding the image to the destination, finalizing, then PasteboardPutItemFlavor the ref into the clipboard. However it doesn't work, so two questions: Is this the correct way to go ab...