Why should we check WIFEXITED after wait in order to kill child processes in Linux system call?

wait returning >= 0 tells you a child process has terminated (and that calling wait didn’t fail), but it does not tell you whether that process terminated successfully or not (or if it was signalled). But, here, looking at your code, it’s fairly obvious the program does care about whether the child process that terminated did so successfully or not: … Read more

C dynamically growing array

I can use pointers, but I am a bit afraid of using them. If you need a dynamic array, you can’t escape pointers. Why are you afraid though? They won’t bite (as long as you’re careful, that is). There’s no built-in dynamic array in C, you’ll just have to write one yourself. In C++, you … Read more

Use of cudamalloc(). Why the double pointer?

All CUDA API functions return an error code (or cudaSuccess if no error occured). All other parameters are passed by reference. However, in plain C you cannot have references, that’s why you have to pass an address of the variable that you want the return information to be stored. Since you are returning a pointer, … Read more

What is the C equivalent to the C++ cin statement?

cin is not a statement, it’s a variable that refers to the standard input stream. So the closest match in C is actually stdin. If you have a C++ statement like: a similar thing in C would be the use of any of a wide variety of input functions: See an earlier answer I gave to a question today … Read more

Meaning of Exit Code 11 in C?

You didn’t find a definitive answer because there isn’t one. It’s up to the author of the program to decide what exit codes they wish to use. Standard C only says that exit(0) or exit(EXIT_SUCCESS) indicate that the program is successful, and that exit(EXIT_FAILURE) indicates an error of some kind. (Returning a value from main is equivalent to calling exit with that value.) Most common … Read more

What is time(NULL) in C?

You can pass in a pointer to a time_t object that time will fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL, it just ignores it and merely returns a new time_t object that represents the current time.

Iterate through a C array

You can store the size somewhere, or you can have a struct with a special value set that you use as a sentinel, the same way that \0 indicates the end of a string.

Return a `struct` from a function in C

You can return a structure from a function (or use the = operator) without any problems. It’s a well-defined part of the language. The only problem with struct b = a is that you didn’t provide a complete type. struct MyObj b = a will work just fine. You can pass structures to functions as … Read more

Categories C Tags

Undefined Reference issues using Semaphores

I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX’s semaphore tutorial and changed things around to their syntax and as a result … Read more