以无符号整数存储缓冲区的地址; [英] Storing address of buffer in an unsigned integer;
问题描述
我有一个内存缓冲区,我想将其地址存储为无符号整数值.
I have a memory buffer whose address i want to store in an unsigned integer value.
uint8_t* _buff = new uint8_t[1024];
uint64_t* _base_addr = (uint64_t *)_buff;
我希望将_buff或_base_addr所指向的位置的地址(无论如何都是同一位置)存储在uint32_t值中.
I want the address of the location pointed by _buff or _base_addr (anyhow it is the same location) to be stored in say uint32_t value.
这样,当我读取整数值时,它便给了我地址. 怎么办呢?
So that when i read the value of integer it gives me the address. How can this be done ?
推荐答案
您无法在"say" uint32_t
变量中存储地址,因为该地址可能不适合-在64位系统上,指针需要64位存储.而不是固定大小,请使用C99(<stdint.h>
)或C ++ 11(<cstdint>
)的uintptr_t
.
You cannot store an address in "say" uint32_t
variable, as the address might not fit - on 64-bit systems the pointers require 64 bits of storage. Instead of fixing the size, use the uintptr_t
of C99 (<stdint.h>
) or C++11 (<cstdint>
).
要将地址存储在这样的整数变量中,请使用
To store the address in such an integer variable, use
uintptr_t variable = (uintptr_t)pointer;
这篇关于以无符号整数存储缓冲区的地址;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!