C结构实例覆盖以前的实例
我是C语言的新手,所以答案可能很明显,但我就是想不通 我正在努力创造。我使用Windows EnumWindows函数在所有窗口中循环。在回调函数中,我为每个窗口创建windowHandle-structure的新实例。然而,出于某种原因,它似乎不会创建新实例,而只是覆盖旧实例。或者它会创建新实例,但当我给属性windowHandle->title赋值时,它会将更改应用到windowHandle的每个实例吗 回调函数:C结构实例覆盖以前的实例,c,pointers,struct,malloc,memcpy,C,Pointers,Struct,Malloc,Memcpy,我是C语言的新手,所以答案可能很明显,但我就是想不通 我正在努力创造。我使用Windows EnumWindows函数在所有窗口中循环。在回调函数中,我为每个窗口创建windowHandle-structure的新实例。然而,出于某种原因,它似乎不会创建新实例,而只是覆盖旧实例。或者它会创建新实例,但当我给属性windowHandle->title赋值时,它会将更改应用到windowHandle的每个实例吗 回调函数: BOOL CALLBACK delegate(HWND wnd, LPARA
BOOL CALLBACK delegate(HWND wnd, LPARAM param){
if (filter(wnd)){ //<- just to make sure it's kind of window that we want.
char windowName[256] = {};
GetWindowText(wnd, windowName, sizeof(windowName));
windowHandle *handle = malloc(sizeof(windowHandle)); //<- create new instance of windowHandle for each window
handle->hWND = wnd;
handle->title = windowName;
insert((windowArray*) param, handle); //<- insert each windowHandle in our 'vector'
}
// but return true here so that we iterate all windows
return TRUE;
}
windowArray* array = array_init(20);
EnumWindows(&delegate, (LPARAM) array); //<- not quite sure what that '&'-sign does?
最后插入:
int insert(windowArray* array, void* handle){
int index = (array->count * array->objectSize);
if(index > 0){
printf("\n%s %s\n", "first element's title is: ", array->windows->title); //<- prints to see if new handle overwrite the previous ones
}
printf("%s %s %s %p %s %d\n", "Inserted element ", ((windowHandle*)handle)->title, " with pointer ", handle, " on index ", index);
fflush(stdout); //<- prints info about first & current window
memcpy(array->windows + (index), (windowHandle*)handle, array->objectSize);
array->count++;
}
然后,如果我打印windowArray->windows的全部内容,我会得到以下结果:
Getting value src , at index 0 and pointer 0000000000FBBB80
Getting value src , at index 16 and pointer 0000000000FBBC80
Getting value src , at index 32 and pointer 0000000000FBBD80
Getting value src , at index 48 and pointer 0000000000FBBE80
Getting value src , at index 64 and pointer 0000000000FBBF80
Getting value src , at index 80 and pointer 0000000000FBC080
Getting value src , at index 96 and pointer 0000000000FBC180
另外,我还想知道为什么windowArray的
windows
-属性的指针不同于在委托中创建的*handle
-对象的指针?这似乎不是很有效的记忆方式?您将handle->title
设置为指向windowName
。但一旦退出此作用域,windowName
将不再存在。因此,您的结构包含一个指向不再存在的数组的指针
char windowName[256] = {};
GetWindowText(wnd, windowName, sizeof(windowName));
windowHandle *handle = malloc(sizeof(windowHandle)); //<- create new instance of windowHandle for each window
handle->hWND = wnd;
handle->title = windowName;
}
// when we get here, windowName ceases to exist
// so why did we store a pointer to it?
charwindowname[256]={};
GetWindowText(wnd、windowName、sizeof(windowName));
windowHandle*handle=malloc(sizeof(windowHandle))//hWND=wnd;
句柄->标题=窗口名;
}
//当我们到达这里时,windowName就不存在了
//那么我们为什么要存储指向它的指针呢?
退出作用域后,跟随
title
指针是一个错误,因为它不再指向任何仍然活动的对象。也许将标题
从char*
更改为char[256]
Ohh,这是有意义的。非常感谢。拯救了这一天。
Getting value src , at index 0 and pointer 0000000000FBBB80
Getting value src , at index 16 and pointer 0000000000FBBC80
Getting value src , at index 32 and pointer 0000000000FBBD80
Getting value src , at index 48 and pointer 0000000000FBBE80
Getting value src , at index 64 and pointer 0000000000FBBF80
Getting value src , at index 80 and pointer 0000000000FBC080
Getting value src , at index 96 and pointer 0000000000FBC180
char windowName[256] = {};
GetWindowText(wnd, windowName, sizeof(windowName));
windowHandle *handle = malloc(sizeof(windowHandle)); //<- create new instance of windowHandle for each window
handle->hWND = wnd;
handle->title = windowName;
}
// when we get here, windowName ceases to exist
// so why did we store a pointer to it?