How could I print the size of the entire address space in a C program ?
Asked
Active
Viewed 165 times
-2
-
2C has no idea of address space. It's platform-specific – phuclv Sep 28 '22 at 10:08
-
1Entire address space of _what_? – Lundin Sep 28 '22 at 10:08
-
1Do you mean the amount of memory used by your program... or do you really mean address space? BTW: Read https://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details – Support Ukraine Sep 28 '22 at 10:11
-
From C point of view size of entire address space is `2↑(8*sizeof(void *))`. But not all addresses are accessible. – dimich Sep 28 '22 at 10:53
-
@dimich No, program space may be larger. `2↑(8*sizeof(void *))` limits objects, not functions. Consider 32-bit `void*` and 64-bit function pointers. – chux - Reinstate Monica Sep 28 '22 at 11:45
-
@dimich that's not true at all, [`char` doesn't always has 8 bits](https://stackoverflow.com/q/2098149/995714), and there can be [padding bits](https://stackoverflow.com/q/4475540/995714) and [trap representations](https://stackoverflow.com/q/6725809/995714). The theoretical maximum is therefore `2^(sizeof(void*)*CHAR_BIT - NUMBER_OF_PADDING_BITS) - NUMBER_OF_TRAP_REPRESENTATIONS`, and that only covers the data address space. The *code* address space (i.e. function pointers) can be bigger or smaller. There's no pointer to member in C but in C++ they're even bigger than normal function pointers – phuclv Sep 29 '22 at 03:40
1 Answers
3
On linux (POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD):
getrlimit is your friend, maybe one of the below is what you're looking for
- RLIMIT_AS, maximum size of the process's virtual memory (address space)
- RLIMIT_DATA, maximum size of the process's data segment (initialized data, uninitialized data, and heap)
- RLIMIT_STACK, maximum size of the process stack
I have to pass, when it comes to other Operating Systems but if you google for getrlimit port, maybe you'll find one that suits your needs.

Erdal Küçük
- 4,810
- 1
- 6
- 11