How to pass a struct as a pointer? [C++]
I have a struct that I'm passing through CreateThread
packetargs *args;
args->s=s;
args->buf=buf;
args->len=len;
args->flags=flags;
args->to=to;
args->tolen=tolen;
CreateThread(NULL,0,mThread,args,0,NULL);
But when I receive it in my Thread function, it crashes the application(Because the information is wrong):
DWORD WINAPI mThread(LPVOID args)
{
packetargs *pargs = (packetargs *)args;
How am I supposed to pass 开发者_开发百科the struct as a pointer and then create it again in the thread function?
You forgot to allocate any memory for your struct:
packetargs *args = new packetargs;
(Of course, you'll need to delete
it at some point.)
精彩评论