Jump to content
  • Advertisement
Sign in to follow this  
Simplicity

Memory for literal strings

This topic is 4647 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

Consider: class TestClass { public: TestClass( char* Text ) : m_Text(Text) {} char* m_Text; }; TestClass* a = new TestClass( "This is the text." ); m_Text is now valid but where is the memory allocated?

Share this post


Link to post
Share on other sites
Advertisement
In the exe file itself. On Windows it'll be in the .data section of the file. Create a new project, instance that class in the code and compile then open the resulting exe with a hex editor and search for the text. It'll be there.

Share this post


Link to post
Share on other sites
As an aside:

If your worried about memory consumption from many string literals ---

If you find yourself using the same literal values over and over, check your compiler for what is usually refered to as "String Pooling" - sometimes the compiler names it "Read Only Strings"

If you use the string "This is a string" 50 times, some compilers will allocate each instance seperately.

char *s1 = "This is a string"
char *s2 = "This is a string"
etc...

If you never modify the data, then you can flip on string pooling, that way the string is only allocated once. It can save some space although it will usually be very little.



Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!