I have a function foo(void* pBuf). I need to pass it a 64 bit address but I can't seem to get the right typecast when I'm passing by value.
我有一个函数foo(void* pBuf)我需要传递它一个64位的地址,但是当我传递值时,我似乎不能得到正确的类型广播。
Example: foo(address). Where- uint64_t address=0x00000000DEADBEEF
例如:foo(地址)。- uint64_t地址= 0 x00000000deadbeef哪里
EDIT: Compiling using an ARM compiler.
编辑:使用ARM编译器编译。
uint64_t foo(void *pBuf){
uint64_t retAddr = (uint64_t) pBuf;
retAddr += 0x100000;
return retAddr;
}
I'm on a 32-bit ARM and sizeof(void *)
is 4
我的手臂是32位的,sizeof(void *)是4
Clarification: Why I needed a 64-bit address on a 32-bit ARM? Because my memory map uses 36-bit addressing.
说明:为什么我需要一个64位的地址在一个32位的ARM上?因为我的内存映射使用36位寻址。
3 个解决方案
#1
0
Call it this way:
叫它:
uint64_t address = 0xDEADBEEF;
foo((void*)address);
That is, you cast the address to a void-pointer to be compatible with the function signature.
也就是说,您将地址转换为与函数签名兼容的void指针。
#2
0
You should not use a 64-bits type for an address, as it is undefined behavior for 32-bits (or any non-64 bits) systems.
您不应该使用64位类型的地址,因为它是32位(或任何非64位)系统的未定义行为。
Rather, prefer using uintptr_t
, which is standard C. See this question for more details or this page for references.
相反,更喜欢使用uintptr_t,它是标准的c。
Then a solution could be :
那么解决方案可以是:
uintptr_t address = 0xDEADBEEF; /* will trigger a warning if the constant is > max possible memory size */
foo((void*)address);
Note : if uintptr_t
is not available on your system, size_t
is usually a good second choice.
注意:如果uintptr_t在您的系统上不可用,size_t通常是一个很好的第二选择。
Part 2 :Looks like, in your rephrased question, you want to convert an address into a 64-bits integer.
看起来,在您的重新措辞的问题中,您希望将一个地址转换成一个64位整数。
In which case, a direct cast from ptr to integer is likely to trigger a compiler warning, due to potential differences in wideness.
在这种情况下,从ptr到integer的直接转换很可能会触发编译器警告,这是由于wirant中的潜在差异造成的。
Prefer a double cast : uint64_t value = (uint64_t)(size_t) ptr;
选择双铸:uint64_t值= (uint64_t)(size_t) ptr;
#3
0
I can think of two ways to get this right. Got a solution to my problem by calling foo the first way
我可以想出两种方法来解决这个问题。用第一种方法调用foo来解决我的问题
- foo((void*)(uint32_t)address)
- foo((void *)(uint32_t)地址)
This works only because my input to foo is always a 32-bit value. The returned value can be 64-bit.
这是因为我对foo的输入总是一个32位的值。返回的值可以是64位的。
- Of course, a proper fix would be to change foo itself, if I could modify it. I could just pass foo(&address). Inside foo, retAddr = *pBuf.
- 当然,如果我可以修改foo,一个合适的修正就是修改foo本身。我可以传递foo(&address)。在foo中,retAddr = *pBuf。
Thanks for all the suggestions!
谢谢你的建议!
#1
0
Call it this way:
叫它:
uint64_t address = 0xDEADBEEF;
foo((void*)address);
That is, you cast the address to a void-pointer to be compatible with the function signature.
也就是说,您将地址转换为与函数签名兼容的void指针。
#2
0
You should not use a 64-bits type for an address, as it is undefined behavior for 32-bits (or any non-64 bits) systems.
您不应该使用64位类型的地址,因为它是32位(或任何非64位)系统的未定义行为。
Rather, prefer using uintptr_t
, which is standard C. See this question for more details or this page for references.
相反,更喜欢使用uintptr_t,它是标准的c。
Then a solution could be :
那么解决方案可以是:
uintptr_t address = 0xDEADBEEF; /* will trigger a warning if the constant is > max possible memory size */
foo((void*)address);
Note : if uintptr_t
is not available on your system, size_t
is usually a good second choice.
注意:如果uintptr_t在您的系统上不可用,size_t通常是一个很好的第二选择。
Part 2 :Looks like, in your rephrased question, you want to convert an address into a 64-bits integer.
看起来,在您的重新措辞的问题中,您希望将一个地址转换成一个64位整数。
In which case, a direct cast from ptr to integer is likely to trigger a compiler warning, due to potential differences in wideness.
在这种情况下,从ptr到integer的直接转换很可能会触发编译器警告,这是由于wirant中的潜在差异造成的。
Prefer a double cast : uint64_t value = (uint64_t)(size_t) ptr;
选择双铸:uint64_t值= (uint64_t)(size_t) ptr;
#3
0
I can think of two ways to get this right. Got a solution to my problem by calling foo the first way
我可以想出两种方法来解决这个问题。用第一种方法调用foo来解决我的问题
- foo((void*)(uint32_t)address)
- foo((void *)(uint32_t)地址)
This works only because my input to foo is always a 32-bit value. The returned value can be 64-bit.
这是因为我对foo的输入总是一个32位的值。返回的值可以是64位的。
- Of course, a proper fix would be to change foo itself, if I could modify it. I could just pass foo(&address). Inside foo, retAddr = *pBuf.
- 当然,如果我可以修改foo,一个合适的修正就是修改foo本身。我可以传递foo(&address)。在foo中,retAddr = *pBuf。
Thanks for all the suggestions!
谢谢你的建议!