c99

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

纵饮孤独 提交于 2019-11-28 04:55:05
问题 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

What are those strange array sizes [*] and [static] in C99?

泄露秘密 提交于 2019-11-28 04:42:22
Apparently the following function prototypes are valid in C99 and C11: void foo(int a[const *]); void bar(int a[static volatile 10]); What is the purpose of those strange subscript notations * , static , and CV qualifiers? Do they help distinguish statically typed arrays from variable-length arrays? Or are they just syntactic sugar? static in parameter array declarator void f(int a[static 10]); static here is an indication that parameter a is a pointer to int but that the array objet (where a is a pointer to its first element) has at least 10 elements. A compiler has then the right to assume f

Printf long long int in C with GCC?

自作多情 提交于 2019-11-28 04:38:36
How do I printf long long int and also unsigned long long int in C99 using GCC? I have searched the other posts which suggest to use %lld but it gives these warnings: warning#1: unknown conversion type character 'l' in format [-Wformat]| warning#2: too many arguments for format [-Wformat-extra-args]| For the following attempt: #include <stdio.h> int main() { long long int x = 0; unsigned long long int y = 0; printf("%lld\n", x); printf("%llu\n", y); } nos If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64d for a 64 bit integer. (and %I64u for an unsinged

Smart pointers/safe memory management for C?

柔情痞子 提交于 2019-11-28 04:19:49
I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point (no pun intended) to help with safe memory management? Thanks. The question is a bit old, but I figured I would take the time to link to my smart pointer library for GNU compilers (GCC, Clang, ICC, MinGW, ...). This implementation relies on the cleanup variable

Split C string into tokens using sscanf

流过昼夜 提交于 2019-11-28 04:06:36
问题 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

How to properly inline and use an inline function in C99, correcting link failure?

百般思念 提交于 2019-11-28 03:38:43
问题 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

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

耗尽温柔 提交于 2019-11-28 03:32:40
问题 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

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

让人想犯罪 __ 提交于 2019-11-28 03:04: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? 回答1: 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),

Which functions in the C standard library commonly encourage bad practice? [closed]

不打扰是莪最后的温柔 提交于 2019-11-28 02:50:57
This is inspired by this question and the comments on one particular answer in that I learnt that strncpy is not a very safe string handling function in C and that it pads zeros, until it reaches n , something I was unaware of. Specifically, to quote R.. strncpy does not null-terminate, and does null-pad the whole remainder of the destination buffer, which is a huge waste of time. You can work around the former by adding your own null padding, but not the latter. It was never intended for use as a "safe string handling" function, but for working with fixed-size fields in Unix directory tables

Is a compiler allowed to add functions to standard headers?

瘦欲@ 提交于 2019-11-28 01:55:56
Is a C compiler allowed to add functions to standard headers and still conform to the C standard? I read this somewhere, but I can't find any reference in the standard, except in annex J.5: The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers , or predefined macros with names that do not begin with an underscore. However, Annex J is informative and not normative... so it isn't helping. So I wonder if it is okay or