Suppose you assigned memory dynamically using malloc()
you release it using free()
. If memory has been statically allocated, you can’t release it.
Follows the code Assembly of 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 ; "\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 code dismounted above is the result of this code snippet below.
#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] = '\0';
printf("%s", name);
free(name);
return 0;
}
I’m not sure that’s what the OP asked in the question but I hope I can help in some way. Below are some articles(in English) that may assist you.
- The Function Stack
- C Function Call Conventions and the Stack
If you put an example code that uses the stack and accepts a response that speaks strictly about the
malloc()
your question is not clear. I already thought that was not much, gave room for interpretation and tried to answer addressing everything that gave. Even after the answer you never said it was aboutmalloc()
who is interested. The answer accepts both does not effectively answer the question asked (and the blame is the question) that the code posted is completely different from the question.– Maniero
@bigown the question accepts several answers. Simply the chosen one was the solution.
– user2692
If you can, add OS-only and scope ways to your answers. They also work.
– user2692
If it accepts several solutions, it is very wide. But actually the same problem is that you asked one thing and wanted another.
– Maniero
@mustache after all, what do I want? I want to know how to take a variable from memory in C. There are 3 ways (thanks to the answers and the surveys I already know), that’s all.
– user2692
If you don’t know what you want, who am I to know. I can only tell you what you asked. You clearly put a code that uses stack, you didn’t use an example with heap and didn’t mention it. I even tried to give an answer that looked at all the angles and you tried to edit it saying that it had no focus. Your question has no focus and you admitted it above. I doubt the answer you accepted helped you in anything. That you’ll get to do something with this Assembly. And if you can, you’d have no reason to ask the way you asked.
– Maniero