c99

Return value of a boolean expression in C

余生颓废 提交于 2019-12-01 08:42:25
问题 For reasons that are not worth mentioning, I want to know if there's a standard defined value for boolean expressions. E.g. int foo () { return (bar > 5); } The context is that I'm concerned that our team defined TRUE as something different than 1, and I'm concerned that someone may do: if (foo() == TRUE) { /* do stuff */ } I know that the best option would be to simply do if (foo()) but you never know. Is there a defined standard value for boolean expressions or is it up to the compiler? If

Why do new C books not adhere to the C99 standard?

馋奶兔 提交于 2019-12-01 05:59:42
Nearly every (relatively) new book about c programming I've seen doesn't seem to adhere to the C99 standard, or they cover it in an extra chapter. Comming from a Java background, the C99 standard made the migration (well, still migrating ^^) much easier for me, and this probably applies to other languages, too. It seems like C99 hasn't reached most of the C developers yet. But why? Short answer: compiler support is slow to get installed and c programmers are a conservative lot who change their behavior slowly. I strongly suspect it's mainly because MSVC does not attempt to support C99, and

What is the use of ignoring `SIGCHLD` signal with `sigaction(2)`?

▼魔方 西西 提交于 2019-12-01 05:56:12
It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't wait() for it to _exit() ) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD is ignored by default anyway. How come does this work? int main (void) { struct sigaction sa; sa.sa_handler = SIG_IGN; //handle signal by ignoring sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) == -1) { perror(0); exit(1); } int pid = fork(); if (pid == 0) { //child process _exit(0); } do_something(); //parent process return 0; } Barmar The

Clarification on integer constant expressions

谁说胖子不能爱 提交于 2019-12-01 05:35:55
问题 Somewhere I've read that integer constant expressions consists integer constants such as: (5 + 5) //integer constant expression That was the only example I have seen. Now, from standard which says: (C99 6.6/6) An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast

When to use variable length array in C, but when a dynamic allocation?

ⅰ亾dé卋堺 提交于 2019-12-01 05:32:27
I find out about Variable Length Array in C99, but it looks like it behave almost the same as malloc + free. The practical differences I found: Too big array handling: unsigned size = 4000000000; int* ptr = malloc(size); // ptr is 0, program doesn't crash int array[size]; // segmentation fault, program crashes Memory leaks: only possible in dynamic array allocation: int* ptr = malloc(size); ... if(...) return; ... free(ptr); Life of object and possibility to return from function: dynamically allocated array lives until the memory is frees and can be returned from function which allocated the

What is the use of ignoring `SIGCHLD` signal with `sigaction(2)`?

走远了吗. 提交于 2019-12-01 03:56:52
问题 It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't wait() for it to _exit() ) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD is ignored by default anyway. How come does this work? int main (void) { struct sigaction sa; sa.sa_handler = SIG_IGN; //handle signal by ignoring sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) == -1) { perror(0); exit(1); } int pid = fork

Why do new C books not adhere to the C99 standard?

孤街醉人 提交于 2019-12-01 03:55:15
问题 Nearly every (relatively) new book about c programming I've seen doesn't seem to adhere to the C99 standard, or they cover it in an extra chapter. Comming from a Java background, the C99 standard made the migration (well, still migrating ^^) much easier for me, and this probably applies to other languages, too. It seems like C99 hasn't reached most of the C developers yet. But why? 回答1: Short answer: compiler support is slow to get installed and c programmers are a conservative lot who change

When to use variable length array in C, but when a dynamic allocation?

倖福魔咒の 提交于 2019-12-01 03:01:39
问题 I find out about Variable Length Array in C99, but it looks like it behave almost the same as malloc + free. The practical differences I found: Too big array handling: unsigned size = 4000000000; int* ptr = malloc(size); // ptr is 0, program doesn't crash int array[size]; // segmentation fault, program crashes Memory leaks: only possible in dynamic array allocation: int* ptr = malloc(size); ... if(...) return; ... free(ptr); Life of object and possibility to return from function: dynamically

Exception libraries for C (not C++)

时光毁灭记忆、已成空白 提交于 2019-12-01 02:36:12
问题 I am rolling my own exception library for C and would like good examples to examine. So far, I have been looking at David Hanson's: http://drhanson.net/work/ But I know I've seen other ones available in the past. Can you send me some additional pointers? Thanks, SetJmp 回答1: Here is one, compatible with C89 and implementing the try/catch/finally schema as can be found in other OO languages. 回答2: Symbian implemented exceptions (called 'leaves') in terms of longjmp. This was C++ code, but

variable length array declaration not allowed in OpenCL - why?

北城余情 提交于 2019-12-01 02:22:14
问题 I want to create a local array inside my OpenCL kernel, whose size depends on a parameter of the kernel. It seems that's not allowed - at least with AMD APP. Is your experience different? Perhaps it's just the APP? Or is is there some rationale here? Edit: I would now suggest variable length arrays should be allowed in CPU-side code too, and it was an unfortunate call by the C standard committee; but the question stands. 回答1: You can dynamically allocate the size of a local block. You need to