View Single Post
  #2 (permalink)  
Old 09-14-2008, 12:06 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 860
Last Blog:
Programs Under the Hoo...
Credits: 0
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Reallocate Memory

No. Try using realloc(). You need to include malloc.h for it. I'm not sure if it works with the new operator. In that case, you'll need to allocate memory like this (someone else correct me if I'm wrong, please):

Code:
#include <malloc.h>
#include <iostream>

using namespace std;
.
.
.
int *myArray;
int arrSize = 10;

//use calloc to allocate arrays, and malloc to allocate single blocks,
//like for classes and structs. I'd use the new operator for classes,
//though.

myArray = (int *)calloc(arrSize,sizeof(int));
if(myArray == NULL)
    cout << "Allocation failed." << endl;
.
.
.
//reallocate:
arrSize = 15;
int *temp = (int *)realloc(myArray,arrSize);
if(temp == NULL)
    cout << "Reallocation failed." << endl;
else
    myArray = temp;
.
.
.
free(myArray);
Reply With Quote