Jump to content

Has this ever happened to you?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Alright I've got a problem with that recent program I developed for Chili5's contest thing. It would seem that about 98% of the time, it runs flawlessly, but in the last 2% of the times and without any changes to the code, it throws an exception. Now, not a bad_allocation exception, no, bad_alloc I can get over, but an exception that should only occur with the overflow of a switch statement in my program. The source code is available on the other thread, so you can see it all, and below is what's going on in this comp:
zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
 --   --   --   --   
   | |  |    | |  | 
   | |  |    | |  | 
 --   --   --   --   
   |    | |    |  | 
   |    | |    |  | 
 --   --   --   --   

 ---   ---   ---   
|   |     |     | 
|   |     |     | 
|   |     |     | 
 ---   ---   ---   
    | |         | 
    | |         | 
    | |         | 
 ---   ---   ---   

 -----   -----   
|     |       | 
|     |       | 
|     |       | 
|     |       | 
|     |       | 
 -----   -----   
|     | |       
|     | |       
|     | |       
|     | |       
|     | |       
 -----   -----   

 ---------   ---------   ---------   
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
          | |         |           | 
 ---------   ---------   ---------   
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
|           |         |           | 
 ---------   ---------   ---------   

 -   -   -   -   
  |   | | |   | 
 -       -   -   
|     | | |   | 
 -       -   -   

zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers testfile
Exception thrown in NumberFactory: DigException
zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ 
So... what the heck? Why is it randomly just "deciding" to throw an exception it should never get in the first place (the code is below)? I thought computer programming was a deterministic process. Do any of you know how?

    Digit* NumberFactory::makeDigit(int dig) throw(std::bad_alloc)
    {
      try
      {
        switch (dig)
        {
            case 0:
                return new Zero();

            case 1:
                return new One();

            case 2:
                return new Two();

            case 3:
                return new Three();

            case 4:
                return new Four();

            case 5:
                return new Five();

            case 6:
                return new Six();

            case 7:
                return new Seven();

            case 8:
                return new Eight();

            case 9:
                return new Nine();

            default:
                throw DigException();
                return NULL;
        }
      }
      catch (DigException& e)
      {
        std::cout << "Exception thrown in NumberFactory: " << e.what() << std::endl;
        exit(EXIT_FAILURE);
      }
    }

////////////////////////////////////////STARTER.CPP////////////////////////////////
void Starter::run() throw(std::bad_alloc)
{
    std::auto_ptr< std::vector< Digit* > > digVect(new std::vector< Digit* >);
    for (unsigned int iii = 0; iii < numberSet.size(); ++iii)
    {
        digVect->push_back(numFact.makeDigit(numberSet.at(iii)));
    }
    std::auto_ptr< std::string > theString = prs.parse(digVect, size);
    std::cout << *theString << std::endl;
}
Starter::run() is the only place in the program that calls NumberFactory::makeDigit, so the natural question is where the numberSet vector gets it's numbers. Well, there are actually two different sources, but they're almost precisely the same, the difference being dependent wholly on whether the user is issuing a command-line argument version or a cin version:
    int anum = getLength(argv[2]);
    // You can safely assume getLength always returns the correct length.
    for (int iii = 0; iii < anum; ++iii)
    {
        char* temp = new char(argv[2][iii]);
        numberSet.push_back(atoi( (const char*) temp) );
        delete temp;
    }
So I think that may be the source of all my pain, but who knows. Anyway, I'd love to hear any ideas, and if I'm doing everything terribly or not. Alright, I'm getting some sleep. :P
Wow I changed my sig!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
This bothers me: argv[2][iii]
Don't you mean: argv[2+iii]?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
No, argv[2][iii] points to a particular character in the argv[2] character array, which is what I want, whereas argv[2+iii] would return a pointer to the entire string of supposed future arguments, of which there are none (since there were only two arguments passed, it would have an out of bounds error).

