/*global*/ char *strings[100];
...
void Some_func(......)
{
...
strings = "blabla";
...
}
void Some_other_func(......)
{
...
strings[k] = "blablabla";
...
}
void print()
{
for(int i = 0; i < 100; i++)
printfunc(strings);
}
?
string literals: how, when, where do (if) expire?
Is a string literal once defined/declared (what is the correct term), lasts all the time the program runs, or is overwritten 'randomly', or has its expiration some kinds of rules?
Can this be done to 'collect' strings:
From the C++ standard, 2.13.4/1:
Objects with static storage duration last for the entire length of the program's execution.
Typically they are implemented by storing the character data in a read-only part of the executable's image.
Quote:An ordinary string literal has type "array of n const char" and static storage duration
Objects with static storage duration last for the entire length of the program's execution.
Typically they are implemented by storing the character data in a read-only part of the executable's image.
Quote:Original post by mattd
Typically they are implemented by storing the character data in a read-only part of the executable's image.
Related to that:
Quote:Original post by szecsThat's not valid to do, and your compiler should give you a warning. String literals are const, and assigning them to a non-const pointer is A Bad Thing./*global*/ char *strings[100];... strings = "blabla";
(In before const char* vs char* const, etc [smile])
Yes, I forgot to copy the const in front of that.
So with
...
const char *strings[100];
....
is OK, or not elegant or dangerous etc?
So literals will remain in memory even if I declared/defined them inside a function?
So with
...
const char *strings[100];
....
is OK, or not elegant or dangerous etc?
So literals will remain in memory even if I declared/defined them inside a function?
Quote:Original post by szecs
So literals will remain in memory even if I declared/defined them inside a function?
Yes.
Quote:Original post by mattd
From the C++ standard, 2.13.4/1:Quote:An ordinary string literal has type "array of n const char" and static storage duration
Objects with static storage duration last for the entire length of the program's execution.
Typically they are implemented by storing the character data in a read-only part of the executable's image.
I see. I always wondered how it would execute something like: char* bleh = "hello";, because of course, there's no 'new' operator! You're saying it essentially does, char* bleh = new char[]{'h', 'e', 'l', 'l', 'o'}; and simply deletes the memory used up at the end?
Quote:Original post by ZotoasterQuote:Original post by mattd
From the C++ standard, 2.13.4/1:Quote:An ordinary string literal has type "array of n const char" and static storage duration
Objects with static storage duration last for the entire length of the program's execution.
Typically they are implemented by storing the character data in a read-only part of the executable's image.
I see. I always wondered how it would execute something like: char* bleh = "hello";, because of course, there's no 'new' operator! You're saying it essentially does, char* bleh = new char[]{'h', 'e', 'l', 'l', 'o'}; and simply deletes the memory used up at the end?
No, that would imply that the character array is dynamically allocated on the heap, which it isn't. It's stored in the image of the executable, as in you could load up the EXE file in a hex editor and find your string (in some form or another). It's just loaded directly with the program.
How does the program know if a pointer is pointing to the heap, or to static memory in the executable?
A program doesn't generally need to know if a pointer points to heap memory or static memory. A memory manager program might need to know that, but that would be due to programmer design. On 32 bit Windows each process has an address space 4 Gb in size. 4 Gb is the largest address that can fit into a 32 bit variable (2^32 == 4 Gb). The address space is virtual. A process isn't assigned 4 Gb of hardware memory. Most of that space is empty space. When a program is launched the operating system creates a process and the address space for that process and then maps the image of the program into that address space. The image of the program is the exe file. That's where the static variables are stored. The image is mapped rather than loaded, because the exe file isn't simply copied as is from the hard drive to memory. Sections of the exe file are loaded at specific intervals in the address space. This is a rough sketch of course.
Is there really a difference between
and
then?
const char* const foo = "bar";
and
static const char* const foo = "bar";
then?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement