Const char* data storage structure

Asked 2 years ago, Updated 2 years ago, 122 views

const char* name = "string";
cout << &name;
cout << static_cast<const void*(&*name);

Here, the address of the pointer name changes every time you run it

The address that the name points to (where the letter s is stored) appeared to hold the address.

And the address that the pointer points to changed when you replaced "string" with a different string.

Why do you see this structure?

char string

2022-09-22 14:33

1 Answers

What is the tested os?

If you look at the binary format, it's divided into sections.

There are a few things you need to know to understand the questions.

The currently used os are operating in protected mode, and protected mode is also known as virtual address mode. In other words, when a process is created on a 32-bit operating system, two kernel areas are assigned two user areas and a total of four gigabytes are used.

Here, the kernel region has a one-to-one relationship with the physical memory, but the user region has a one-to-n relationship with the virtual region.

For example, if you open two notepad pages and check the memory value, the value is different even if it is the same memory address.

Canal area is the same, but user area is independent of each other. you understand that.

Because of this form, even if the application is not responding or a memory error occurs, if you just shut down the process, there will be no problem with os...Problems in the kernel area. For example, if an error occurs due to a driver crash, a blue screen will appear and you must exit os.

Now why is the value of the heap space, which is the essential statement, the same...Whether If you look at the window binary format peformat, there are several definitions of space called sections.

When you run the exe file, the payloader secures memory according to the section information and stores variables while the program runs.

The .text section where the code is stored Sections such as .rdata.data, etc. where data is stored

Where .rdata section is the section where initialized constants (read-only) are stored.

Here, the string is a read-only constant, which is stored in the .rdata section, and if you modify the string, the string is read-only, so you have a new space to save. This means that the memory address is changed because the string is not modified and is newly registered.

And if the string variable address is the same every time you run it, you can check the address in the section.


2022-09-22 14:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.