It's implementation specific, but the general answer is neither.
"This is a new pointer" is a text literal. The text literal gets stored with other program constants. The address that's stored into ptr by the compiler is the address of where the text constant is in the code space of your program.
No, even constants are stored in RAM. Loading from the HDD during run time is expensive which is why page outs are such a big deal for performance. The "heap" refers to a portion of RAM that is writable, where as constants are stored in an area of RAM that is generally not.
If you want a visual aid for what AbstractionAnon is talking about then load an application binary into a debugger and take a look at the memory map. Alternatively you could dump a processes memory to a file. There should be a label for each pages contents saying things like 'text', 'rdata' and 'reallocations'. Care to guess what the area that AbstractionAnon is referring to is labeled?
I don't get it, and when I restart the computer means the lines with string literal is deleted as the RAM is a volatile memory
and if you mean while loading the constants are moved from HDD to RAM then where on RAM are they loaded stack or heap??? your context seems to be like you're saying they are loaded on stack BUT then when after that I extend those how do they get extended.
My teacher told me that stack is once initialized and then it never grows
Your teacher is wrong.
The stack is extremely dynamic. Every time you make a function call in your program, several things are pushed onto the stack. Specifics are hardware dependent, but in general:
1) The arguments to the called function are pushed on the stack.
2) The program address in the calling function of where to return to when the called function exits. This is often called the Program Counter (PC).
3) The current value of the Stack Pointer (SP).
4) Stack pointer is adjusted to allocate space for local variables in the called function.
If the called function in turn calls another function, this process is repeated.
When a called function exits, PC and SP (#2 and #3) are popped from the stack and restored. Any return value from the called function is left on the stack and execution continues at the restored value of PC.
The stack can also be used for temporary storage of intermediate results during a complex calculation.
> but when we do something like this
> char * ptr = "This is a new pointer";
> where does memory come from stack OR heap ???
Really should have been constchar * ptr = "This is a new pointer";
The C++ standard has this to say about string literals:
An ordinary string literal has type "array of n const char" and static storage duration.
And this is what it has to say about static storage duration:
The storage for these objects shall last for the duration of the program.
Attempting to modify a string literal engenders undefined behaviour.
A conforming implementation is permitted to store a string literal anywhere it pleases as long as that storage will 'last for the duration of the program'.
lolx well my teacher is fine, He is too much more than I can describe here but either I must have misunderstood or he would have kept the details of the topic to be described latter
well I see now that stack is very dynamic got it thanks
also thanks for describing it in Assembly context, actually I'm pro-Assembly lolx , cause actually I've been programming micro-controllers
and do you mean to say that:
char * ptr = "something"; // is illegal or deprecated or not recommended
danicpp's teacher is in fact correct. It's just that the word "stack" can be used to refer to two different things: a data structure that stores scope-relevant information, and the memory region that holds this data structure.
Other way around. The stack data structure changes in size (i.e. the amount of data it contains changes), but the memory region that contains the data structure doesn't change in size*.
* Generally. It's possible to implement a "segmented stack" that gets around the problem of a fixed-size stack by allocating more stack segments as required. The original stack memory region remains the same size, but now the stack data structure exists spread out over several non-contiguous regions.
> do you mean to say that: char * ptr = "something"; is illegal or deprecated or not recommended
Yes.
> instead constchar * = "something"; is good
Yes.
As far as C++ is concerned, the points of interest about string literals are:
a. string literals are arrays of non-modifiable characters (null terminated).
b. the memory that they occupy is retained for the entire duration of the program.