Jump to content
  • Advertisement

Archived

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

tomalter80

C++ strings and memory management

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

We do the following: char *str = new char[10]; strcpy(str,"hi"); delete[] str; now is only the space corresponding to the first three characters (''h'',''i'',and ''\0'') is freed? Is the memory corrospending to next seven bytes leaked?

Share this post


Link to post
Share on other sites
Advertisement

int* x = new int[10000];
delete [] x;


is there no one freed? do all get freed? guess the answer.

replace int with char. should it behave differently now? guess the answer

does the function called inbetween happen to change the behaviour of delete[] in any form? guess the answer



and now the answers:

all get freed

it doesn''t mather if its an int*, or a char* to a dynamic array, all get freed

the function strcpy cannot change the behaviour of the delete[] function. the delete[] function always deletes all the memory of the pointer to the array




conclusion:

char* is not a string. its just another pointer. and it just behaves like a pointer.

for strings in c++, we all use std::string, to get an object (not a pointer), that actually behaves like a string.

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
If there is no dfference between a dynamically allocated char* array and int* array then why does "delete[] str" look for ''\0'' character . why do we get a run time error if there is no ''\0'' character in the string. Now dont think that it does not happen. i have tested it.

Share this post


Link to post
Share on other sites
No there should be no difference at all. And I''ve never seen a delete[] that scans the string for \0, perhaps the error lies elsewhere? Post some code where you get the error.

Share this post


Link to post
Share on other sites
Following returns error in VC++:
void test
{
char *str = new char[9];
strcpy(str,"everybody");
delete[] str;
}
void main
{
test();
}

But if i dont use strcpy and do assignment individually like

str[0] = ''e'';
str[1] = ''v'';
.
.
.
str[8] = ''y''

then there is no problem.

Share this post


Link to post
Share on other sites
THe reason you are getting a runtime error is that you are clobbering memory. "Everybody" is 9 characters itself,so you forgot space for the \0 that goes on the end. Try making the buffer 10 chars and see what happens

--Tyson

Share this post


Link to post
Share on other sites
well why does delete[] look for ''\0'' then. i have delebrately not given space for writing ''\0'' because i expect that delete[] should have nothing to deal with that.

Share this post


Link to post
Share on other sites
It doesn''t. Its the strcpy that tries to write 10 chars to a memory block that is only 9 chars big.

Share this post


Link to post
Share on other sites
delete doesnt look for the ''\0'' as stated before

also not giving enough space for a strcpy doesnt mean it will stop at the end of the array, it just goes on, overwriting anything that lies beyond!

even this could work (but not guaranteed as you might overwrite code just beeing executed)

char *x=new char[1];
strcpy(x,"that is much to much data for it");
printf(x);

(if you ever write code like this on purpose you should burn in hell except for testing purposes)


relying on functions knowing the array lenght works only in java or any other ''managed code'' where the vm checks the bounds of variables at runtime!

so why do you get the error:
as far as i know new allocates a bit more memory in debug mode at the beginning and end of allocated data and fills it with a predefined pattern. if you call delete it will check those patterns and if they are changed (in your example by overwriting it with a ''\0'' after the end of the array) it will complain (run time error)


mfg Phreak
--
"Input... need input!" - Johnny Five, Short Circuit.

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!