illegal operation

This is a discussion on illegal operation within the Windows Programming forums, part of the Platform Specific Boards category; I'm working on a small program to test some file i/o. I have a class (CMything): Code: class CMything { ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    illegal operation

    I'm working on a small program to test some file i/o. I have a class (CMything):
    Code:
    class CMything
    {
    private:
      long l1;
      long l2;
      long l3;
      RECT rc;
      char cC[30];
    public:
      CMything();
      void Setl1(long lNew);
      long Getl1();
      void Setl2(long lNew);
      long Getl2();
      void Setl3(long lNew);
      long Getl3();
      void SetRC(long lL, long lT, long lR, long lB);
      LPRECT GetRC();
      void SetCC(char cNew[30]);
      char* GetCC();
      ~CMything();
    };
    Pretty simple and straight forward. Just a generic object that I can use in my file read/write ops. I have an array of 5 of these objects. I have 4 functions: 1 to fill the objects with data, 1 to write the data to a file, 1 to read the data in from a file and 1 to display the data read in so I can make sure everything went ok.

    Code:
    CMything cmThing[5];
    
    void FillData()
    {
       for(lCount=0;lCount<5;lCount++)
       {
         cmThing[lCount].SetCC("Jeff Lenoce");
         cmThing[lCount].Setl1(lCount*2);
         cmThing[lCount].Setl2(cmThing[lCount].Getl1()+1);
         cmThing[lCount].Setl3(cmThing[lCount].Getl2()+1);
         cmThing[lCount].SetRC(0,0,32,32);
       }
    }
    
    void SaveData()
    {
       lSize=sizeof(CMything);
       ULONG fp=0;
       HANDLE hFile=CreateFile("cmThings.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
       for(lCount=0;lCount<5;lCount++)
       {
        fp=lCount*lSize;
        WriteFile(hFile,&cmThing[lCount],lSize,&fp,NULL);
       }
      CloseHandle(hFile);
    }
    
    void ReadData()
    {
      lSize=sizeof(CMything);
      ULONG fp=0;
      HANDLE hFile=CreateFile("cmThings.dat",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
      for(lCount=0;lCount<5;lCount++)
      {
      fp=lCount*lSize;
      ReadFile(hFile,&cmThing[lCount],lSize,&fp,NULL);
      }
      CloseHandle(hFile);
    }
    
    void DisplayData()
    {
      char cL1[20], cL2[20], cL3[20], cCount[20];
      char cRL[20], cRT[20], cRR[20], cRB[20];
      char cSep[5]=" - ";
      RECT rcTest;
      for(lCount=0;lCount<5;lCount++)
      {
        char cCat[10]="My Thing";
        _ltoa(cmThing[lCount].Getl1(),cL1,10);
        _ltoa(cmThing[lCount].Getl2(),cL2,10);
        _ltoa(cmThing[lCount].Getl3(),cL3,10);
       CopyRect(&rcTest,cmThing[lCount].GetRC());
       _ltoa(rcTest.left,cRL,10);
       _ltoa(rcTest.top,cRT,10);
       _ltoa(rcTest.right,cRR,10);
       _ltoa(rcTest.bottom,cRB,10);
       _ltoa(lCount,cCount,10);
       strcat(cCat," = ");
       strcat(cCat,cCount);
       strcat(cCat,"\n");
       strcat(cCat,"Longs:");
       strcat(cCat,"\n");
       strcat(cCat,cL1);
       strcat(cCat,"\n");
       strcat(cCat,cL2);
       strcat(cCat,"\n");
       strcat(cCat,cL3);
       strcat(cCat,"\n");
       strcat(cCat,"RECTs");
       strcat(cCat,"\n");
       strcat(cCat,cRL);
       strcat(cCat,"\n");
       strcat(cCat,cRT);
       strcat(cCat,"\n");
       strcat(cCat,cRR);
       strcat(cCat,"\n");
       strcat(cCat,cRB);
       MessageBox(NULL,cCat,"Debug",NULL);
      }
    }
    Everything works fine until I run DisplayData(). It goes through the for loop fine until it get to (lCount==4) and then I get an invalid page fault error.

    This doesn't happen if I don't populate the variables with either FillData() or ReadData(). It runs fine and just gives me a MessageBox with a bunch of 0's for my values.

    My mistake is most likely then in my FillData() function, but I just don't see it.

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: illegal operation

    Originally posted by jdinger
    My mistake is most likely then in my FillData() function, but I just don't see it.
    I went back and tried using TextOut to display the values (instead of using strcat to get a large string for a message box) and everything seems to be working.

    I'm sure the problem is obvious, just don't see it. Anyone see what I'm doing wrong?

  3. #3
    Alipha
    Guest
    you made cCat only 10 chars long, but you are storing A LOT more than 10 characters in it, with all your strcats. You are adding at least 150 characters onto the end of cCat (I didn't bother to count exactly).

    You need to have cCat large enough to hold the string AND all the strings that you "cat" onto the end of it!

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Thanks, Alipha! I feel like an idiot. I misunderstood the way that
    strcat worked (that's what I get for not checking MSDN). I
    thought that it dynamically increased the size of the char array
    that was originally concatenated upon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. illegal operation error
    By xlordt in forum C Programming
    Replies: 2
    Last Post: 09-01-2003, 08:53 AM
  4. Illegal Operation
    By Bert in forum C++ Programming
    Replies: 15
    Last Post: 07-22-2002, 11:29 AM
  5. Illegal operation
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 04:33 PM

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