Jump to content
  • Advertisement

Archived

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

Palidine

Passing arrays between functions

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

hey, so for whatever reason i want to build a function that generates arrays based upon some input parameters. I''m having problems creating the array inside of a function and then passing it to the outside world. my function so far (just trying to get the basics working here): int * makeArray(int length) { int myArray[length]; for (unsigned int i = 0; i < length; i++) myArray = 5; return myArray; } now i keep getting a compiler warning here: "warning: address of local variable `binaryArray'' returned" i take this to mean that myArray was a local variable so upon exiting from the function it will be deleted from memory and the pointer to the head of the array will be meaningless. so how do i do what i want to do? thanks -me

Share this post


Link to post
Share on other sites
Advertisement
How about trying something along the lines of


int *createArray(int arySize)
{
int *retValue;

retValue = new int[arySize];

for (int i = 0; i < arySize; i++)
retValue = 5;

return retValue;
}


Edit: Oops, forgot to mention that you will want to call delete on the arrary when you are done with it.

Edited by - slepyii on December 27, 2001 5:04:58 PM

Share this post


Link to post
Share on other sites
you could pass a "results" array to the function, rather than trying to return it. then, the calling function will have to worry about allocation and deallocation of memory.
for example:

// da function
void makeArray(int Length, int* rArray)
{
for (int i = 0; i < Length; i++)
rArray = 5;
};
// using it
int* DaArray;
DaArray = new int[10];
makeArray(10, DaArray);
// do stuff with it
delete[] DaArray;

i dunno if that''s perfect code, i just typed it in this message. but, that''s the basic idea anyways.
HTH.


--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
I''m surprised you also didn''t get a warning about how static arrays must be initialized with a constant. What you''re trying to do is rather problematic.

In general, to work around the problem of not being able to return a pointer or reference to a local variable, you can make it static (like static int a;). Normally when a function is invoked the local variables it uses are created from scratch—and this happens each time. static makes the variable stay for the entire duration of the program, thus it will always have the same memory address.

But that won''t work here, since your array will be changing sizes.

So, the only solution (using normal arrays) is to dynamically- allocate it.
  
int* make_array(unsigned int length)
{
int array* = new int[length];

// although new should NEVER return NULL, as

// specified by the standard (instead, it should

// throw the exception bad_alloc), VC++ has it

// return NULL by default, so do test for it if

// using VC++ and you haven''t changed operator

// new''s behavior to conform to the standard

if(array != NULL)
for(unsigned int i = 0; i < length; ++i)
array[i] = 5;

return array;
}


Now, there''s one major problem with this approach. The function will allocate the memory for the array, but who will deallocate it? In other words, you''d have to remember to delete[] it when you were done, otherwise you''d get memory leaks.

This is one of the many reasons that returning an array is discouraged.

Instead of using dynamically-allocated arrays, consider using an STL container such as vector. Now, vector won''t fix your problem. But I think it will eliminate it altogether since you seem to be after a sort of dynamic array. Not only is vector just as fast and efficient as a "normal" array, but it also allows you to perform all sorts of operations on it, such as resizing and determining the size. Best of all, it automatically deallocates any allocated memory when it goes out of scope, so you''ll never get any memory leaks.

Share this post


Link to post
Share on other sites
ok so another important thing is that in the function i''m eventually writing i want the function to figure out how long the array is. it will be different depending on the input parameters. so the method of passing a results array as a parameter though otherwise good, won''t work for me.

i found a description of getting this to work in C at:
http://www.eskimo.com/~scs/cclass/int/sx5.html

anyway, thanks all. i think i''m more or less set from here.

-me

Share this post


Link to post
Share on other sites
You could use the vector class from the STL. It implements a dynamic array. However I would pass it as a parameter to the function to avoid an unnecessary copy operation.

Share this post


Link to post
Share on other sites
sweet Merlin9x9 solution implimented and working nicely.

thanks guys

-me

Edited by - Palidine on December 27, 2001 6:11:32 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.

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!