提问人:loaded_dypper 提问时间:9/13/2022 更新时间:9/13/2022 访问量:37
为什么回调函数不更改全局结构中的值?
why doesn't the callback function change a value in a global struct?
问:
我调用了这个结构,其中包含指向数组的指针和该数组的长度,现在当调用回调函数时,它确实将输出复制到分配的缓冲区中,但 len 没有改变String
char
grabOutput
ptr
#define BUF_SIZE 1024
typedef struct String
{
char * ptr;
int len;
} String;
HANDLE Child_IN_R = NULL;
HANDLE Child_IN_W = NULL;
HANDLE Child_OUT_R = NULL;
HANDLE Child_OUT_W = NULL;
HANDLE handle = NULL;
String str;
DWORD grabOutput(LPVOID lpParam )
{
BOOL success = FALSE;
DWORD dwRead, total =0;
char buffer[BUF_SIZE];
while(1)
{
success = ReadFile(Child_OUT_R, buffer, BUF_SIZE, &dwRead, NULL);
if (!success) break;
memcpy(str.ptr+total, buffer, dwRead);
total += dwRead;
str.ptr = realloc(str.ptr, total+BUF_SIZE);
}
str.len = total; // this does not change the value of str.len
return 0;
}
为什么这段代码不改变全局的值?str.len
String str
答: 暂无答案
评论
memcpy(str.ptr+total, buffer, dwRead)
str.ptr
memcpy
realloc
str.ptr
memcpy