flat assembler
Message board for the users of flat assembler.

Index > Main > realloc not work

Author
Thread Post new topic Reply to topic
wooai



Joined: 21 May 2023
Posts: 1
wooai 21 May 2023, 16:17
code out:

hello world!!!
memory alloced
array2 setted
memory realloced
array2 setted
array2 0 line 2


Code:


macro AssignText param1,param2


        mov rax,param2
        mov [param1],rax

    
    end macro



    txt1 db 'test 11',0
txt2 db 'line 1',0
txt3 db 'line 2',0

macro Main 


        invoke printf, 'hello world!!! %s',EOL

        invoke malloc, 1 * 8
mov [array1], rax
        invoke malloc, 1 * 8
mov [array2], rax

        invoke printf, 'memory alloced %s',EOL


        AssignText (array2+0*8), txt1

        invoke printf, 'array2 setted %s',EOL

        invoke realloc, [array1],2 * 8
mov [array1], rax
        

        invoke printf, 'memory realloced %s',EOL


        AssignText (array1+0*8), txt2

        AssignText (array1+1*8), txt3

        invoke printf, 'array2 setted %s',EOL

        invoke printf, 'array2 0 %s %s',[array2+0*8],EOL

    
    end macro

    array1 dq ?
    array2 dq ?
    


why not work ??
array1 not realloced
Post 21 May 2023, 16:17
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1657
Location: Toronto, Canada
AsmGuru62 21 May 2023, 23:25
It looks like AssignText macro is just erasing allocated pointers.
Maybe, what you want is to COPY the strings into allocated blocks of memory?
If realloc function detects a 'bad' pointer in the chain of blocks -- it may fail.
I did not run your code in debugger, but here is how I would make the AssignText macro:
Code:
macro AssignText buffer, source
    mov rdi, [buffer]
    mov rsi, source
@@:
    movsb
    test al, al
    jnz @r
end macro
    
Post 21 May 2023, 23:25
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.