initializing a char* with a string literal

Started by
16 comments, last by nathan123 20 years, 11 months ago
Hi All. I have a question regarding initializing a variable of type char* with a string literal. int main() { char* pString = new char[5]; char* pStringII = "TEST"; delete []pString; delete []pStringII; return 0; } The above code creates an error at runtime because of the line delete []pStringII. Based on this error, I have the following questions I hope someone could help me with. Is initializing a variable of type char* with a string literal the same as dynamically allocating memory with the new keyword? For example: Is char* pStringII="TEST" equivalent to char* pStringII = new char[5]? If these two statements are equivalent, how does one reclaim the memory (char* pStringII="TEST"), since delete []pStringII throws an error? Thanks for your help.
Advertisement
quote:Original post by nathan123
Is initializing a variable of type char* with a string literal the same as dynamically allocating memory with the new keyword?

No. As I recall, string literals are usually located in the data segment of your executable (I''m not at all sure this is mandated by the standard, but it appears to be how it is usually done). The general rule is, use delete[] if and only if you actually allocate the memory with the keyword new.
what he said. the stringII is freed when your program is released from memory since it''s basically part of your program.
Thanks for your reponse.
Does this mean that the memory used by the string is not reallocated until the program completes execution? Is there a way (or is it even necessary) to reclaim this memory?

For example, if throughout the sample program below, I make many calls to testMethod, am I constantly wasting memory (each time I pass in a string literal)?


#include <iostream.h>


void testMethod( char *word)
{
cout<
}


int main()
{


testMethod("HELLO");
//...OTHER CODE which may include other calls to testMethod()
return 0;
}
To make this VERY VERY clear ...

you MUST call operator delete IF AND ONLY IF you called operator new

you MUST call operator delete [ ] IF AND ONLY IF you called operator new [ ]

you MUST call free IF AND ONLY IF you called a C alloc function (malloc, calloc, realloc)
In fact you don''t wast memory.
If you call 10 times the function testMethod("HELLO"), it will create the HELLO string 10 time but once at a time, during the function call. The string will be automaticly released when the function will return.

Let me first say, thank you for all your reponses. I truly appreciate your help.
With that said, I do have one more question though.

"If you call 10 times the function testMethod("HELLO"), it will create the HELLO string 10 time but once at a time, during the function call. The string will be automaticly released when the function will return."--Viquel

If the string is automatically released when the function returns, does this mean that the data stored at *word is released?
In the program below, I copy the char pointer, *word, to a global variable char pointer, *gTest in testMethod()-- however, the information stored at gTest still exists after the function is returned. Why is this? Is my logic flawed(which it very well may be)?

I guess what I am asking is, why does the data at *word still exist after the method is returned?
Shouldn't gTest in testMethodII() point to nothing, since the data it points to was created and presumably released when testMethod() was returned?

I hope my question makes sense.

#include <iostream.h>

char *gTest;
void testMethod( char *word)
{
cout word "\n";//Q1. Do you have to delete word
gTest = word;
}

void testMethodII()
{

cout gTest "\n";
}

int main()
{


testMethod("HELLO");
testMethodII();

return 0;
}







[edited by - nathan123 on May 1, 2003 3:50:46 PM]
"If you call 10 times the function testMethod("HELLO"), it will create the HELLO string 10 time but once at a time, during the function call. The string will be automaticly released when the function will return."--Viquel

The string HELLO will only ever be created once at compile time and stuck in the data section of your executable.

(I don''t think this is mandated by any standard, but every compiler i know of will do it this way).
I would guess that this is because the "HELLO" string is stored in a data section that is loaded when the program starts, and is only released when the program exits. It''s not very safe to depend on this behavior, because there is not any rule that says the compiler has to do things this way. And it''s bad coding style.
Thanks!

This topic is closed to new replies.

Advertisement