Global Class problem

This is a discussion on Global Class problem within the C++ Programming forums, part of the General Programming Boards category; If "InputQueue_c InputQueue; " is outside int main ,being declared as global, the code will compile but it will trigger ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    48

    Global Class problem

    If "InputQueue_c InputQueue; " is outside int main ,being declared as global, the code will compile but it will trigger a memory error and crash. if "InputQueue_c InputQueue; " is inside int main() it works fine, or so it seems. What am i doing wrong?
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    struct Queue_str
    { int iFlag;
      void * info;
      int iFrom;
      int iTo; 
      Queue_str * next; 
           
           };
    struct Data_str
    { int iFlag;
      void * info;
      int  iFrom;
      int  iTo; 
      
           };
           
    Data_str * CreateData(void * pArg, int iFlag, int iFrom, int iTo)
    {Data_str * strData;
     strData=new Data_str;
     strData->info=pArg;
     strData->iFlag=iFlag;
     strData->iFrom=iFrom;
     strData->iTo=iTo;
       return strData;
             };
    
    
    
    class InputQueue_c
    {public:
       int iCount;
       Queue_str * pLastRead;
       Queue_str * pStart;
       Queue_str * pEnd;
       
             
            void Input(Data_str * strData) {
                 
                 pEnd->next=new Queue_str;
                 pEnd->next->info=strData->info;
                 pEnd->next->iFlag=strData->iFlag;
                
                 pEnd->next->iFrom=strData->iFrom;             
                 pEnd->next->iTo=strData->iTo;
                 
                 pEnd->next->next=NULL;
                 pEnd=pEnd->next;
                 iCount++;
                
                 } 
            Data_str *  Read()
            {   Data_str * strData;
               strData=new Data_str;
                
                if (pLastRead==pEnd)
               {strData->info=(void*) "\0";
                strData->iFlag=-1;
                return strData;
                   }
                
            Queue_str *pBuffer;
              pBuffer=pLastRead->next;
              pLastRead=pBuffer;
              strData->iFlag=pBuffer->iFlag;
              strData->info=pBuffer->info;
              strData->iFrom=pBuffer->iFrom;
              strData->iTo=pBuffer->iTo;
              return strData;
                 
                 }
              bool Unread() {if(pEnd==pLastRead) return 0;
                           else return 1;
                }   
            InputQueue_c(){iCount=0; pLastRead=pStart; pEnd=pStart; }
           ~InputQueue_c(){} 
          };
          
    
      InputQueue_c InputQueue;  
    int main()
    
    {   
         InputQueue.Input(CreateData((void*)"test",1, 0,0 )) ; //at this point it will crash
        cout<<(char*)InputQueue.Read()->info;
    cin.get();
        
        }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can I first say that your tendency to put code immediately right of a brace is not particularly pleasant. It also completely prohibits auto-formatting in MS Visual Studio...

    As to the crash, I'm surprised it DOESN'T crash when you have a local instance of the class - but since pStart is not set at all, it may randomly point to some writeable memory in your particular case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    tjb
    tjb is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Try initializing pStart. That it's working when it's a stack variable... I think you're just getting lucky.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tjb View Post
    I think you're just getting lucky.
    I would put lucky in quotes, because "being lucky" when it comes to uninitialized variables is when it is discovered quickly, not when the code "works" when trying out the first few times.

    I also don't quite understand the purpose of pStart - it is only used once in two places, to initialize some other pointers. [I haven't spend many seconds trying to understand the code - only enough to spot what the initial problem is].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    thanks, i should be more careful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  2. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  3. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  4. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 05:16 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21