c99

Why doesn't the compiler detect and produce errors when attempting to modify char * string literals?

隐身守侯 提交于 2019-11-29 11:13:29
Assume the following two pieces of code: char *c = "hello world"; c[1] = 'y'; The one above doesn't work. char c[] = "hello world"; c[1] = 'y'; This one does. With regards to the first one, I understand that the string "hello world" might be stored in the read only memory section and hence can't be changed. The second one however creates a character array on the stack and hence can be modified. My question is this - why don't compilers detect the first type of error? Why isn't that part of the C standard? Is there some particular reason for this? C compilers are not required to detect the

Split C string into tokens using sscanf

匆匆过客 提交于 2019-11-29 10:50:18
I'm trying to split a string into tokens but somewhat recursively. I am trying to parse: "content=0&website=Google" so that I have a way to take out the parameters and values. If I try strtok I end up destroying the string I want to parse twice. So I tried char *contents = "content=0&website=Google" char arg[100]; char value[100]; sscanf(contents, "%s&%s", arg, value); as a first pass, hoping to parse arg again, but it fails, and arg contains the entire string. I tried using "%s\&%s" thinking & was a reserved word, but no luck there. Help! Edit: This was my strtok hack: static void readParams

Maximum size of size_t

别来无恙 提交于 2019-11-29 10:38:54
问题 I know in C return type of sizeof operator is size_t being unsigned integer type defined in <stdint.h> . Which means max size of it should be 65535 as stated in C99 standard 7.18.3: limit of size_t SIZE_MAX 65535 However in gcc-4.8.2 header file stdint.h has defined its size much greater than 65535 contradicting to which is stated in C99 standard as below shown, /* Limit of `size_t' type. */ # if __WORDSIZE == 64 # define SIZE_MAX (18446744073709551615UL) # else # define SIZE_MAX (4294967295U

How to properly inline and use an inline function in C99? (build fails)

泪湿孤枕 提交于 2019-11-29 10:32:33
Doing cc -std=c99 example.c on the following simplified example.c file: inline void a() { } int main() { a(); return 0; } gets me: In function `main': example.c:(.text+0x7): undefined reference to 'a' collect2: ld returned 1 exit status As I understand this has to do with the requirement of C99 standard to demand exactly one more definition for each inline non-static function that is used in cases where the body cannot be inlined? If that is so, I am guessing I could do with static inline instead, but I don't want this to bite me later, so what would be the best course of action here?

Undefined behavior: when attempting to access the result of function call

北城余情 提交于 2019-11-29 10:11:27
The following compiles and prints "string" as an output. #include <stdio.h> struct S { int x; char c[7]; }; struct S bar() { struct S s = {42, "string"}; return s; } int main() { printf("%s", bar().c); } Apparently this seems to invokes an undefined behavior according to C99 6.5.2.2/5 If an attempt is made to modify the result of a function call or to access it after the next sequence point, the behavior is undefined. I don't understand where it says about "next sequence point". What's going on here? You've run into a subtle corner of the language. An expression of array type is, in most

Variable-length arrays in C89?

牧云@^-^@ 提交于 2019-11-29 09:57:34
I've read that C89 does not support variable-length arrays, but the following experiment seems to disprove that: #include <stdio.h> int main() { int x; printf("Enter a number: "); scanf("%d", &x); int a[x]; a[0] = 1; // ... return 0; } When I compile as such (assuming filename is va_test.c ): gcc va_test.c -std=c89 -o va_test It works... What am I missing? :-) GCC always supported variable length arrays AFAIK. Setting -std to C89 doesn't turn off GCC extensions ... Edit: In fact if you check here: http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options Under -std= you will

What technical disadvantages do C99-style VLAs have? [closed]

試著忘記壹切 提交于 2019-11-29 09:41:39
I heard from many people that variable length array, introduced in C99, are terrible. Some guys on IRC said a minute ago « I don't think C++ will get VLA's, strousoup made some very negative comments about them ». What are the reasons why those people hate VLAs? VLAs allocate arrays on the stack, in runtime, making it harder, or even impossible to determine the stack size used at compile time. Since the stack has a rather small amount of memory available (in comparison with the heap), many worry that VLAs have a great potential for stack overflow. The upcoming version of the MISRA-C coding

C - Check currently available free RAM?

不问归期 提交于 2019-11-29 09:37:27
问题 I know how to use malloc() and free() to allocate memory, but is there also a standard C function to check how much memory is left, so I can call that periodically to make sure my code has no memory leaks? The only thing I can think of is calling malloc(1) in a endless loop until it returns an error, but shouldn't there be a more efficient way? 回答1: No, there's no standard C function to do that. There are some platform-specific functions you can use to perform certain types of queries (like

How to portably convert a string into an uncommon integer type?

◇◆丶佛笑我妖孽 提交于 2019-11-29 09:25:25
问题 Some background: If I wanted to use for, for instance, scanf() to convert a string into a standard integer type, like uint16_t , I’d use SCNu16 from <inttypes.h> , like this: #include <stdio.h> #include <inttypes.h> uint16_t x; char *xs = "17"; sscanf(xs, "%" SCNu16, &x); But a more uncommon integer type like pid_t does not have any such thing; only the normal integer types are supported by <inttypes.h> . To convert the other way, to portably printf() a pid_t , I can cast it to intmax_t and

Improving the performance of Matrix Multiplication

大城市里の小女人 提交于 2019-11-29 07:57:27
This is my code for speeding up matrix multiplication, but it is only 5% faster than the simple one. What can i do to boost it as much as possible? *The tables are being accessed for example as: C[sub2ind(i,j,n)] for the C[i, j] position. void matrixMultFast(float * const C, /* output matrix */ float const * const A, /* first matrix */ float const * const B, /* second matrix */ int const n, /* number of rows/cols */ int const ib, /* size of i block */ int const jb, /* size of j block */ int const kb) /* size of k block */ { int i=0, j=0, jj=0, k=0, kk=0; float sum; for(i=0;i<n;i++) for(j=0;j<n