Dynamic allocation of char**

Given:
I have an array of dynamically allocated char* array.

 
char** myString;


And I allocate it with
 
myString = new char*[NumberOfStrings];


Then I initialize it with normal assignment
1
2
3
4
myString[0] = "String 1";
myString[1] = "String 2";
...
myString[NumberOfStrings - 1] = "String NumberOfStrings";


Then kill it with
1
2
3
4
int i;
for(i = 0; i < NumberOfStrings; i++)
{myString[i] = NULL;}
delete [] myString;


Strings are working properly, and when I call them for MessageBox() functions, they output the right string so no problem there. This is part of an Error handler class which output strings based on the Error code returned by other classes. The initialization and assignment is done by the class constructor and killed with the destructor.

Question:
Is this safe?
What is the maximum length for each string?
Are the strings themselves in the stack (probably)? Are the only things in the heap are the pointers for each string?

Using GCC 4.4.1 from MinGW 3.16

Thanks.

EDIT: Edited a typo as pointed out by cire.
Last edited on
And I allocate it with
myString = char*[NumberOfStrings];

Presumably there is a new missing.


Is this safe?

Yes, however the strings pointed to are const. This should be reflected in the myString definition.
const char** myString ;


What is the maximum length for each string?

Maximum length doesn't really make sense to be concerned about. You cannot modify the strings without invoking undefined behavior.


Are the strings themselves in the stack (probably)?

No. Nor are they on the heap. They may be stored in read only memory.


Are the only things in the heap are the pointers for each string?

Yes.


Thank you. The absence of new was just a typo. The original code was

myString = new char*[NumberOfStrings];

I'm new at this, and I'm not sure about the importance of declaring strings as const char*. Is this to avoid the undefined behavior by accidentally modifying them as you pointed out?

About the length, does it mean that I can have the strings as long as I want as long as the system running the application has enough memory for it? Then again, I have no intention of making each error string more than 127 chars long.
Last edited on
I'm new at this, and I'm not sure about the importance of declaring strings as const char*. Is this to avoid the undefined behavior by accidentally modifying them as you pointed out?

The assignment of a string literal address to a non-const char pointer was allowed (and is still allowed) in C, which is why it was allowed (for compatibility reasons) in C++ for some time. As of C++11, it is no longer allowed. So, your answer is yes, prior to C++11 and that isn't allowed after.


About the length, does it mean that I can have the strings as long as I want as long as the system running the application has enough memory for it? Then again, I have no intention of making each error string more than 127 chars long.

I believe the recommended (minimum) maximum length of a string literal in C++ is 65,535, although it is strictly implementation defined. You should be able to attain the maximum length for a string literal from your compiler's documentation. (For VS2012 the limit is 16384 bytes. The limit given in bytes applies to both narrow and wide character string literals. There is no limit, iirc, for GCC.)
Last edited on
Thank you for the explanation. Marking thread as solved.
Topic archived. No new replies allowed.