1

I create a vector of uint_8t* sometimes with 3, 4 or 5 bytes but when I make the sizeof my array, I always get 4. How can I solve this?

After creating my array I want to get the length of it.

Example:

uint_8t* u8;

u8[0]=...;
u8[1]=...;

sizeof(u8)=4 instead of 2.

sizeof(uint8_t) is 1, I Alo triedsizeof(u8)/sizeof(u8[0])` and I get 4

김선달
  • 1,485
  • 9
  • 23
mikelasesino
  • 11
  • 1
  • 3

1 Answers1

3

sizeof(uint8_t*) is the size of pointer. Which is typically 4 for 32-bit architectures and 8 for 64-bit architectures. Also typically this doesn't depend on the underlying structure (should be the same for uint8_t*, std::string*, long*, and so on), although this is not guaranteed.

sizeof(uint8_t) is 1 because that's what uint8_t is: a 1 byte.

It seems that you want to know the size of the memory which uint8_t* points at. There is no way to do that given a pointer only. You have to track the size manually. Typically by using std::vector<uint8_t> instead of raw pointers.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Mmm okay I understand. So how could I pass the allocated size? This pointer is the output of a function, and after I need this pointer and also the size.. – mikelasesino May 29 '20 at 08:00
  • @mikelasesino as I said: in general there's no way to do that. Unless this function does something special, like for example it guarantees that the size is always the same. Or maybe it guarantees that the the last element in the array is 0. Otherwise this is very badly written function and you are doomed to fail. – freakish May 29 '20 at 08:05