• Advertisement
Sign in to follow this  

string literals: how, when, where do (if) expire?

This topic is 3086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
/*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);
}
?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 szecs

/*global*/ char *strings[100];
...
strings = "blabla";
That'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.

(In before const char* vs char* const, etc [smile])

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
So literals will remain in memory even if I declared/defined them inside a function?

Yes.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zotoaster
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?

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.

Share this post


Link to post
Share on other sites
How does the program know if a pointer is pointing to the heap, or to static memory in the executable?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Is there really a difference between


const char* const foo = "bar";


and


static const char* const foo = "bar";


then?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement