1

I have a C program that does the following two operations:

struct element *e = (struct element*) malloc(sizeof(struct element));
long unsigned addr = (long unsigned) e;

From this, addr has the decimal value of the pointer. I can convert addr back to an element pointer and use that to get the element from memory.

I am wondering how many possible values addr can be. I know the maximum value of long unsigned is about 4.3 billion, but can I really have an addr value of 1? Is there a certain range of numbers that I can get, and what is that range dependent on, if anything?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Joseph Thweatt
  • 316
  • 2
  • 14
  • 2
    Use `intptr_t` or `uintptr_t` (``) – BLUEPIXY Jul 01 '17 at 22:32
  • 2
    The memory layout is determined by the OS. But why do you care anyway? It does not seem like a useful thing to know. – kaylum Jul 01 '17 at 22:32
  • 1
    On most CPUs, numbers are stored in binary, not decimal. Using `long unsigned addr` is not safe — it won't work for 64-bit Windows, for example. For safety, you'd use `uintptr_t` from `` (or ``), if it is available. (I know of no system where it is not available.) – Jonathan Leffler Jul 02 '17 at 00:05

1 Answers1

2

Some addresses are reserved for the Operating System (OS) and are usually located in the low memory addresses. However you shouldn't be much interested in the addresses* your data will have, since they are determined by the OS, thus is OS-dependent, plus much dependent in the current state of the OS (an OS with many programs been executed will behave differently than one with few programs executed, and give your program different addresses in these two cases).

Read more in C : Memory layout of C program execution.


Use intptr_t (<stdint.h>) for the address, as BLUEPIXY said (more).


I have a C program that does struct element *e = (struct element*) malloc(sizeof(struct element));

Do I cast the result of malloc? No!


*Except if you are writing OS code or embedded code with no OS might very well be interested in what addresses data might have, as Carey Gregory said, or other special cases.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Someone writing OS code or embedded code with no OS might very well be interested in what addresses data might have. – Carey Gregory Jul 01 '17 at 22:46
  • @CareyGregory I was thinking about it when writing the answer, but was not sure whether to include it or not, thank you! Hope you like the answer now! – gsamaras Jul 01 '17 at 22:49
  • 1
    Better. :-) Although I doubt the OP is doing either of those things, it's always good to keep in mind that some people do program right down to the bare metal. – Carey Gregory Jul 01 '17 at 22:54
  • Being an embedded developer (right down to the bare metal), I don't see a single reason to "be interested in ...". Also, beyond the embedded world, a program lives in a virtual address space, and has the same memory map regardless of what you described as a state of the OS. – user58697 Jul 02 '17 at 07:24
  • @user58697 you have a point, do you suggest I should remove that sentence? – gsamaras Jul 02 '17 at 12:27