Reallocation of Memory in a string

Hi All,
Below is a piece code which im using to store some values from the array tempflow to flow.
I have an array of bytes whose size is unknown and to which i will have to reallocate each time.
However, when im compiling the program, my array flow is storing only one element.
Can anyone help me by suggesting something and pinpointing to me as to where the mistake might be?
Thanks,


int maxi=4;
unsigned char* flow = new unsigned char[maxi];
unsigned char* tempflow;

int _tmain(int argc, _TCHAR* argv[])
{
int count =0;
int value =0;

while(aa<flow_length)
{
count = tempflow[aa]; //Number of times of occurence
value = tempflow[aa+1]; //Value to be repeated

for(int j=0;j<count;j++)
{
flow[n]=value;
n=n+1;
if (n >= maxi)
{
maxi = maxi * 2; // double the previous size
unsigned char* temp = new unsigned char[maxi]; for (int i=0; i<n; i++)
{
temp[i] = flow[i]; // copy values to new array.
}
delete [] flow; // free old array memory.
flow = temp; // now a points to new array.
}
}
aa = aa+2;
}

}
Can you please post your code int he [c0de] [/c0de] tags.

Also, Where is N being defined and assigned?
There is no standard way to realloc() data managed with new and delete.

You would be just as well off using a vector, which will handle all that memory for you:
1
2
3
4
5
6
7
8
#include <vector>
...

vector<unsigned char> flow( maxi );

for each c in tempflow[i]:
  if (i < flow.size()) flow[i] = c;
  else flow.push_back( c );


Hope this helps.
Topic archived. No new replies allowed.