Sign in to follow this  
PureSnowX

[C++] Array resize dosn't work

Recommended Posts

I'v been trying to wrap my head around dynamic allocation in C++ by getting a Vector to-resize.
But my code keeps crashing and I can't figure out why:

[code]

#include <iostream>

using namespace std;

void insertNewValue(int *&vector, int value);

static int SIZE = 0;

int main()
{
/* Declare a new pointer to hold our array */
int* vector = NULL;
int value = 0;

do
{
cout << "Insert value into array" << endl;
cin >> value;
insertNewValue(vector, value);

}while(value != -1);

system("PAUSE");
}

void insertNewValue(int *&vector, int value)
{
SIZE++;
int* newVector = new int[SIZE];

for(int i = 0; i < SIZE-1; i++)
newVector[i] = vector[i];

newVector[SIZE-1] = value;

delete[] vector;
vector = newVector;

for(int i = 0; i < SIZE; i++)
cout << vector[i] << endl;
}
[/code]

Share this post


Link to post
Share on other sites
It looks like at least one of your problems is that you're assigning to the function argument inside the function, but that doesn't modify the variable that was passed to the function. One way to fix it is to use a reference to a pointer instead of just a pointer argument (int *&).

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1318878447' post='4873585']
It looks like at least one of your problems is that you're assigning to the function argument inside the function, but that doesn't modify the variable that was passed to the function. One way to fix it is to use a reference to a pointer instead of just a pointer argument (int *&).
[/quote]

Yes that fixed it!
Other then that does my memory de-allocation look good or is it flawed?

Share this post


Link to post
Share on other sites
You never allocated vector, so its null. In the first pass it doesn't explode because your for loop doesn't execute because of the size criteria, on the second pass BOOM, because you passed in and used an unallocated pointer.

EDIT: Damn you are quick SiCrane!

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1318878824' post='4873587']
Other then that does my memory de-allocation look good or is it flawed?
[/quote]
For a learning exercise involving int arrays about the only thing to say is that you never deallocate the array at the end of the program and you don't have any error handling code.

Moving outside those constraints, there's always the standard disclaimer about using std::vector rather than manually managing arrays, and if you were dealing with complex types that could throw exceptions when created or assigned, like std::string, then you'd have to change your logic slightly to avoid leaking memory on an exception.

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