1

Let's say I have a C function:

Func(void* param);

Is there a way to know number of bytes of param content?

klutt
  • 30,332
  • 17
  • 55
  • 95
  • Not portably, no. All those standard library and user-defined functions you see passing a `size_t` length, in addition to a memory address in a pointer, are doing so for a reason. One of *many* related questions to this [can be seen **here**](https://stackoverflow.com/questions/9390615/how-to-calculate-size-of-array-from-pointer-variable). – WhozCraig Nov 22 '17 at 16:33

3 Answers3

5

No, it is not possible. That's why almost all functions that takes pointers as arguments has an extra argument for size. It should be like this:

void func(void *param, int size);

Also note that it is up to the function to interpret the size argument. It can be number of bytes, number of elements or whatever.

The important thing here to remember is the only thing param is, is a memory address. Nothing more, nothing less.

Some functions doesn't utilize this. One example is strcpy(char * dest, char * src) that copies a string, but this function assumes two things. It assumes that src is a zero-terminated string. strcpy will read from src until it hits a '\0' character. Furthermore, it assumes that dest points at a memory location big enough to hold the string.

sizeof(*param) will not work. param is only a pointer to where the memory chunk starts. It would be the same as using sizeof(void)

klutt
  • 30,332
  • 17
  • 55
  • 95
  • @Hollyol you can't dereference a `void*`. Given your OP, what type is `param`? We don't know, the compiler doesn't know either. You have to cast it to its appropriate type before you can dereference it. – yano Nov 22 '17 at 16:46
  • " Especially when it comes to void pointers, it can be a bit ambiguous" I'd argue the opposite. There's no reasonable unit of measure for the size of the object pointed to by a `void*` except bytes. – JeremyP Nov 22 '17 at 16:58
  • @JeremyP True, I removed that. – klutt Nov 22 '17 at 17:06
3

For starters the type void is an incomplete type that is its size in bytes is indeterminate.

From the C Standard (6.2.5 Types)

19 The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

However if you have a pointer of some other type as an argument of a function as for example

T *param

then in general case you again will be unable to determine the number of bytes occupied by the referenced actual data.

Pointers do not keep information whether they point to a single object or the first object of an array.

For example if you have a function declared like this

void f( const char *s );

then it can be called for example for these objects of different types

char c = 'A';
f( &c );

and

char s[] = "Hello";
f( s );

Of course you can get the number of bytes occupied by the element of an array or a single object referenced to by pointer as for example using expression

sizeof( *param )

But in case of the type void * you may not do even this though some compilers have their own language extensions that allow to apply the operator sizeof for the type void. In this case the returned value of the expression is usually equal to 1.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

No. Without knowing what param is pointing to, you can't know what size it is.

Seth
  • 2,683
  • 18
  • 18