The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on June 16, 2008, 08:38:49 PM

Title: Dynamic Memory Allocation
Post by: n00b! on June 16, 2008, 08:38:49 PM
Hi,
I want to make a String within a Proc with a length of strlen(xyz).

So my question: Is there something like (char *)var malloc(strlen(xyz) + 1) in C?
Title: Re: Dynamic Memory Allocation
Post by: jj2007 on June 16, 2008, 10:58:07 PM
Yes, there is. Check \masm32\help\hlhelp.hlp, String macros, Allocating String Buffers
Title: Re: Dynamic Memory Allocation
Post by: hutch-- on June 17, 2008, 01:51:42 AM
noob,

In the simple sense of using provided macros, try something like this.


; byte count required = 4096   ; pick your size as needed

mov hMem, alloc(4096)

; do something with the memory

free hMem


It is worth understanding what is behind macros of this type, there are multiple strategies in memory allocation for different asks.

VirtualAlloc(), HeapAlloc(), GlobalAlloc(), OLE string memory and a few others. Each comes with a matching realloc and free.
Title: Re: Dynamic Memory Allocation
Post by: Rainstorm on June 22, 2008, 01:56:53 AM
hi.

Any recommendations or tips as to what memory allocation functions to use when & where ?

thanks
Title: Re: Dynamic Memory Allocation
Post by: hutch-- on June 22, 2008, 11:41:27 AM
There is a bit of personal preference in this area. For fixed address single contiguous memory I still prefer GlobalAlloc() with the GMEM_FIXED flag. If I am bashing string data around in a hurry I use OLE string. VirtualAlloc() handles very large blocks well but is badly suited for small repeated allocations. I rarely ever use HeapAlloc() but it does the job fine if you want more of a dos C style of memory control.