2

In my example, strcpy_s and malloc_s throw an error while manual copying seems to work. here's the code. This works:

hookaddrinfoluacode=(char *)malloc(16384);
// This works           
for(i=0;i<strlen(this_token);++i){
   hookaddrinfoluacode[i]=this_token[i];
}                       
hookaddrinfoluacode[i+1]='\0';

This doesn't

memcpy_s(hookaddrinfoluacode,sizeof(char),this_token,strlen(this_token));

And neither does this:

strcpy_s(hookaddrinfoluacode,strlen(this_token),this_token);

The error seems to be thrown from this code line in standard library:

 _VALIDATE_RETURN_ERRCODE(dst != NULL, EINVAL);

this_token is obtained from this_token=strtok_s(NULL,":",&next_token); call.

I'm confused :)

4

2 回答 2

2

memcpy_s throws an exception if the source won't fit in destination. sizeof(char) is 1. Both also validate pointers are not NULL, so you must have one.

于 2012-04-17T13:20:42.663 回答
0

您看到的错误似乎是在验证接收到的目标指针不为空。你在使用它之前分配它吗?正如你所展示的,你是手动做的吗?除此之外,您应该将目标缓冲区的大小作为第二个参数,并在第四个参数中给出要复制的字节数。它应该是这样的:

hookaddrinfoluacode=(char *)malloc(16384);
memcpy_s(hookaddrinfoluacode, 16384 * sizeof(char),this_token,strlen(this_token) );
于 2012-04-17T13:26:31.287 回答