Jump to content
  • Advertisement
Sign in to follow this  
PureSnowX

[C++] Array resize dosn't work

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

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:



#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 = vector;

newVector[SIZE-1] = value;

delete[] vector;
vector = newVector;

for(int i = 0; i < SIZE; i++)
cout << vector << endl;
}

Share this post


Link to post
Share on other sites
Advertisement
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

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 *&).


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

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!