Advertisement

new[] version of realloc?

Started by February 01, 2004 07:49 AM
21 comments, last by Tree Penguin 21 years, 1 month ago
Hi, is there a way to allocate more memory to an array allocated with new[] ? Thanks
No.

new[] a bigger array, copy everything over, and delete[] the
old one.

Better yet, use std::vector.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Advertisement
Thanks, can anyone tell me what''s exactly the problem with malloc() as i''ve been told several times i should use new[].

Thanks
malloc is C
new (or new[]) is C++

There is no really difference between them when. Both allocate data for a type. If you use self-made types (classes, structs; only in C++) the new operator calls the constructor of that type.
So if you use classes and structs stick to new (new[]) and delete (delete[]). DON''T MIX NEW/DELETE WITH MALLOC/FREE!!! If you alloc an object with malloc the state is undefined if you free the memory with delete. The same thing happens if you alloc memory with new and free the memory with free().

So I''d recommend you to use new/new[] and delete/delete[].

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
New also knows about the size of types, so you're less likely to make mistakes such as:
int intArraySize = 10;int* intArray = (int*)malloc(intArraySize); // oops, only allocated 10 bytes, not 40int* variable = (int*)malloc(1); // only 1 byte, not 4*variable = 0;for (int i = 0; i < 10; i++){	intArray[i] = i; // third iteration will probably clobber variable}assert(*variable == 0); // will probably fail 


Enigma

EDIT: stupid [i] interpreted as italics tag!!!

[edited by - Enigma on February 1, 2004 9:42:29 AM]
So, is the following way of adding an element to an array commonly used and the fastest way to do it?
quote:
Original Post By tangentz
new[] a bigger array, copy everything over, and delete[] the
old one.
Advertisement
Yes, but if you have big elements in that array try to use dynamic lists!

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Don''t know whether it''s the most commonly used, but the best and probably fastest way is to use vector and push_back.

Enigma
Has anyone got any tutorials on that ?

Thanks
Yes, you can also use the std::vector class. Here some info about that:

http://www.sgi.com/tech/stl/
http://www.roguewave.com/support/docs/sourcepro/stdlibug/index.html
http://www.josuttis.com/libbook/idx.html

--------------------------------------------------------

There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

There is another theory which states that this has already happened...
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...

This topic is closed to new replies.

Advertisement