MS Visual C++ bug?

Discussion in 'C++' started by Kevin Stern, Aug 19, 2003.

  1. Kevin Stern

    Kevin Stern Guest

    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?
    Kevin Stern, Aug 19, 2003
    #1
    1. Advertising

  2. "Kevin Stern" <> wrote in message
    news:...
    > 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?


    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
    John Harrison, Aug 19, 2003
    #2
    1. Advertising

  3. Kevin Stern wrote:
    >
    > 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>


    #include <iostream>
    using namespace std;

    > struct blah {
    > char a[2];
    > };
    >
    > void main() {


    int main() {

    > blah* b;
    > b = new blah();
    > delete b;
    > b = new blah();
    > cin >> b -> a;


    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);

    > b -> a[1] = '\0';


    This line won't be necessary if you use getline.

    > delete b;
    >
    > }
    >
    > Any ideas?


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

    Regards,

    Russell Hanneken
    Russell Hanneken, Aug 19, 2003
    #3
  4. Russell Hanneken wrote:
    >
    > Have you considered using std::string instead of a character array?


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

    --
    Russell Hanneken
    Russell Hanneken, Aug 19, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Timbers
    Replies:
    32
    Views:
    1,372
    Alvin Bruney
    Nov 8, 2003
  2. wh
    Replies:
    2
    Views:
    477
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  3. Rob Meade
    Replies:
    2
    Views:
    362
    Rob Meade
    May 22, 2004
  4. M P
    Replies:
    6
    Views:
    533
    Joe Fallon
    Aug 7, 2004
  5. Ily
    Replies:
    2
    Views:
    936
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=
    Aug 3, 2005
Loading...

Share This Page