int main() {...">

Char * c = "1234". The address stored in c is always the same

This was the question asked by the interviewer:

#include<stdio.h>

int main()
{
    char *c="123456";
    printf("%d\n",c);
    return 0;
}

      

This piece of code always prints a fixed number (for example, 13451392), no matter how many times you execute it. Why?

+3


source to share


3 answers


Your code contains undefined behavior: printing a pointer must be done using a format specifier %p

and only after converting it to void*

:

printf("%p\n", (void*)c);

      



This will result in a system dependent number, which may or may not be the same across platforms.

The reason it gets fixed on your platform is probably because the operating system always loads your executable into the same virtual memory location (which might map to different areas of physical memory, but your program never learns). A string literal that is part of the executable will also end up in the same place, so the printout will be the same all the time.

+4


source


To answer your question, the character string "123456" is a static constant in memory, and when the .exe is loaded it always goes to the same memory location.

What c

(or rather what it contains) is the memory address of that character string, which, as I said, is always in the same place. If you print the address as a decimal number, you see the address in decimal form.



Of course, as @dasblinkenlight said, you have to print it as a pointer, because different machines / languages ​​have different conventions about pointer size versus int size.

+1


source


Most executable file formats have the ability to tell the OS loader where the virtual address loads the executable file, for example, the PE format used by Windows contains a ImageBase

field and usually sets the value 0x00400000

for applications.

When the loader first downloads the executable, it tries to download it at that address, if not in use, it downloads it at it, which is mostly true, but if it is in use. It downloads it to a different address given by the system.

The point is that your offset "12345"

in the data section is the same, and the OS loads the image base at the same base address, so you always get the same virtual address, base + offset.

But this is not always the case, one for the reason above, the base address can be used, and most Windows DLLs compiled using MSVC set their base address to 0x10000000

, so only one or no one is actually loaded at that address.

Another case is ASLR address space random randomization , security feature if supported and enabled by the system, MSVC has linker option /DYNAMICBASE

, the system will ignore the specified image base and give you different random addresses on its own.

Two things:

  • You do not have to depend on this behavior, the system can load your program at any address and, thus, you specify a different address.
  • Use %p

    to print the address, on some systems, for example int

    - 4 bytes, and pointers - 8 bytes, part of your address will be sliced.
+1


source







All Articles