Tuesday, January 30, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...