开发者

How to pass a struct as a pointer? [C++]

开发者 https://www.devze.com 2023-03-16 05:34 出处:网络
I have a struct that I\'m passing through CreateThread packetargs *args; args->s=s; args->buf=buf;

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

精彩评论

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

关注公众号