Assuming you dynamically allocated memory using malloc()
you release it using free()
. If the memory has been allocated statically, you can not free it.
Follow the Assembly code for a piece of code written in C.
00401334 /$ 55 PUSH EBP ; Inicio da pilha
00401335 |. 89E5 MOV EBP,ESP ; EBP aponta agora para o topo da pilha
00401337 |. 83E4 F0 AND ESP,FFFFFFF0
0040133A |. 83EC 20 SUB ESP,20 ; Espaço alocado na pilha para as variáveis locais
0040133D |. E8 2E060000 CALL Program1.00401970
00401342 |. C74424 1C 0300>MOV DWORD PTR SS:[ESP+1C],0A ; Valor utilizado na função abaixo
0040134A |. 8B4424 1C MOV EAX,DWORD PTR SS:[ESP+1C] ;
0040134E |. 890424 MOV DWORD PTR SS:[ESP],EAX ;
00401351 |. E8 92080000 CALL <JMP.&msvcrt.malloc> ; A função "malloc" é chamada
00401356 |. 894424 18 MOV DWORD PTR SS:[ESP+18],EAX ; Ponteiro para o bloco de memória alocada pela função acima
0040135A |. 837C24 18 00 CMP DWORD PTR SS:[ESP+18],0 ; Verifica se NÃO obtivemos êxito ao alocar memoria
0040135F |. 75 13 JNZ SHORT Program1.00401374 ;
00401361 |. C70424 2430400>MOV DWORD PTR SS:[ESP],Program1.00403024 ; puts() recebe como parâmetro a string "Out of memory!"
00401368 |. E8 73080000 CALL <JMP.&msvcrt.puts> ; puts() é chamado
0040136D |. B8 01000000 MOV EAX,1 ; Coloca em EAX o valor de retorno "1"
00401372 |. EB 48 JMP SHORT Program1.004013BC ; return 1;
00401374 |> 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401378 |. C600 66 MOV BYTE PTR DS:[EAX],66 ; 66(Hex) 102(Decimal) Ascii "f"
0040137B |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
0040137F |. 40 INC EAX ;
00401380 |. C600 6F MOV BYTE PTR DS:[EAX],6F ; 6F(Hex) 111(Decimal) Ascii "o"
00401383 |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401387 |. 83C0 02 ADD EAX,2 ;
0040138A |. C600 6F MOV BYTE PTR DS:[EAX],6F ; 6F(Hex) 111(Decimal) Ascii "o"
0040138D |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401391 |. 83C0 04 ADD EAX,3 ;
00401394 |. C600 00 MOV BYTE PTR DS:[EAX],0 ; "#include<stdio.h>
int main()
{
int nameSize = 10;
char *name = malloc(nameSize * sizeof(char));
if (name == 0)
{
printf("Out of memory!\n");
return 1;
}
name[0] = 'f';
name[1] = 'o';
name[2] = 'o';
name[3] = '00401334 /$ 55 PUSH EBP ; Inicio da pilha
00401335 |. 89E5 MOV EBP,ESP ; EBP aponta agora para o topo da pilha
00401337 |. 83E4 F0 AND ESP,FFFFFFF0
0040133A |. 83EC 20 SUB ESP,20 ; Espaço alocado na pilha para as variáveis locais
0040133D |. E8 2E060000 CALL Program1.00401970
00401342 |. C74424 1C 0300>MOV DWORD PTR SS:[ESP+1C],0A ; Valor utilizado na função abaixo
0040134A |. 8B4424 1C MOV EAX,DWORD PTR SS:[ESP+1C] ;
0040134E |. 890424 MOV DWORD PTR SS:[ESP],EAX ;
00401351 |. E8 92080000 CALL <JMP.&msvcrt.malloc> ; A função "malloc" é chamada
00401356 |. 894424 18 MOV DWORD PTR SS:[ESP+18],EAX ; Ponteiro para o bloco de memória alocada pela função acima
0040135A |. 837C24 18 00 CMP DWORD PTR SS:[ESP+18],0 ; Verifica se NÃO obtivemos êxito ao alocar memoria
0040135F |. 75 13 JNZ SHORT Program1.00401374 ;
00401361 |. C70424 2430400>MOV DWORD PTR SS:[ESP],Program1.00403024 ; puts() recebe como parâmetro a string "Out of memory!"
00401368 |. E8 73080000 CALL <JMP.&msvcrt.puts> ; puts() é chamado
0040136D |. B8 01000000 MOV EAX,1 ; Coloca em EAX o valor de retorno "1"
00401372 |. EB 48 JMP SHORT Program1.004013BC ; return 1;
00401374 |> 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401378 |. C600 66 MOV BYTE PTR DS:[EAX],66 ; 66(Hex) 102(Decimal) Ascii "f"
0040137B |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
0040137F |. 40 INC EAX ;
00401380 |. C600 6F MOV BYTE PTR DS:[EAX],6F ; 6F(Hex) 111(Decimal) Ascii "o"
00401383 |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401387 |. 83C0 02 ADD EAX,2 ;
0040138A |. C600 6F MOV BYTE PTR DS:[EAX],6F ; 6F(Hex) 111(Decimal) Ascii "o"
0040138D |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
00401391 |. 83C0 04 ADD EAX,3 ;
00401394 |. C600 00 MOV BYTE PTR DS:[EAX],0 ; "#include<stdio.h>
int main()
{
int nameSize = 10;
char *name = malloc(nameSize * sizeof(char));
if (name == 0)
{
printf("Out of memory!\n");
return 1;
}
name[0] = 'f';
name[1] = 'o';
name[2] = 'o';
name[3] = '%pre%';
printf("%s", name);
free(name);
return 0;
}
"
00401397 |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
0040139B |. 894424 04 MOV DWORD PTR SS:[ESP+4],EAX ;
0040139F |. C70424 3330400>MOV DWORD PTR SS:[ESP],Program1.00403033 ; ASCII "%s"
004013A6 |. E8 45080000 CALL <JMP.&msvcrt.printf> ; printf() é chamado
004013AB |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
004013AF |. 890424 MOV DWORD PTR SS:[ESP],EAX ;
004013B2 |. E8 41080000 CALL <JMP.&msvcrt.free> ; free() é chamado
004013B7 |. B8 00000000 MOV EAX,0
004013BC |> C9 LEAVE ; Liberar o espaço usado pela função na pilha
004013BD \. C3 RETN ; return 0;
';
printf("%s", name);
free(name);
return 0;
}
"
00401397 |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
0040139B |. 894424 04 MOV DWORD PTR SS:[ESP+4],EAX ;
0040139F |. C70424 3330400>MOV DWORD PTR SS:[ESP],Program1.00403033 ; ASCII "%s"
004013A6 |. E8 45080000 CALL <JMP.&msvcrt.printf> ; printf() é chamado
004013AB |. 8B4424 18 MOV EAX,DWORD PTR SS:[ESP+18] ;
004013AF |. 890424 MOV DWORD PTR SS:[ESP],EAX ;
004013B2 |. E8 41080000 CALL <JMP.&msvcrt.free> ; free() é chamado
004013B7 |. B8 00000000 MOV EAX,0
004013BC |> C9 LEAVE ; Liberar o espaço usado pela função na pilha
004013BD \. C3 RETN ; return 0;
The disassembled code above is a result of this code snippet below.
%pre%
I'm not sure if this is what the OP asked for in the question but I hope it can help in some way. Here are some articles that may help you.
The Function Stack
C Function Call Conventions and the Stack