struct Example
{
char* string;
int x;
};
When I allocate a new instance of Example 8 bytes are allocated (assuming that sizeof(char*)=4). So when I call this:
Example* sp = new Example();
sp->string = "some text";
How is the string allocated? Is is placed in a random empty memory location? or is there some kind of relation between sp and member string?
So, “some text” makes a dynamic memory allocation?
String literals like that are put wherever the compiler wants to put them, they have a static storage duration (they last for the life of the entire program), and they never move around in memory.
The compiler usually stores them in the executable file itself in a read-only portion of memory, so when you do
something = "some text";it just makessomethingpoint to that location in memory.