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? 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
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!
>
--
Salu2pardon me.. but that means..?
Would it work or not?
and moreover, is it an acceptable practice?
thanks again in advance..
cheers!
"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). 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
"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;
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)
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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;
|
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...
|
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...
|
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...
|
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;
}...
|
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...
|
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>
|
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...
|
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,...
|
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$) {
}
...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |