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?
Thank you. The absence of new was just a typo. The original code was
myString = newchar*[NumberOfStrings];
I'm new at this, and I'm not sure about the importance of declaring strings as constchar*. 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.
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.)