Tuesday, 30 January 2018

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...