472,971 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 8729
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,208 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,208 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

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
9
by: newbiecpp | last post by:
When you insert an element into a vector, if the internal memory is already used, how does the vector allocate additional memory? It just allocates one unit to satisfy the insert, or allocates a...
6
by: Lieven | last post by:
Consider: std::vector<int> vec(100); vector<int>::iterator it(vec.begin()); for(int i(0); i < 10000000; i++){ vec.push_back(rand()); }
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
2
by: Dmitri Shvetsov | last post by:
Hi All, Does somebody know how to work with this class directly? I know that we could redefine some internal predefined variables in C++ to force some methods to run much faster. For example. If...
1
by: onvenkat | last post by:
Can any one help me how we can rellocate a chunk of memory with out any temporary pointer. Also not using linklist.
1
by: sreenadh494 | last post by:
how we are reallocation memory in c++, is there any functions or automatically reallocated
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.