Skip to main content
GameDev.net gamedev.net
🔒 Locked

Returning Arrays

Started by GroZZleR Jan 11, 2003 at 8:54 PM 4 replies 700+ views
Original Post
GroZZleR
GroZZleR
Hey all, I''m trying to return an array from a function (or the pointer to the array, same thing more or less). Anyways, the array is declared locally within the function and VC is bitching at me and I can''t help but listen when I get a compiler warning. The warning is saying that I''m returning a temporary or local address. I really need to return 2 values though, from the function, so I can manipulate stuff in another function. Is it okay to return a temporary or local array or is there a better way to return 2 values? Please no STL or anything. Thanks.
Martee
Martee

  
int* returnAnArrayOfInts()
{
int* foo = new int[2];

foo[0] = 1;
foo[1] = 9999;

return foo;
}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
quasar3d
quasar3d
you can make it a static array

static int array[5];

My Site
Julio
Julio
just a suggestion, but it might be more efficient to pass the array as a parameter of the function.

My Homepage
Freeservers.com sucks. Angelfire.lycos.com is cool.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Nypyren
Nypyren
if you have a variable that isn't dynamically allocated (no "new" or "malloc"), and is declared inside a funciton, returning it by value will make a copy (if it's an array, you're hosed) to return.

with an array, it would return the pointer to an array that isn't there anymore (exiting the function cleaned up all of the local variabled).

if you say "static" on your variable, they never get cleaned up.

if you say "new" or "malloc" like Martee said, you will be fine, but you would need to delete/free the memory manually later.


If you want to return the values using the parameter list, the parameters need to be a pointer to the type you want to return:


    
void foo(int *int_to_return, char **string_to_return)
{
*int_to_return = 5;
char *my_string = new char*[9];
strcpy(my_string, "testing!");
*string_to_return = my_string;
}

void main()
{
int a=0;
char *str=NULL;

foo(&a, &str);
// a is now 5, str is now pointing to that memory with the string.

}


I don't know for sure whether returning an array in the parameter list is faster/slower than returning it via return-value...

return-value can use EAX / parameter doesn't need to set up any additional variable memory (but you have to set it up in the calling function anyway)... If the compiler's smart, it's really fast either way.

[edited by - Nypyren on January 11, 2003 10:50:24 PM]
maknowles7
maknowles7
To get around the "returning 2 values" issue, if the array you send back is of variable length you can pass in an additional int reference to get the length back:


  
int *returnArray(Foo *doWorkOn, int &arrayLength)
{
// Do some work with Foo to fill the arrayLength

arrayLength = doWorkOn->getArrayLength();

// create and return array

int *retArray = new int[arrayLength];

// fill the array and return it

return retArray;

}//close returnArray


Another alternative to returning 2 values is to wrap the two values up in a struct or class.
Good luck.
maknowles7

==============================
Matt Knowles
Programmer/Game Developer
www.mak-tech.com
==============================
==============================Matt KnowlesProgrammer/Game Developerwww.mak-tech.com==============================

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.