Memory layouts learned in theory don't work out as expected when actually printed as addresses.

Asked 1 years ago, Updated 1 years ago, 408 views

Executing the code below resulted in the following results:

As a basic memory layout, if you declare the main function or func function as below and output the address, it will be placed in the stack area, so I expected to get an address close to the variable x as a result of the output.
I understand that the memory layout varies from environment to environment, but is it possible that the self-made function will be placed in memory that is not a stack area?

execution results:

0x1001a3edc
0x1001a3ee0
0x1001a8000
0x16fc5f4dc
0x6000000b0030

Code of interest:

#include<stdio.h>
# include <stdlib.h>

void func(){
    ;
}

intj;

int main() {
    intx = 1;
    printf("%p\n", func);
    printf("%p\n", main);
    printf("%p\n", &j);
    printf("%p\n", & x);
    void*ptr = malloc(1);
    printf("%p\n", ptr);
}

c

2023-02-24 08:19

1 Answers

What is the basis for assuming that you have an execution environment to place the function in the stack area?
I don't think that's the case with Windows, Unix, or Linux.

When the code area and the data area compile and assemble the source code, the translation system is
Do you know what to assign?
It is also organized into a data area and a code area when it is made into an executable file with a linker.
Otherwise, it will be troublesome to calculate the relocation.

If you have a guarantee that the size of the code area is smaller than the size of the stack area, push yourself
It may be possible to push code space into the stack area, but the stack usage is
Won't it decrease?


2023-02-24 11:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.