MS Visual C++ bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Stern

    MS Visual C++ bug?

    Hi All,

    If I type in more than 1 character in the cin portion, the delete
    causes the following:

    Debug Error!
    Program: test.exe
    DAMAGE: after Normal block (#54) at 0x002F0930.

    Take a look at the following:

    #include <iostream.h>

    struct blah {
    char a[2];
    };

    void main() {
    blah* b;
    b = new blah();
    delete b;
    b = new blah();
    cin >> b -> a;
    b -> a[1] = '\0';
    delete b;

    }

    Any ideas?
  • John Harrison

    #2
    Re: MS Visual C++ bug?


    "Kevin Stern" <K-Stern@neiu.edu> wrote in message
    news:ca77e32d.0 308191228.5970c 9c@posting.goog le.com...[color=blue]
    > Hi All,
    >
    > If I type in more than 1 character in the cin portion, the delete
    > causes the following:
    >
    > Debug Error!
    > Program: test.exe
    > DAMAGE: after Normal block (#54) at 0x002F0930.
    >
    > Take a look at the following:
    >
    > #include <iostream.h>
    >
    > struct blah {
    > char a[2];
    > };
    >
    > void main() {
    > blah* b;
    > b = new blah();
    > delete b;
    > b = new blah();
    > cin >> b -> a;
    > b -> a[1] = '\0';
    > delete b;
    >
    > }
    >
    > Any ideas?[/color]

    Not a bug, just an array overflow. If you type two characters into cin, then
    three characters will get stored in your array, the two you typed plus the
    null terminator. Since your array only has room for two characters this
    causes the problem.

    john


    Comment

    • Russell Hanneken

      #3
      Re: MS Visual C++ bug?

      Kevin Stern wrote:[color=blue]
      >
      > If I type in more than 1 character in the cin portion, the delete
      > causes the following:
      >
      > Debug Error!
      > Program: test.exe
      > DAMAGE: after Normal block (#54) at 0x002F0930.
      >
      > Take a look at the following:
      >
      > #include <iostream.h>[/color]

      #include <iostream>
      using namespace std;
      [color=blue]
      > struct blah {
      > char a[2];
      > };
      >
      > void main() {[/color]

      int main() {
      [color=blue]
      > blah* b;
      > b = new blah();
      > delete b;
      > b = new blah();
      > cin >> b -> a;[/color]

      This has the effect of calling

      operator>> (cin, b->a);

      b->a is an array of 2 chars, but when you pass an array as an argument
      to a function, the function actually just gets a pointer to the first
      element. So operator>> just gets a char *, and has no way of knowing
      how large the buffer is. So when you type more than one character,
      characters are written outside the bounds of the array. (The version of
      operator>> that writes to a character array will always write the '\0'
      at the end.) This invokes undefined behavior.

      Try this:

      cin.getline(b->a, 2);
      [color=blue]
      > b -> a[1] = '\0';[/color]

      This line won't be necessary if you use getline.
      [color=blue]
      > delete b;
      >
      > }
      >
      > Any ideas?[/color]

      Have you considered using std::string instead of a character array?

      Regards,

      Russell Hanneken
      rhanneken@pobox .com

      Comment

      • Russell Hanneken

        #4
        Re: MS Visual C++ bug?

        Russell Hanneken wrote:[color=blue]
        >
        > Have you considered using std::string instead of a character array?[/color]

        Or, for that matter, a char; you seem to be interested in only one
        character.

        --
        Russell Hanneken
        rhanneken@pobox .com

        Comment

        Working...