realloc in C++

D

divya_rathore_

Old question in a new bottle:

What if i delete a 1D array and again use new to allocate a bigger (or
smaller) size?
is this ok? Feel free to shoot me!

int *array;
int size = 100;

//allocate memory for 'size' no of integers.
array = new int[size];
//fill up the 'array'.
for (int i=0; i<100; i++) array = i;

//delete data pointed to by 'array'
delete[] array;

//again allocate but a bigger size!!
//is this ok??
array = new int[200];
//fill up the 'array'.
for (int i=0; i<200; i++) array = i;

//delete data pointed to by newly allocated 'array'
delete[] array;


Compiler specifics: Microsoft visual studio ver 6.0 and higher.
What I know: it can be done using STL, I KNOW. But how right/wrong is
the above approach?
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

What if i delete a 1D array and again use new to allocate a bigger (or

You never 'allocate again', you just allocate. The fact that you assign the
result of the allocation to the same pointer than a previous allocation is
completely irrelevant.
 
D

divya_rathore_

result of the allocation to the same pointer than a previous allocation is
completely irrelevant.

pardon me.. but that means..?
Would it work or not?

thanks in advance!
 
J

Jim Langston

and moreover, is it an acceptable practice?

thanks again in advance..
cheers!

Yes it would work. Yes, I've seen it done. Where you would do this depends
on your program.

I can see std::vector doing this exact thing (not sure if it does, but I
could see it implemented this way).
 
D

Daniel T.

Old question in a new bottle:

What if i delete a 1D array and again use new to allocate a bigger (or
smaller) size?
is this ok?

Yes, it is OK, though I suggest you use std::vector instead.
int *array;
int size = 100;

I believe the above needs to be a const int.
//allocate memory for 'size' no of integers.
array = new int[size];
//fill up the 'array'.
for (int i=0; i<100; i++) array = i;

//delete data pointed to by 'array'
delete[] array;

//again allocate but a bigger size!!
//is this ok??
array = new int[200];
//fill up the 'array'.
for (int i=0; i<200; i++) array = i;

//delete data pointed to by newly allocated 'array'
delete[] array;
 
D

divya_rathore_

It works and is acceptable.

Thanks a lot, Jim and Julián! I will do some tests and revert back
with my observations.
thanks again :)

- divya rathore
(remove all underscores for email ID)
 
S

Sylvester Hesp

Daniel T. said:
Yes, it is OK, though I suggest you use std::vector instead.


I believe the above needs to be a const int.

Why? For the 'array = new int[size];' statement? That's not true, of course
new array expressions can take nonconst integrals, otherwise they would be
pretty useless :)

- Sylvester
 
D

Daniel T.

Sylvester Hesp said:
Daniel T. said:
Yes, it is OK, though I suggest you use std::vector instead.


I believe the above needs to be a const int.

Why? For the 'array = new int[size];' statement? That's not true, of course
new array expressions can take nonconst integrals, otherwise they would be
pretty useless :)

True, my mistake. I was thinking about auto arrays.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top