I use argv[2][iii] since I want to get each individual character from the argument rather than the whole argument. Besides, this problem occurs in both situations, not just using that particular loop. The other loop looks like so:
        for (int jjj = 0; jjj < len; ++jjj)
        {
            char* temp = new char(commandSet.at(iii)->second[jjj]);
            numberSet.push_back(atoi( (const char*) temp) );
            delete temp;
        }
I just didn't want to jump through more hoops to show everyone how commandSet works since the code's available for review. :P

Though thank you for responding, I'm going to try sending back the dig variable in my exception so I know what's going on. :)
Wow I changed my sig!

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Are you sure temp is a null-terminated string acceptable for use with atoi?

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
EDIT: @dcs: Man... I JUST wrote a huge post showing that what you summed up in one line was my exact problem. Now how do you think I look?

Alright, I think I fixed it, and I learned something new. Try really, really hard to make sure all of your values aren't trash values.

Anyway, the source of my pain was indeed the for loop I had expected.
    for (int iii = 0; iii < anum; ++iii)
    {
        char* temp = new char(argv[2][iii]);
        numberSet.push_back(atoi( (const char*) temp) );
        delete temp;
    }
But for a completely different reason. It involves the fact that I'm using a character pointer, which when it is interpreted by atoi, will assume that a character pointer is to a string. In order for me to use this properly, I SHOULD have reserved two chars, not one, and always filled the last character with the string delimiter, like so:
    for (int iii = 0; iii < anum; ++iii)
    {
        char* temp = new char[2];
        temp[0] = argv[2][iii];
        temp[1] = '\0';
        numberSet.push_back( atoi(temp) );
        delete[] temp;
    }
I found this out by placing in some debug information into each of these loops, simply using std::cout to print out the values after every instruction to find out what was happening. Everything was functioning properly, I'm just an amateur. :P

When atoi received the character pointer (string), it would interpret all the way up the first non-numerical character. Usually this did work, as seen below.
Sometimes, the next character in memory just happened to be a delimiter character ('\0'):
zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers 2 3948
3 3 3
9 9 9
4 4 4
8 8 8
3 9 4 8 
 --   --        --   
   | |  | |  | |  | 
   | |  | |  | |  | 
 --   --   --   --   
   |    |    | |  | 
   |    |    | |  | 
 --   --        --   
Sometimes it was a trash character and wasn't interpreted by atoi anyway (this was usually the case):
zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers 2 3948
3 3 3
9 9 9
4 4� 4
8 8� 8
3 9 4 8 
 --   --        --   
   | |  | |  | |  | 
   | |  | |  | |  | 
 --   --   --   --   
   |    |    | |  | 
   |    |    | |  | 
 --   --        --   
But sometimes, the next character in memory after the allocation also happened to be a numerical character!
zekedragon@zeke-comp:~/Projects/C++/LCNumbers$ bin/Debug/LCNumbers 2 3948
3 3 3
9 9 9
4 40�	 40
8 80�	 80
3 9 4 8 
 --   --        --   
   | |  | |  | |  | 
   | |  | |  | |  | 
 --   --   --   --   
   |    |    | |  | 
   |    |    | |  | 
 --   --        --   
And that's when the bug would happen and throw a DigException. Now that I've straightened this out, the program works great without any issues that I can see.

I am DONE dealing with this program, but I did learn something new, and I hope that other people will take this problem to heart and remember to shield your character pointer allocations! Alright, on to the next program.

Edited by ZekeDragon, 06 September 2009 - 07:09 PM.
See EDIT

Wow I changed my sig!

#6
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

dcs said:

Are you sure temp is a null-terminated string acceptable for use with atoi?

yes null terminated string can be used..

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

ZekeDragon said:

EDIT: @dcs: Man... I JUST wrote a huge post showing that what you summed up in one line was my exact problem. Now how do you think I look?
Heh. That was an interesting bit of near-simultaneous posting. I'm sure your version will be more beneficial to others in the future.