• Advertisement

Archived

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

Returning an array, isnt this a problem?

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

If I create an array in any one of the following ways... char *array = new char[16]; char array[16]; and my function returns a char*, wouldnt this be a problem since the array is only valid within the scope of the function...and after the function returns then the char* could be pointing a "junk" basically? the solution that i have now is the pass the array by reference. but if i return it as a char*, im just wondering what the implications would be.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by LordLethis
If you dynamically allocate memory using new, you have to free it with delete. So if you use char* array = new char[16] and return array, it''ll be a valid pointer until you delete it.

[My Lousy Page | Kings Of Chaos | Vampires | email.me]


But if you don''t use ''char *string = new char[16];'' and instead you use ''char String[16];''
then it would be a problem.



KaMiKaZe

Share this post


Link to post
Share on other sites

  • Advertisement