Saving variables in memory, C ++

Today something strange occurred to me. When I want to draw some string in C (C ++) the old way, without using a string header, I just create an array and store that string in it. But I read that any definition of a variable in C in the local area of ​​the function ends up pushing these values ​​onto the stack.

So, the line is actually 2 * more than necessary. Since at first the push commands are located in memory, but then when they are executed (pushed onto the stack), another "copy" of the line is created. First, instructions are pressed than the stack space for one line.

So why so? Why doesn't the compiler just add the string (or other variables) to the program, and not create them again at runtime? Yes, I know that you can’t just have some data inside the program block, but you can just bind it to the end of the program, with some jump instructions earlier. And what, we just point to this data? Because they are stored in RAM during program execution.

Thank.

+3
source share
4 answers

If you have:

extern void some_function(char * s, int l);

void do_it(void) {
     char str[] = "I'm doing it!";
     some_function(str, sizeof(str) );
}

This would turn into something like (in psudo asm for the processed processor):

.data
local .do_it.str       ; The contents of str are stored in a static variable
.text   ; text is where code lives within the executable or object file
do_it:
    subtract (sizeof(str)) from stack_pointer        ; This reserves the space for str, sizeof(str)
                                                     ; and a pointer to str on the stack

    copy (sizeof(str)) bytes from .do_it.str to [stack_pointer+0]   ; this loads the local variable
                                               ; using to the memory at the top of the stack
                                               ; This copy can be a function call or inline code.

    push sizeof(str)         ; push second argument first
    push stack_pointer+4     ; assuming that we have 4 byte integers,
                             ; this is the memory just on the other side of where we pushed
                             ; sizeof(str), which is where str[0] ended up

    call some_function

    add (sizeof(str)+8) to stack_pointer          ; reclaim the memory used by str, sizeof(str),
                                                  ; and the pointer to str from the stack
    return

From this you can see that your assumption about how the local variable str is created is not entirely correct, but it is still not as efficient as it could be.

If you did

void do_it(void) {
     static str[] = "I'm doing it!";

. some_function str, ( ) do_it ( ) str.

some_function ​​:

extern void some_function(const char * s, int l);

, , , str do_it, , str , str static.

0

C ++:

char string[] = "Contents of the string";
char const *string2 = "Contents of another string";

, , . , , , , .

+5

. , static () EDIT: , , ?

, - . . ?

+1

This is not how it works. Nothing pushes, the compiler just reserves space in the stack frame. You cannot return such a string from a function; you will return a pointer to a dead stack frame. Any subsequent function call will destroy the string.

It returns strings, allowing the caller to pass a pointer to the buffer, as well as an argument indicating how large the buffer is, so you will not overflow the end of the buffer when the string is too long.

+1
source

Source: https://habr.com/ru/post/1739621/


All Articles