Sign in to follow this  
AIRmichael

useing the same dynamic array in multiple functions

Recommended Posts

AIRmichael    136
Hey, I want to allocate an array and able to delete it and resize it at any time. I also want it to be available in all functions of a class. Now it crashes. This is my current program in short: Test.cpp
int array_size = 3;

int *array = new int[array_size]; // the array I want to use for all functions in this cpp

void Test::deleteandnewArray()
{
    delete [] array;

    int *array = new int[array_size]; // the array I again want to use for all functions
}

void Test::functionThatFillsArray
{
   // code filling up the array
}
void Test::functionThatUsesArray
{
   // code useing the array
}


The program crashes when I call functionThatFillsArray(); or functionThatUsesArray();. Is it because the new array is defined in another function and the original one is deleted which is outside? And is there a way to have a general dynamic array which can be allocated, and called in differant functions? Greetings, Michael

Share this post


Link to post
Share on other sites
Enigma    1410
Quick fix: remove the int * type declaration from the second line in Test::deleteandnewArray() so that it uses the existing (global?) array variable instead of declaring a new local variable which is immediately destroyed as the function leaves scope (thus leaking the memory you just allocated).

Issues: Likely to be inefficient. You ought to delete the last allocated chunk of memory before the program exits. statically initialised Test objects can cause problems if they are initialised before array. If global array can be modified outside of Test, where you might not want it to be (in particular it can be delete[]ed). Requirement to keep array_size correct for number of elements in array if used as an element count.

Consider: making array a (possibly static) member of Test of type std::vector< int > and (provided it is only used to track the required size of the array) eliminate array_size. A vector can be resized using its resize member. vector will not deallocate memory when resized which can make it more time efficient but less space efficient. A vectors memory can be explicitly deallocated by using std::vector< int >().swap(array); (This constructs a temporary vector with no elements and swaps it's contents with array. The temporary vector then leaves scope and destroys its memory).

Enigma

Share this post


Link to post
Share on other sites
_goat    804
Yes.

In deleteandnewArray() you've created ANOTHER pointer called array. Get rid of the int * before, to give:

void Test::deleteandnewArray()
{
delete [] array;

array = new int[array_size];
}


This will use the array "array" at global scope.

Share this post


Link to post
Share on other sites
AIRmichael    136
Oh my god, thanks alot it's working now!

I have been trying to fix this bug for like 12 hours in total. I just didn't see it anymore that they where ofcourse a new set of defined pointers...


Greetings, Michael L.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this