By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

MS Visual C++ bug?

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Kevin Stern" <K-*****@neiu.edu> wrote in message
news:ca*************************@posting.google.co m...
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
Jul 19 '05 #2

P: n/a
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
rh*******@pobox.com

Jul 19 '05 #3

P: n/a
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
rh*******@pobox.com

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.