How to remove a variable from memory?

Asked

Viewed 420 times

1

How to remove a variable, not necessarily from the code, but from the memory/record, 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 is the process to delete a static variable in C, considering the CPU instructions as necessary information.

  • 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 about malloc()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.

  • 1

    @bigown the question accepts several answers. Simply the chosen one was the solution.

  • If you can, add OS-only and scope ways to your answers. They also work.

  • If it accepts several solutions, it is very wide. But actually the same problem is that you asked one thing and wanted another.

  • @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.

  • 1

    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.

Show 1 more comment

2 answers

9


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.

  1. The Function Stack
  2. C Function Call Conventions and the Stack
  • Just to test my knowledge: you are on a 64 bit system, Windows, right? The program is Test1, more specifically...

  • :\ It’s time to study harder...

  • WHAT IS OP ?...

  • @axell13 original poster

8

Depends on what you mean by "delete".

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

Whether to release memory depends on how it was allocated.

In this specific case is allocated to stack so the release will only occur when this variable goes out of scope, that is to leave the current execution block (which is between keys). You do not explicitly release the memory of stack.

In fact liberation does not actually erase value, value is dropped there in memory. If you wish, perhaps for security, that nothing can improperly access this value forward - although unlikely - you should improve it before leaving the scope. Most likely 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 the heap the release should be made explicitly with free() but it’s not the case and I think if you were doing with malloc() would know how to use the free().

But you’re probably worried there shouldn’t be. At least your question doesn’t make it clear that there’s some extra reason behind this. Or maybe you want to outsmart operating system implementers, languages and compilers.

  • @OP The case is memory allocation. The reality is that I’m starting with Assembly and in some C/C++ files I use features inline (so I want to know the back of Assembly). Yeah, I’m being a little clever, I’m doing reverse engineering hehehehe. I don’t know (yes, I do, but I didn’t know it was the case) malloc. That’s why I didn’t know how to allocate. And you know, there’s no reverse engineering except in C.

  • There is reverse engineering in any code in any form. In general there is no Assembly to do so. I wouldn’t know to say the exact implementation and may even vary, but essentially in question I Linkei shows how the stack. Basically it is a pointer that usually sits on a processor-specific log that shows in which stack position the next value will be allocated when needed. When it goes "allocating", the pointer goes up (or down as some like) and when "desaloca" the pointer goes in the opposite direction. Release is only a decrease in SP.

  • To recap what I didn’t say: reverse engineering binary. What would be SP?

  • 1

    Stack Pointer. It’s the register I told you about, it ran out of space. You can ask a question :P http://en.wikipedia.org/wiki/Call_stack

  • Why don’t you ask a Wiki question? (Asked and answered by the same user)

  • I think a similar in Assembly is OS-only. Take a look at the response of "Gunner": http://forum.nasm.us/index.php?topic=1659.0

  • 2

    @Lucashenrique Take a look at this page, may be of interest to you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.