How to remove a variable from memory? [closed]

1

How do I remove a variable, not necessarily from the code, but from memory / register, in C?

Hypothetical example:

char meu_byte;
//codigo
deleta_da_memoria(meu_byte);

That generates something like:

pushb 0
; Utiliza o byte
call deleta_da_memoria

That is, I want to know what process to delete a static variable in C, considering the CPU instructions as necessary information.

    
asked by anonymous 04.01.2015 / 22:28

2 answers

8

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
  • 04.01.2015 / 23:01
    8

    Depends on what you mean by "deleta".

    If you want the value to be deleted, you can at most zero it, ie meu_byte = 0; .

    If you want to free memory depends on how it was allocated.

    This particular case is allocated in stack so the release will only occur when this variable exits the scope, ie exits the current execution block (which is in braces). You do not explicitly release stack memory.

    Actually the release does not actually erase the value, the value is dropped there in memory. If you want, perhaps for security, that nothing can unduly access this value further - though unlikely - you must zero it out before you leave the scope. Most probably soon after some execution will take advantage of this space released and throw something over, since it has a die there but it was considered released.

    If it had been allocated with malloc() in heap the release should be done explicitly with free() but this is not the case and I think if you were doing with malloc() you would use free() .

    But you probably have a concern that should not exist. At least your question does not make it clear that there is some extra reason behind it. Or you might want to be smarter than operating system implementers, languages, and compilers.

        
    04.01.2015 / 22:55