Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code is really simple.

C++
char *foo = "000"; // foo->"000\0"
*(foo+0) = '1'; // Crash | what i expecting is foo->"100\0"
*(foo+1) = (char)'2'; // Crash | what i expecting is foo->"120\0"
*(foo+2) = '3'; // Crash | what i expecting is foo->"123\0"


IMO

C++
char *foo = "000";

Create new char pointer and pointer to the string "000\0".

C++
*(foo+0) = '1';

or just
C++
*foo = '1';

dereference the pointer and assign new char to the address of the pointer pointing to.

What I have tried:

C++
char *foo = malloc(4); // codes work like a charm.
*(foo+0) = '1';
*(foo+1) = '2';
*(foo+2) = '3';
*(foo+3) = '\0';

This issue can be solve by using malloc(), but isn't the previous code dealing with "Stack", and this one dealing with "Heap"?
Posted
Updated 29-Dec-16 23:37pm
v2

Your foo pointer is allocated on the stack. But the storage it points to -- the string literal "000" -- is NOT. It probably resides in write-protected memory. That can possibly be changed by a compiler switch (see your compiler documentation). But I would say that it is a bad practice anyhow to modify the storage of a string literal. Use either stack or heap allocated storage (via malloc or new).

The following gives an example for allocated storage:

C++
char buffer[10];
buffer[0] = '1';
 
Share this answer
 
Comments
CPallini 30-Dec-16 4:45am    
5.
String literals are treated as const in the c (and c++) standard. STR30-C. Do not attempt to modify string literals - SEI CERT C Coding Standard - CERT Secure Coding Standards[^]

See "Modifying String Literals": C++ String Literals[^]

See also: c++ - Why are string literals const? - Stack Overflow[^]

Modifying a string literal results in undefined behaviour.
 
Share this answer
 
v3
Comments
CPallini 30-Dec-16 4:46am    
5.
Member 12928053 30-Dec-16 6:55am    
Thanks for you explaination, and decent answer of the "String Literals"
C++
char *foo = "000"; // foo->"000\0"
*(foo+0) = '1'; // Crash | what i expecting is foo->"100\0"
*(foo+1) = (char)'2'; // Crash | what i expecting is foo->"120\0"
*(foo+2) = '3'; // Crash | what i expecting is foo->"123\0"

In this code, the "000" is part of the program and is protected against changes. If it was allowed, you would end up with a self modifying program.

C++
char *foo = malloc(4); // codes work like a charm.
*(foo+0) = '1';
*(foo+1) = '2';
*(foo+2) = '3';
*(foo+3) = '\0';

Here , you allocate some memory in a space that allow changes, it is where your variables data is stored.

Quote:
This issue can be solve by using malloc(), but isn't the previous code dealing with "Stack", and this one dealing with "Heap"?

In both codes, the pointer is on stack, but in first, it is pointing to exe code and is not changeable, in second data is in heap.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900