leak of pointer

T

toeni.ant

Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.

with regard,
ToeNi
 
S

Sharad Kala

Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];


Memory leak...
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.

Use std::vector. Read about it in your favourite textbook.

Sharad
 
R

Rolf Magnus

Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration.

Right. You created a new array of 20 A's and let your pointer point to that,
ignoring the fact that it pointed to another array already.
I don't want to lost the data. And also I want to enlarge the array size.

Arrays in C++ have a fixed size. Note that in your code above, the two
arrays returned by new are in no way related to each other. They are two
distinct objects.
I don't know how to solve this. Plz help me.

You have to copy the contents of the old array to the new array, then delete
the old one, and then overwrite the pointer. Of course you have to make
sure that _all_ pointers to the old array are updated to point to the new
one.
Alternatively, use std::vector, which does that all for you.
 
H

Howard

Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];

Do you mean...

int *A = new int[10];

? A is the array name, int is the type...right?
//...some codes here add data
A= new A[20];

Same here.
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.

You can't "resize" the array. But you can create a different one (with a
different name, say, B), and the size you want. Then, copy what you need
from A to B. Then delete [] A. After that, you can either work with B, or
copy the B pointer to A and work with A.

You should note, this is not the best way to handle resizeable arrays. The
std::vector class is much better at doing this kind of thing, and more
easily and safely. Plus, if you're creating an array of objects (instead of
just ints), then the work of copying from A to B described above is very
wasteful.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top