Jump to content
  • Advertisement


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


Returning an array, isnt this a problem?

This topic is 5765 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
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.


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!