This is Completely wrong isn't it?...

Started by
14 comments, last by tonymontana 19 years, 10 months ago
It doesn''t matter. When you have a string literal in your code, it still takes up memory. It''s in the string table in the exe. Anyway...

void getString(char** ret, int num){   char* t = 0;   if (num == 0) t = "A string.";   if (num == 1) t = "Foo"; //etc   /* ************************* */   *ret = new char[strlen(t)+1];   strcpy(*ret,t);}

Advertisement
I think both the original question as well as the answers are confusing. Most importantly I don''t understand why you want to do this, is there a good reason why you want to avoid string constants? I didn''t understand your concern about memory, I''ve never seen a program where the size of string constants were a problem, but I don''t know what platform you''re developing for of course. If can''t stand the idea of the strings taking up precious memory space I''m afraid you will have to store them externally (like resources). Your current solution only takes up more space.
Putting the strings together in one place can be helpful for i18n, but there''s no getting around the memory usage. They have to be *somewhere*.
If you''re using ASCII strings, each character in the array takes up 1 byte of memory.

You can''t get any smaller.

Theoretically, you *COULD* represent the english alphabet with just 5 bits per character (using only upper or lower case letters), but that''d be far more effort than it''s worth.





---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

So you want to be able to load one of a large number of strings at any time but not have all of the strings in memory all the time right?

Memory-mapped files sounds like the perfect solution to me.

btw if you do end up using a static initialised const array of string pointers then you may find that the compiler is smart enough to remove duplicates and perhaps even reuse short strings off the end of longer strings.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
in the OP''s original example, you could save 12 (really 20 because your arry is too small) bytes by removing the static char* thing, and just having

switch(num)
{
case 1 : return "Are You Sure?";
case 2 : return "Registration Error!";
// etc.
}

but like others said, you cannot remove the memory requirements for the string literals unless you read them from file or something.

This topic is closed to new replies.

Advertisement