Search code examples
c++cpointersc-libraries

Large unexpected u16_t buf


I have C function that calls pointer to function (with pointer to buf and size of buf)

printf("------------------------------\n");
printf("lengh of DATA_output = %zu\n", p->tot_len);
(*myStr.OnSendData)(buf, p->tot_len);
printf("------------------------------\n");

pointer to function defined like

typedef void (*pOnSendData)(void *buf, u16_t len);

Then my real c++ class method gets it through that pointer:

void Someclass::myPOnSendData(void *buf, u16_t len) {
std::cout << " Someclass::mypOnSendData len = " << (int)len << std::endl;
//Do something
}

The problem is that len of buf is pretty large.

output is :

------------------------------
lengh of DATA_output = 42
------------------------------

Someclass::myPOnSendData len = 50512

How could that be?


Solution

  • You say that myPOnSendData is not a static member function, but you are trying to assign a pointer to it into a C pointer-to-function. This won't work because C-style pointers to functions do not support non-static member function.

    If you change the function to be static, it will be compatible with the pointer type.

    Note that since the types are currently incompatible, you probably got an error message when trying to assign the function pointer, and had to perform a cast to get around it. Don't do this, as you are just hiding the error rather than solving it.