Thread: Massive Confusion with Pointers

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Massive Confusion with Pointers

    Hey,

    Even though this uses the WinAPI for threading, the pointers part is general C++, so I think it would probably go best here.

    This code is probably invalid, I just created a over-simplified version of what I'm actually trying to accomplish.

    Code:
    struct KeyBind
    {
         int * a;
    };
    
    DWORD WINAPI thread ( LPVOID lpParam )
    {
         KeyBind *kb = reinterpret_cast <KeyBind *>(lpParam);
    
         *kb->a = 5;
    }
    
    void a ( int &var )
    {
         KeyBind kb;
    
         *kb.a = var;
    
         DWORD dwThreadId;
    
            ///create the thread
    	HANDLE hThread = CreateThread ( NULL, 0, keythread, reinterpret_cast<LPVOID>(&kb), 0, &dwThreadId );
    }
    
    int main ( void )
    {
         int i = 4;
    
         a ( i ); //i should now equal 5 from the thread...
    }
    So essentially, I'm trying to change the value of i from with a struct within a thread within a function within the main function.

    As of now, I have absolutely no idea what I'm doing anymore. I think I'm over complicating this, which has fried my brain.

    To avoid someone saying "Why are you putting one variable into a struct?," The struct has like 5 variables in it, which the thread needs. I just need to change the value of the variables with pointers and stuff so the value of the variable in the function main will be changed.

    Any help will be appreciated.

    Thanks,

    Guitarist809
    ~guitarist809~

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    *kb.a = var;
    Wrong way around.
    Code:
    kb.a = &var;
    [edit] The former dereferences kb.a and stores the value of var in that location; the latter takes the address of var and stores it in the pointer kb.a, so that when you go "*kb->a = 5;" later, 5 will be stored in the memory location of var. [/edit]
    Last edited by dwks; 07-22-2008 at 03:05 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thanks for the response.

    Well, it compiled, and I got this error while it was running:
    Quote Originally Posted by Microsoft
    Unhandled exception at 0x009929ef in Quick Keys.exe: 0xC0000005: Access violation writing location 0x00000000.
    ~guitarist809~

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    You also nee to wait for the thread to actually run.

    You set i = 4, then execute function a(), but function a() creates the thread and immediately returns. Expecting the thread to execute is undefined behavior. You should either WaitForSingleObject(hThread , INFINITE) on the threads handle, or put some signalling in place to let main() know when the thread has actually finished its work. Also, always close the threads handle when you are finished or it will leak system resources. If you create a lot of threads without freeing the handles after they exit, eventually windows will refuse to start any more threads.

    Code:
    DWORD WINAPI thread ( LPVOID lpParam ){
    
         KeyBind *kb = reinterpret_cast <KeyBind *>(lpParam);
    
         *kb->a = 5;
    
       return 0;
    
        }
    
    
    
    void a ( int &var )
    
    {
    
         KeyBind kb;
    
         *kb.a = var;
    
         DWORD dwThreadId;
    
            ///create the thread
    
        HANDLE hThread = CreateThread ( NULL, 0, keythread, reinterpret_cast<LPVOID>(&kb), 0, &dwThreadId );
    
       WaitForSingleObject(hThread , INFINITE);
       CloseHandle(hThread);
    
    }
    
    
    
    int main ( void ){
         int i = 4;
    
         a ( i ); //i should now equal 5 from the thread...
    
    }
    Last edited by abachler; 07-22-2008 at 03:16 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's strange . . . if I change the code to this, so that it compiles on my Linux box, I get an output of "5".
    Code:
    #include <iostream>
    
    struct KeyBind
    {
         int * a;
    };
    
    void thread ( void *param )
    {
         KeyBind *kb = reinterpret_cast <KeyBind *>(param);
    
         *kb->a = 5;
    }
    
    void a ( int &var )
    {
         KeyBind kb;
    
         kb.a = &var;
    
         thread(&kb);
    }
    
    int main ( void )
    {
         int i = 4;
    
         a ( i ); //i should now equal 5 from the thread...
    
         std::cout << i << std::endl;
    }
    It must be something to do with the thread . . . .

    [edit] See abacler's post. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Im not as familiar with the linux version of starting threads. The original code may work on windows as well, but it sets up a race condition and AFAIK the thread isnt guaranteed to execute before the createthread returns. CreateThread doesnt actually execute the thread, it just creates it and schedules it for execution.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    With the code you gave me WaitForSingleObject() and CloseHandle() the program freezes. The thread is supposed to terminate when a key is pressed (returns an int), but now nothing happens.


    **EDIT**

    Works in a console, MFC just likes to not do what it's told to .

    Gotta figure this out
    Last edited by guitarist809; 07-22-2008 at 03:41 PM.
    ~guitarist809~

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    You shoudl compile without MFC for console programs. Also, are you using the multithreaded version of the runtime library? By default i think it links the single threaded version. Not sure if this is the problem.

    It shodul compile and run perfectly as provided. Since there is no keyboard code in the example you showed, perhaps you could post the actual code you are having a problem with.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    That's what I was thinking. I got rid of all of the code except the "return 0" part of the thread, and it still freezes. I'm guessing MFC and Console Applications are entirely different.

    What I'm doing is when a button is pressed it calls the function start the thread and do it's thing. Maybe this is what's causing the error?


    ******EDIT*******

    Everything is working fine. The code above does work fine for MFC. There was a SendMessage function in the thread which caused some sort of holdup in the thread indefinitely.

    Thanks everyone for the help!
    Last edited by guitarist809; 07-22-2008 at 04:20 PM.
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Pointers to function(function pointers)
    By abhishek_charli in forum C Programming
    Replies: 4
    Last Post: 06-23-2002, 01:24 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM