Sunday, 10 February 2019

c++ - Cast a void pointer to a struct or memcpy to new struct?

I am passing a void * as an argument to a function foo(void* parm) which calls function bar(STmyStruct* parm)
Should I cast the void pointer to STmyStruct* or create a new struct, memcpy what the void* points to and then pass a pointer to that struct?
I guess solution A is more efficient?



A:    foo(void* parm)
{
bar((STmyStruct*) parm)
}

B: foo(void* parm)

{
STmyStruct myStr;
memcpy(&myStr, parm, sizeof(STmyStruct));
bar(&myStr)
}


bar(STmyStruct* mystr);

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...