Jump to content
  • Advertisement
Sign in to follow this  
OrenGL

Do I need to delte str in: char* str = "String"

This topic is 5082 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, When I write: char* str = "Just a string"; Do I need to delete 'str'? How is it really allocated; useing malloc or new?

Share this post


Link to post
Share on other sites
Advertisement
You don't have to delete it...and you're not using malloc or new. My guess is that it's making that string, and the pointer to it, on the stack...but i can't tell for sure. Haven't gone too deep on that...

Share this post


Link to post
Share on other sites
The pointer is on the stack, the string is part the program itself. You shouldn't delete it. You also shouldn't change it. In fact, you should use const char* str = "Just a string"; instead. For example, assuming your compiler hasn't marked it as read only (it probably has):


#include <stdio.h>

int main()
{
char *string = "This is a test.";
snprintf(string, strlen(string), "Oh no!");

printf("This string is: %s", "This is a test.");
return 0;
}




This program will either print "This is a test." (it's giving each string it's own chunk of memory), "Oh no!" (it's storing all identical strings in one place, and we overwrote it), or crash (you've altered a read-only chunk of memory).

[Edited by - smart_idiot on October 24, 2004 10:57:19 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

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