| View previous topic :: View next topic |
| Author |
Message |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Mon Dec 01, 2008 10:03 am Post subject: [C++] Allocating memory from the heap |
|
|
Hi,
I'm currently using the 'new' operator in C++ to allocate memory, but I've seen a lot of different ways to do it:
1.
| Code: | Address = HeapAlloc(GetProcessHeap(), 0,Size);
HeapFree(GetProcessHeap(), 0, Address);
|
2. (What I do:)
| Code: | char * Buffer = new char[size]; //Or with any other type
Delete Buffer; |
3.
| Code: | Address = malloc(Size);
free(Address);
|
4.
And more like GlobalAlloc, LocalAlloc, but I'm not going to type all those.
Are there any differences in those methods and is there one which is the best? Is there one which is better for memory that's allocated for a short time and another one better if you allocate the memory for a long time or doesn't it make a difference?
Thanks in advance,
-Tombana
|
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Mon Dec 01, 2008 11:42 am Post subject: |
|
|
the 'new' keyword relies on the CLR, which in turn max exe co-dependent on the load of CLR dll's and it get all bitchy when you remove the CLR from linking :p
i tend to use HeapCreate HeapAlloc HeapFree HeapDestroy
as those are not CLR dependent.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Dec 01, 2008 12:52 pm Post subject: |
|
|
malloc() and free() are the C style of heap allocation. HeapAlloc and HeapFree are the Win32 apis for C style heap management - in Win32 calls to malloc and free map directly into the Heap* functions (well, almost - malloc uses the heap that the process starts with by default, or creates a new heap if there is none yet).
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Mon Dec 01, 2008 1:41 pm Post subject: |
|
|
So actually all those functions are some sort of wrappers, and will all result in a call to HeapAlloc on either the process' default heap or a created heap?
I guess I'll start using HeapAlloc then, as that also reduces the dependency on the CLR.
By the way, how do you completely 'disable' the CLR in C++. I use Microsoft Visual C++ 2008 Express Edition. What project options should I set in order to not link to any of those libraries?
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Dec 01, 2008 2:04 pm Post subject: |
|
|
malloc has the advantage of being POSIX standard, if you want to be vaguely portable.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Tue Dec 02, 2008 10:07 am Post subject: |
|
|
XX = version number
add a 'D' for debug versions of same libs
i usually restrict all default imports and name a few others directly to make sure they dont link also to disable it completly u need to remove the buffer security check as it is also CLR dependent define your own EntryPoint in the 'link->system' tab (i think)
msvcrtXX.lib
LIBCMT.lib
|
|
| Back to top |
|
 |
dkimxd Master Cheater
Reputation: 0
Joined: 22 Nov 2006 Posts: 437 Location: 11
|
Posted: Tue Dec 02, 2008 2:03 pm Post subject: |
|
|
when you free memory of a matrix, do you have to free each line seperately? Like in a 4 x 4 matrix. lets say A[4][4]. Can you just put free(A); and it will free all the memory in it or do you have to free each line one by one? like free(A[1]); free(A[2]); etc...
_________________
My siggy makes me look 1337 |
|
| Back to top |
|
 |
sphere90 Grandmaster Cheater
Reputation: 0
Joined: 24 Jun 2006 Posts: 912
|
Posted: Tue Dec 02, 2008 6:51 pm Post subject: |
|
|
| BanMe wrote: | the 'new' keyword relies on the CLR, which in turn max exe co-dependent on the load of CLR dll's and it get all bitchy when you remove the CLR from linking :p
i tend to use HeapCreate HeapAlloc HeapFree HeapDestroy
as those are not CLR dependent. |
Are you sure? 'gcnew' depends on CLR (Common Language Runtime) but 'new' in C++ on CRT (C Runtime Library). However, do note that 'new' in C# depends on CLR as it is equivalent to 'gcnew' in C++.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Tue Dec 02, 2008 9:25 pm Post subject: |
|
|
dkimxd: You need to call free once for every time you called malloc.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
|