Thread: Global variable problem (function related)

  1. #1
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107

    Global variable problem (function related)

    Hello.
    I'm trying to create some kind of error system but I fail and I'm asking if one of you could help me. Here's the deal:
    When one of my functions is about to return FALSE (or -1) I'll make it call a SetError function first (with a string as paramater).
    When the programs notice that the function returns FALSE it will then call the GetError function and display the error to the user.
    My question: How could I create this without using a global variable?

    Here is a simple code example where I hope you see what I mean:

    Code:
    //In "Error.hpp"
    # include <windows.h>
    
    LPSTR StrGlobalError = NULL;//This is what I'm trying to avoid.
    
    VOID SetErrorString ( LPSTR StrError )
    {
    
        StrGlobalError = StrError;
    }
    
    LPSTR StrGetErrorString()//Using a global variable like this makes this function useless
    {
    
        return StrGlobalError;
    }
    Thanks, Ktulu.
    This parameter is reserved

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    What's the problem with it being global exactly?

    If you think about how the Win32 error functions work, an integer describing the error is held within memory and retrieved upon request. Therefore, it is a global. The strings aren't, they're pulled out of a resource file and (maybe/maybe not) destroyed after being copied to an application-supplied buffer.
    Last edited by SMurf; 05-24-2007 at 05:18 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I would either use a static global variable and put your error-handling code in a separate source file, or use something like this hack:
    Code:
    static char **get_error_buffer(void) {
        static char *error = "no error";
        return &error;
    }
    Actually, I'd probably use the former because the latter looks wierd.
    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.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    DWORD GetError(){
         return ErrorFunc(0,0);
         }
    
    DWORD SetError(DWORD Error){
         return ErrorFunc(1,Error);
         }
    
    DWORD ErrorFunc(DWORD RW , DWORD Error){
         static ErrorCode;
    
         switch(RW){
              case 0:
                   return ErrorCode;
              case 1:
                   ErrorCode = Error;
                   return 0;
              }
         return 0;
         }
    No global variable, simple interface. GetError() returns the last set error code, SetError(X) sets the error code to X. Do not call ErrorFunc directly unless you understand what its doing.
    Last edited by abachler; 05-25-2007 at 09:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Replies: 2
    Last Post: 02-09-2006, 06:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM