简体   繁体   中英

why the *P from malloc return an adress?

I'm trying to understand why the *ptr returns an address and when i try to point a pointer to that address. the code doesn't work? i want to know what does that address mean or it's just some garbage in the heap?

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

int main()
{
    int* ptr = (int*)malloc(sizeof(int));
    printf("%d %d\n", *ptr, ptr);
}

this is what it shows: 1315264432 1315247120

%d is the wrong format specifier for int* (in fact, there is no correct format specifier for int* at all). If you use format specifier for wrong type, then the behaviour of the program is undefined.

The mallocated memory is uninitialised. There isn't any object there, and attempting to access it as if there was an object results in undefined behaviour.

Undefined behaviour explains erverything that you can (and need to) know about the behaviour that you observe from the perspective of the language.

Try this:

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

int main()
{
    int* ptr = (int*)malloc(sizeof(int));
    *ptr = 42; // Or any integer value you like
    printf("%d %08lx\n", *ptr, ptr);
}

First, you allocate the memory, then you initialize it to whatever value you want.

15.3 The 32 bit Memory Addressing

  • The 386, 486 and Pentium processors use 32 bit address space capable of addressing 4 GB memory.
  • These processors are still capable of using the segment registers, but Windows keeps them fixed and it uses 32 bit flat addressing space.
  • The 32 bit addresses used by Windows are not physical addresses. The applications are using virtual addresses.
  • The virtual addresses are translated to physical addresses through a page table.
  • The physical memory is divided to 4096 byte pages. Each page address begins at an address with the lower 12 bits zero.
  • Every process has an own directory page with up to 1024 32-bit entries. The physical address of the current directory page is stored in the processors CR3 register, which is changed when the processor switches control between processes.
  • The structure of the virtual address:
  • Upper 10 bits: specify one of the 1024 entry in the directory page
  • The structure of the directory entry:
  • The upper 20 bit indicates a physical address of a page table. (The bottom 12 bits are zero.) This references another directory page with up to 1024 entries.
  • Middle 10 bits: reference one of the 1024 entry of the second directory page. This entry references to a page frame, which is a physical address.
  • Bottom 12 bits: specify the physical position within the page frame. d: directory page entry p: page table entry o: offset Virtual address: dddd-dddd-ddpp-pppp-pppp-oooo-oooo-oooo The contents of the CR3 register: rrrr-rrrr-rrrr-rrrr-rrrr (20 bits) The starting physical address of the process's current directory page: rrrr-rrrr-rrrr-rrrr-rrrr-0000-0000-0000 (32 bits) First processor access (directory page): rrrr-rrrr-rrrr-rrrr-rrrr-dddd-dddd-dd00 63 This location contains a 20 bit table entry: tttt-tttt-tttt-tttt-tttt The starting physical address of a page table: tttt-tttt-tttt-tttt-tttt-0000-0000-0000 The next processor access (page entry): tttt-tttt-tttt-tttt-tttt-pppp-pppp-pp00 This location contains a 20 bit page frame value: ffff-ffff-ffff-ffff-ffff The final 32 bit physical address: ffff-ffff-ffff-ffff-ffff-oooo-oooo-oooo

http://eik.bme.hu/~csurgai/Mswin_en/WinProg.pdf

pointer value is same as virtual address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM