2017-11-14 4 views
-1

私はC言語を新しくしているので、答えは分かりやすいかもしれませんが、私はその周りに頭を浮かべることはできません。Cのインスタンスが前のインスタンスを上書きする

C++ vector kind of structure in Cを作成しようとしています。 WindowsのEnumWindows関数を使用して、すべてのウィンドウをループします。コールバック関数では、各ウィンドウに対してwindowHandle構造の新しいインスタンスを作成します。しかし、何らかの理由で新しいインスタンスを作成するのではなく、古いインスタンスを上書きするだけです。あるいは、新しいインスタンスを作成するかもしれませんが、属性windowHandle-> titleに値を与えると、その変更がwindowHandleのすべてのインスタンスに適用されますか?

コールバック関数:EnumWindows関数へ

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? 

構造:

typedef struct windowHandle{ 
    HWND hWND; 
    char* title; 
} windowHandle; 

typedef struct windowArray{ 
    int count; 
    int capacity; 
    int objectSize; 
    windowHandle* windows; 
} windowArray; 

そして最後に、インサート:

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++; 
} 
挿入時の

出力コール:

Inserted element joo - Java - nativeWindowCapture/src/NativeWindowHookImp.c - Eclipse SDK with pointer 0000000000FB1B90 on index 0 

first element's title is: Komentokehote 
Inserted element Komentokehote with pointer 0000000000FB1BB0 on index 16 

first element's title is: C struct instance overwrites previous instance - Stack Overflow - Mozilla Firefox 
Inserted element C struct instance overwrites previous instance - Stack Overflow - Mozilla Firefox with pointer 0000000000FB1BD0 on index 32 

first element's title is: JNI compiler kutsu – Muistio 
Inserted element JNI compiler kutsu – Muistio with pointer 0000000000FB1BF0 on index 48 

first element's title is: Skype™? 
Inserted element Skype™? with pointer 0000000000FB1C10 on index 64 

first element's title is: eclipse 
Inserted element eclipse with pointer 0000000000FB1C30 on index 80 

first element's title is: src 
Inserted element src with pointer 0000000000FBCCE0 on index 96 
all added 

、その後、私はwindowArray->ウィンドウの全内容を印刷する場合、私は次のような結果を得る:

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 

P.S. windowArrayのwindows属性のポインタが*handleのオブジェクトの代理人で作成されたポインタと異なる理由も知りたいですか?それはメモリ効率的ではないようですね。

答えて

4

handle->titlewindowNameに設定します。しかし、この範囲を終了するとすぐに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? 

それはもはやまだ生きている任意のオブジェクトを指しているので、titleポインタを追跡するとエラーになりません。おそらくtitlechar *からchar[256]に変更します。

+0

ああ、意味があります。ありがとうございました!その日を救った。 – user3738243

関連する問題