Thread: Function return value and scope question

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Question Function return value and scope question

    Hi there,

    I recently just about managed to complete a little challenge I set myself by getting a windows class registered and window created from functions in a DLL instead of from the WinMain function.

    However... there's one part of it that is more of a C++ question than a Windows question so I thought I'd ask it here.

    It concerns a variable not going out of scope when I thought maybe it would. If I try and declare a HWND variable like this:

    Code:
    HWND hwnd;
    
    createRunWindow(hInstance, hDll, hwnd);
    The compiler will complain (C4700 error code I believe) that the variable is used before being initialised. If I however do this:

    Code:
    HWND hwnd = nullptr;
    or HWND hwnd = 0;
    
    createRunWindow(hInstance, hDll, hwnd);
    I get some crazy errors with the linker (probably something windows specific). But if I make the createRunWindow function to do this:

    Code:
    HWND createRunWindow(HINSTANCE hInstance, HMODULE hDll)
    {
        ....some time later
    
        HWND hwnd;   
    
        CREATEWINDOW _createWindow = (CREATEWINDOW)GetProcAddress(hDll, "createWindow");
        _createWindow(&hInstance, WindowProc, &hwnd);
    
        return hwnd;
    }
    And have the function called in WinMain like this:

    Code:
    HWND hwnd = createRunWindow(hInstance, hDll);
    All works nicely. But here's where it becomes a general C++ question. The WinMain function continues afterwards needing a valid HWND handle for other functions within it to work.

    So my question is, given that the function returns a hwnd before exiting, does that mean the line

    Code:
    HWND hwnd = createRunWindow(hInstance, hDll);
    is ok because it's been initialized to something before the temporary variable in the function went out of scope?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    An example
    Code:
    int question() {
      int answer = 42; // a local
      return answer;  // local goes out of scope
    }
    
    int main() {
      int life = question();
    }
    When the code gets to the return statement, the variable is copied to some implementation defined location (usually a machine register in the case of scalar variables).

    The function returns, the variable goes out of scope, but the value in the register persists.

    Back in the calling function, the register is copied to the variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Thanks very much for a great explanation

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    HWND is a simple scalar type (I think it's defined as a DWORD) so it's safe to return from a function (as Salem said). In this case the Windows "createWindow" function allocates a window and returns a handle to the window; a handle is kinda-sorta like a pointer.

    What's not safe is returning a pointer to an automatic local variable because its lifetime ends when the function returns. On the other hand it is safe to return a pointer to a static local variable since its lifetime is the same as the program, even though it has local scope; scope only limits where you can use an object directly by its name, but lifetime affects how long an object lives and can be used, either directly by its name or indirectly through a pointer or the like.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    More good advice thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope of a Function Return
    By theKbStockpiler in forum C Programming
    Replies: 4
    Last Post: 06-16-2010, 07:57 AM
  2. Newbie function return question
    By faifas in forum C Programming
    Replies: 2
    Last Post: 06-29-2009, 10:19 AM
  3. return value from function (out of scope)
    By jeanluca in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 12:12 PM
  4. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

Tags for this Thread