Hi,
Does anyone know a simple way of reallocating memory in c++ apart from using vectors?
Regards,
Am_Rich...!
8 8729
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
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.
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
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...!
HI....
this is a simple example to allocate and deallocate memory locations. - int main()
-
{
-
char* String1=new char[]="My String Size="; //allocate new 15 byte
-
int* Int1=new int(15); //allocate new 4 byte integer & assign 15 to it
-
char* String=new char[19]; //allocate new 19 byte
-
sprintf(String,"%s%i%c",String1,*Int1,0); //copy string1 & Int1 to String
-
delete Int1; //deallocate 4 bytes integer variable
-
cout << String << endl; //print result
-
delete String; //deallocate String 19 bytes
-
return 0;
-
}
Best Regards
Thank You Ahmedyo...
I'll try modify my program as such.
Kind Regards,
Am_Rich...!
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++.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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());
}
|
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...
|
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...
|
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.
|
by: sreenadh494 |
last post by:
how we are reallocation memory in c++, is there any functions or automatically reallocated
|
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...
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
| |