News:

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

Novice questions on dynamic memory

Started by JRevor, March 06, 2010, 11:47:02 PM

Previous topic - Next topic

MichaelW

There is also crt_calloc, but at least under Windows 2000 it is amazingly slow, much slower than the combination of crt_malloc and RtlZeroMemory.

eschew obfuscation

dedndave

if you use HeapAlloc or GlobalAlloc, you have the option of clearing the allocated memory block
VirtualAlloc always initializes the allocated block to 0

redskull

Quote from: dedndave on March 09, 2010, 10:45:24 AM
if you use HeapAlloc or GlobalAlloc, you have the option of clearing the allocated memory block
VirtualAlloc always initializes the allocated block to 0

No matter which method you use, pages on an NT system come back zeroed.  The heap will 'preinitialize' several pages for you during the process startup, which it fills with "baadcode" or "baadfood" or something else ridiculous, but any additional pages that get allocated will be zeroed.
Strange women, lying in ponds, distributing swords, is no basis for a system of government

JRevor

Thanks a lot for the answers, I'm definitely learning a lot of stuff (Although, it's not compulsory to "zero" the memory dynamically allocated, and I normally don't do it. It's not difficult to keep track of what is garbage and what is not).

Seeing that this thread is still active, I'll ask another question on the same subject.

Why this code doesn't work at all?


;dwordbuffer is a DWORD, and contains a pointer to a zone within certain dynamically allocated
; memory which was previously accessed as [eax + ebx].

   mov dwordbuffer, eax
   add dwordbuffer,ebx
   ; I want to copy the content of msg5( a byte array declared in the .data section of my code)
   ;to the memory pointed by dwordbuffer
   invoke MemCopy,ADDR msg5,dwordbuffer,bytesRead ; CRASH!


EDIT: Also, this doesn't work either. Why?

invoke MemCopy,ADDR msg5,BYTE PTR[eax + ebx],bytesRead

redskull

if my memory serves me, MemCpy takes the destination first, and then the source.
Strange women, lying in ponds, distributing swords, is no basis for a system of government

jj2007

The order is correct, src before dest. The byte ptr is the problem. Consider this code:
xor eax, eax
invoke MemCopy, dword ptr [eax + ebx], addr msg5, bytesRead
xor eax, eax
invoke MemCopy, byte ptr [eax + ebx], addr msg5, bytesRead
xor eax, eax


In Olly, you see it as follows:
0040102E       |.  33C0                     xor eax, eax
00401030       |.  FF35 00204000            push dword ptr [402000]
00401036       |.  68 18204000              push 00402018                      ;  ASCII "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - OK?"
0040103B       |.  FF3403                   push dword ptr [ebx+eax]
0040103E       |.  E8 2D000000              call 00401070
00401043       |.  33C0                     xor eax, eax
00401045       |.  FF35 00204000            push dword ptr [402000]
0040104B       |.  68 18204000              push 00402018                      ;  ASCII "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - OK?"
00401050       |.  66:6A 00                 push 0
00401053       |.  8A0403                   mov al, [ebx+eax]
00401056       |.  66:0FB6C0                movzx ax, al
0040105A       |.  66:50                    push ax
0040105C       |.  E8 0F000000              call 00401070

Note the rather odd mov al, [] etc sequence? Masm tries to push the "address" in the byte at [eax+ebx]. 8 bits is a bit short, or rather: 24 bits short of a real 32-bit address ;-)

Here is the working example:
include \masm32\include\masm32rt.inc

.data?

.data
bytesRead dd SIZEOF AppName
AppAddress dd AppName
AppName db "Masm32 is great", 0
msg5 db 100 dup("x")

.code

start:
mov eax, offset AppAddress
xor ebx, ebx
invoke MemCopy, dword ptr [eax + ebx], addr msg5, bytesRead
print offset msg5, " - OK?"
exit
end start

JRevor

EDIT:  Thanks a lot for your responses!

 I was really surprised that my code didn't work using BYTE PTR, because Greg used it in his code, and it worked perfectly. However, there was something wrong with my code. Somewhere in the procedure the adress eax was pointing to got changed (probably because a function returne a value in eax ), so i ended up accessing the wrong memory adress. That's why i had an error!.

By the way, is there any debugger for masm32 you'd recommend?


GregL

JRevor,

I used BYTE PTR in my code because I was working with BYTEs.  With MemCopy you are working with DWORDs.


JRevor

Quote from: Greg Lyon on March 11, 2010, 06:19:11 PM
JRevor,

I used BYTE PTR in my code because I was working with BYTEs.  With MemCopy you are working with DWORDs.



Okay, thanks for the answer.
Anyways, I ended up making my own memcopy  :lol

GregL

Quote from: JRevorAnyways, I ended up making my own memcopy

Good  :U