Oh, sorry, typo. I meant array.
A dynamic array is stored on the heap; a static array is stored on the stack.
Originally Posted by sigfriedmcwild
WHOA!!! I just had to put my two cents in while reading this thread, then i saw all the gurus' already tore into you.
ouch. Hope you figured it out now, if not search the board, you'll find tons on schooling on the difference.
was the authour of your book named herbert schildt by the way?I said that an array is a pointer because that's what I've read in at least 2 books. I guess they just glossed over the important details.
Sorry about that
But if they're stored in the same place as globals and static variables, which are modifiable, then where does this stuff about read-only memory come from? It makes sense if it's stored in the same way as const variables, but I just don't see it happening with globals.A constant string such as this is stored in static memory (along with any static and global variables).
>>is stored in constant memory alongside static memory
Maybe I should read a book about this So the constant memory block is adjacent to the static memory block, and string literals are stored in the constant memory block, while globals/statics are stored in the static memory block.
Did I get it this time?
The only requrement for a string literal is that it exist at the time of instansiation. Thus char *s="This is a string?"; s must contain the address of consecutive characters containing the string. The normal way this is done, and this is just an implementation issue, is that the string is contained within the code segment. Beause the instructions must be loaded before execution can begin, and string literals are often requred very early in program execution it makes sense to load these strings with the instructions. On modern computers memory is devided into pages, and some of these pages have different attributes. Pages that contain instructions don't change, so the processor tags them as read only. This lets the processor have two cache mechanisms, a write back and complicated cache for data and a much simpler cache for instructions. As self-modifiying code is frowned upon making this cache read-only saves a lot of effort.