News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Dynamic Memory Allocation

Started by n00b!, June 16, 2008, 08:38:49 PM

Previous topic - Next topic

n00b!

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?

jj2007

Yes, there is. Check \masm32\help\hlhelp.hlp, String macros, Allocating String Buffers

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

hi.

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

thanks

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php