How to find the number of bytes used by a pointer?

I have a pointer (uint8_t * myPointer) that I pass as a parameter to a method, and then this method sets the value to that pointer, but I want to know how many bytes are used (pointed to?) MyPointer.

Thanks in advance.

+3
source share
4 answers

Pointer size: sizeof(myPointer)(equal sizeof(uint8_t*))
Pointer size: sizeof(*myPointer)(equal sizeof(uint8_t))

If you meant that this points to an array, there is no way to find out. The pointer simply points and does not care where the value is.

To pass an array using a pointer, you also need to pass the size:

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
+11

. , .

+3

You can not. If the array is not created at compile time. Example int test[] = {1, 2, 3, 4};In this case, you can use sizeof(test)to return you the correct size. Otherwise, it is impossible to determine the size of the array without saving its own counter.

0
source

I assume that you mean the memory needed only for the pointer. You can check this at compile time with sizeof(myPointer).

0
source

Source: https://habr.com/ru/post/1751076/


All Articles