Maximum return value malloc ()?

The size_tvirtual memory pointer value returned malloc()has an upper bound?

I am wondering if I can safely set the most significant bit of a 64-bit pointer to indicate that it is not a pointer, but a literal integer.

+4
source share
3 answers

As @datenwolf's response states, you cannot make any assumptions about how mallocthe memory address provides you. An MSB may contain important bits that you could overwrite if you tried to use them to store metadata. I worked on a 32-bit system that returned addresses with bits set in the MSB addresses (not from malloc, but for other memory allocation functions in the system).

, , malloc , . , 32- 4- , 64- 8- . , , 2 3 . , memalign. , , , . / , .

. . , .

+4

malloc a void* . ( ), , , C (0 , ), .

-cast-to-integers, . C .

+8

size_t, , . h

#ifndef SIZE_MAX
#ifdef _WIN64 
#define SIZE_MAX _UI64_MAX
#else
#define SIZE_MAX UINT_MAX

_UI64_MAX UINT_MAX .

#define UINT_MAX      0xffffffff    /* maximum unsigned int value */
#define _UI64_MAX     0xffffffffffffffffui64 

malloc(), 32- Windows () 0 2 , 64- Windows () 0 8 .

Again, on a 32-bit system, starting with WinNT 4, it introduced the / 3G download option. In this case, it malloc()can return any (address) value in the range from 0 to 3 GB of the user mode address space.

For more details see the article by Mark Russinovich here .

0
source

All Articles