468,457 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Reallocation of memory in c++

Hi,
Does anyone know a simple way of reallocating memory in c++ apart from using vectors?

Regards,
Am_Rich...!
Nov 20 '07 #1
8 7497
AHMEDYO
112 100+
HI...

you can use realloc(ptr,Size), if you bas NULL pointer it will work as Malloc and allocate new memory, if you pass valid Pointer it will reallocate it.

note : Realloc will return new pointer for new memory refrence

Best Regards
Nov 20 '07 #2
weaknessforcats
9,207 Expert Mod 8TB
you can use realloc(ptr,Size), if you bas NULL pointer it will work as Malloc and allocate new memory, if you pass valid Pointer it will reallocate it.
Do not use realloc(), calloc(), malloc, alloc() or any other C memory function in C++.

These functions do not call your constrcutors and the free() does not call your destructor.

These are C functions and in C you have to do all the memory work and variable initialization manually. This is a big source of error.

To reallocate in C++, allocate a new area the size of the old area plus additional elements. Note that all the constructors are called on the new allocation. Then you copy each element from the old area to the new one.
This wil call the assignment operator on your class.

Finally, you delete the old area and assign the address of the new areas to the pointer to the old area.

Do not do memcpy(), or any other mem...() calls in C++ because these functions will not call constructors or assignment operators or any other overloaded operators.
Nov 20 '07 #3
AHMEDYO
112 100+
Do not use realloc(), calloc(), malloc, alloc() or any other C memory function in C++.

These functions do not call your constrcutors and the free() does not call your destructor.

These are C functions and in C you have to do all the memory work and variable initialization manually. This is a big source of error.

To reallocate in C++, allocate a new area the size of the old area plus additional elements. Note that all the constructors are called on the new allocation. Then you copy each element from the old area to the new one.
This wil call the assignment operator on your class.

Finally, you delete the old area and assign the address of the new areas to the pointer to the old area.

Do not do memcpy(), or any other mem...() calls in C++ because these functions will not call constructors or assignment operators or any other overloaded operators.
Thanx For your reply..

yes i think you are fully right with this, but just these functions is very useful in same sutiation when normal allocation apart from classes, and classes thats have only data and no methods, and no problem when working with it, he didnt specify what type of allocation he need.

Best Regards
Nov 20 '07 #4
Hi Gentleman...
Thanks a lot for your concern... Indeed thats what ive been wondering. Can any of you provide me with a slight example of how reallocation can be made possible using the new operator?

Thanking you,
Am_Rich...!
Nov 21 '07 #5
AHMEDYO
112 100+
HI....

this is a simple example to allocate and deallocate memory locations.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  char* String1=new char[]="My String Size="; //allocate new 15 byte
  4.  int* Int1=new int(15);      //allocate new 4 byte integer & assign 15 to it
  5.  char* String=new char[19];     //allocate new 19 byte
  6.  sprintf(String,"%s%i%c",String1,*Int1,0);  //copy string1 & Int1 to String
  7.  delete Int1;         //deallocate 4 bytes integer variable
  8.  cout << String << endl;       //print result
  9.  delete String;         //deallocate String 19 bytes
  10.  return 0;  
  11. }

Best Regards
Nov 21 '07 #6
Thank You Ahmedyo...
I'll try modify my program as such.

Kind Regards,
Am_Rich...!
Nov 21 '07 #7
weaknessforcats
9,207 Expert Mod 8TB
but just these functions is very useful in same sutiation when normal allocation apart from classes, and classes thats have only data and no methods, and no problem when working with it,
These are C functions. You can't use these if a) you are using classes or structs, b) you are using placement allocation, c) you are managing a private heap, d) you are overloading operator new.

Please stop using these function in C++.
Nov 21 '07 #8
Do not use realloc(), calloc(), malloc, alloc() or any other C memory function in C++.

These functions do not call your constrcutors and the free() does not call your destructor.

These are C functions and in C you have to do all the memory work and variable initialization manually. This is a big source of error.

To reallocate in C++, allocate a new area the size of the old area plus additional elements. Note that all the constructors are called on the new allocation. Then you copy each element from the old area to the new one.
This wil call the assignment operator on your class.

Finally, you delete the old area and assign the address of the new areas to the pointer to the old area.

Do not do memcpy(), or any other mem...() calls in C++ because these functions will not call constructors or assignment operators or any other overloaded operators.
This is very nice, but you didnt wrote nothing helpfull, you are just saying to not use c functions in C++. Try to make some sugestions.

And BTW there is nothon wrong with using c memory functions in C++ for primitive tipes so ........ I dont know what that this is all about.
Mar 7 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by newbiecpp | last post: by
6 posts views Thread by Lieven | last post: by
22 posts views Thread by xixi | last post: by
2 posts views Thread by Dmitri Shvetsov | last post: by
1 post views Thread by onvenkat | last post: by
1 post views Thread by sreenadh494 | last post: by
1 post views Thread by Jean-Paul Calderone | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.