Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nathan123

initializing a char* with a string literal

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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
what he said. the stringII is freed when your program is released from memory since it''s basically part of your program.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
"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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • 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!