Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Allocating memory from the heap

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Dec 01, 2008 10:03 am    Post subject: [C++] Allocating memory from the heap Reply with quote

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
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Mon Dec 01, 2008 11:42 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Mon Dec 01, 2008 12:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Dec 01, 2008 1:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Mon Dec 01, 2008 2:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Tue Dec 02, 2008 10:07 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
dkimxd
Master Cheater
Reputation: 0

Joined: 22 Nov 2006
Posts: 437
Location: 11

PostPosted: Tue Dec 02, 2008 2:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
sphere90
Grandmaster Cheater
Reputation: 0

Joined: 24 Jun 2006
Posts: 912

PostPosted: Tue Dec 02, 2008 6:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Tue Dec 02, 2008 9:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites