char * ptr = "My String" //where does memory come from

A short question.

I know for: char mystring[5]; memory comes from stack
For malloc(5 * sizeof(char)); memory comes from heap

but when we do something like this

char * ptr = "This is a new pointer"; where does memory come from stack OR heap ???

Well my logic says it should come from heap but !!!! it's kinda confusing
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.
1. okey my code is on HDD so will pointer point to a location on HDD???

2. how about I change the string latter like

cin >> myvar;

* ptr = myvar; // now will this move pointer from HDD to RAM and if yes then seems like it's gonna go on heap what do you think?
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?
Last edited on
> how about I change the string
A text literal is a const char * you shouldn't attempt to change it.

> * ptr = myvar; // now will this move pointer
you are dereferencing the pointer...
to Computergeek01

No, even constants are stored in RAM

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.

lke:

char * ptr;
cin >> * ptr;
ne555

yes I'm dereferencing the pointer to chage the contents of the memory to which it's pointing to
The memory layout is a bit more complicated than just stack and heap.
http://www.geeksforgeeks.org/memory-layout-of-c-program/
Last edited on
I tried to read that article but that is not very clearly written, I just got the basic idea.

My teacher told me that stack is once initialized and then it never grows but that article says that stack grows too (seems irrational to me).

lolx I thought it were just a short question.
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.
Last edited on
> 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 const char * 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

instead

const char * = "something"; // is good
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.
means the structure of stack doesn't change but size of stack changes right?
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 const char * = "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.

Anything else is an implementation detail.
Thankyou all of you I learned much but after some days/weeks practice I must revise this topic again and I'll learn more then
Topic archived. No new replies allowed.