我有一个指针 (uint8_t *myPointer),我将其作为参数传递给一个方法,然后该方法为该指针设置一个值,但我想知道使用了多少字节(指向?)我的指针变量。
提前致谢。
最佳答案
指针的大小:sizeof(myPointer)
(等于sizeof(uint8_t*)
)
指针对象的大小:sizeof(*myPointer)
(等于sizeof(uint8_t)
)
如果你的意思是 this 指向一个数组,那是没办法知道的。指针只是指向,并不关心值来自何处。
要通过指针传递数组,您还需要传递大小:
void foo(uint8_t* pStart, size_t pCount);
uint8_t arr[10] = { /* ... */ };
foo(arr, 10);
您可以使用模板来简化整个数组的传递:
template <size_t N>
void foo(uint8_t (&pArray)[N])
{
foo(pArray, N); // call other foo, fill in size.
// could also just write your function in there, using N as the size
}
uint8_t arr[10] = { /* ... */ };
foo(arr); // N is deduced
关于c++ - 如何找到指针使用的字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3087634/