Jump to content
  • Advertisement


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


Return local array from a function safely

This topic is 5310 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 I am trying to return a local array from a function like this: char *getPathname() { char path[32]; // here I will store path "c:\file.txt" into path[] return path; } void main() { char *new_path = getPathname(); } but I am getting warnings that it is not safe to return a local address. Obviously becuase it will be destroyed when it is out of scope of the function. Fine. But what can I do in this case. I have to have same structure of char path[32]; just traditional array. I dont want any vectors etc.. I just need to know how to return safely this array so I can store the result in main() in another array. I dont care is if pass by copy or ref. its used once in the program and I am concerned with safety not speed. Can you please help? I guess I have somwhere my syntax wrong. I though the function prototype should be : char [] getPathname() rather than: char * getPathname but it didnt work at all. thanks [edited by - papa on May 30, 2004 5:27:33 AM]

Share this post

Link to post
Share on other sites
if your using c++, why not just use strings? strings are so much nicer then dirty ugly character arrays... plus you could safely return one with no problem...

Share this post

Link to post
Share on other sites
You should allocate the array dynamically (char *path = new char [32]), but then you''ll have to destroy it later somewhere in your main function. But I think, that strings are really nicer and there''s no reason not to use them.


- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share this post

Link to post
Share on other sites
I want to add to PipoDeClown #2 option:

Pass both the (already allocated array) to the function, as well as an max size value.


int getPathname(char* buffer, int size){
// here you can store whatever you want in the array, as long as it fits in. (check with the size)

// as a return value you can (optionally) return the actual number of bytes you've copied to buffer


// you would use the funtion something like this

// allocate 32 byte array

char new_path[32];

// pass it to the function as well as the max size

getPathname(new_path, 32);

// use you new_path here

edit: typo

[edited by - Direct4D on May 30, 2004 1:31:12 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!