开发者

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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