Reputation: 685
I was going to answer someones question about printf accepting a char *, so i built a little test program and had my own question. compiled on codechef.com/ide with GCC 6.3
Pass the char* directly to printf I Think the questions is asking what happened to the "missing" const qualifier right? http://man7.org/linux/man-pages/man3/printf.3.html
char * str1 = "This is string 1\n";
char str2[] = "This is string 2\n";
int main(void) {
char str3[100] = "This is string 3\n";
char * str4 = "This is string 4\n";
str3[8] = 'c';
printf(str1);
printf(str2);
printf(str3);
printf(str4);
return 0;
}
Outputs:
This is string 1
This is a string 2
This is ctring 3
This is string 4
I was considering the memory layout of the program and i got even more confused. https://www.geeksforgeeks.org/memory-layout-of-c-program/
str1 is a pointer to the string literal "this is a string 1\n". str1 lives in data and points to the string literal that lives in ?? (i assumed also initialized data segment) How is read-only memory implemented in C?
str2 is like str1 as far as the memory layout goes.
str3 is where it gets interesting. str3 lives on the stack, 100 chars wide, and is assigned char[0] = 'T', char[1] = 'h', etc with the declaration/assignment. This should NOT BE CONST. str3's declaration and assignment are declaring 100 chars on teh stack and assigning them values from data (either rom data segment or initialized data segment or text?).
str4 is a pointer on the stack, pointing to data segment memory. This is like str1 and str2, not too interesting.
Then assigning str3[8] = 'c' just to test/verify/demonstrate that the stack memory for str3 is NOT read only before printfing.
I would expect 1,2,4 to work, but why did 3 work (not even a compiler warning???). I am probably incorrectly assuming that const is synonymous with "read only" because there is no way that str3 is "read only" if I just wrote to it.
Is there a read only memory in the stack for const variable declared in a function?
Can anyone explain to me why str3 didnt throw a warning or error? str3 is not a const char *, either the compiler optimized it out (changing the assignment to "This is ctring 3\n") or warnings were suppressed (also doesn't seem likely) or i have a fundamental misunderstanding of the cost keyword. How is stack memory const? Maybe one or more of my assumptions are wrong.
Upvotes: 0
Views: 216
Reputation: 241741
When a function prototype includes a pointer to a const
-qualified object, that means the function promises to not attempt to modify the pointed at contents. It does not mean that only a pointer to a const
object (whatever that might be) can be accepted.
On the other hand, if the prototype includes a pointer to an object which is not const
-qualified, that means it might attempt to modify the contents. In that case, passing a pointer to an immutable object would cause Undefined Behaviour. Most compilers will warn you if you try to pass a pointer to a const
-qualified object (if you enable warnings) since a const
-qualified pointer might point to an immutable object.
Generally speaking, you should regard const
to mean "I promise not to modify this" rather than "this cannot be modified".
Upvotes: 3