473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

realloc in C++

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] = 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] = 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?

Jan 7 '07 #1
9 1677
di************@gmail.com wrote:
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.

--
Salu2
Jan 7 '07 #2


On Jan 8, 4:49 am, Julián Albo <JULIANA...@terra.eswrote:
divya_ratho...@gmail.com wrote:
What if i delete a 1D array and again use new to allocate a bigger (orYou 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.

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

thanks in advance!

Jan 8 '07 #3
>
--
Salu2pardon me.. but that means..?
Would it work or not?
and moreover, is it an acceptable practice?

thanks again in advance..
cheers!

Jan 8 '07 #4
"di************@gmail.com" <di**********@gmail.comwrote in message
news:11**********************@s80g2000cwa.googlegr oups.com...
--
Salu2pardon me.. but that means..?
Would it work or not?

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).
Jan 8 '07 #5
di************@gmail.com wrote:
>pardon me.. but that means..?
Would it work or not?
and moreover, is it an acceptable practice?
It works and is acceptable.

--
Salu2
Jan 8 '07 #6
"di************@gmail.com" <di**********@gmail.comwrote:
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] = 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] = i;

//delete data pointed to by newly allocated 'array'
delete[] array;
Jan 8 '07 #7


On Jan 8, 6:31 am, Julián Albo <JULIANA...@terra.eswrote:
divya_ratho...@gmail.com wrote:
pardon me.. but that means..?
Would it work or not?
and moreover, is it an acceptable practice?
>It works and is acceptable.

--
Salu2
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)

Jan 8 '07 #8

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
"di************@gmail.com" <di**********@gmail.comwrote:
>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.
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
Jan 8 '07 #9
"Sylvester Hesp" <s.****@oisyn.nlwrote:
"Daniel T." <da******@earthlink.netwrote:
"di************@gmail.com" <di**********@gmail.comwrote:
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.

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.
Jan 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.