You are not logged in.
Pages: 1
Here is my source code
test.cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int SIZE = 6;
char name1[SIZE] = {0};
char name2[SIZE] = {0};
cout << "SIZE = " << SIZE << endl;
cout << endl;
cout << "sizeof(name1) before: " << sizeof(name1) << endl;
cout << "strlen(name1) before: " << strlen(name1) << endl;
cout << "Enter your first name: ";
cin >> name1;
cout << "Your first name is " << name1 << endl;
cout << "sizeof(name1) after: " << sizeof(name1) << endl;
cout << "strlen(name1) after: " << strlen(name1) << endl;
cout << endl;
cout << "sizeof(name2) before: " << sizeof(name2) << endl;
cout << "strlen(name2) before: " << strlen(name2) << endl;
cout << "Enter your last name: ";
cin >> name2;
cout << "Your last name is " << name2 << endl;
cout << "sizeof(name2) before: " << sizeof(name2) << endl;
cout << "strlen(name2) after: " << strlen(name2) << endl;
return 0;
}
Compiled using g++ test.cpp -o test
Now how can this run without hogging the next cin or crashing??
SIZE = 6
sizeof(name1) before: 6
strlen(name1) before: 0
Enter your first name: SuperMan
Your first name is SuperMan
sizeof(name1) after: 6
strlen(name1) after: 8
sizeof(name2) before: 6
strlen(name2) before: 0
Enter your last name: SpiderMan
Your last name is SpiderMan
sizeof(name2) before: 6
strlen(name2) after: 9
Last edited by grimx (2014-06-24 09:27:07)
Arch 64Bit
x86_64 AMD Sempron(tm) Processor LE-1300
2 GIGS RAM, 500 GIG Hard Drive
NVIDIA GeForce 6160 SE integrated graphics
Offline
Maybe don't use an array of size six to read in text of an undefined length.
Offline
or you read only 6 characters from stdin.
To circumvent this you could apply the STL <string> library. This manages the whole memory useage:
std::string foo ;
std::cin >> foo ;
If you prefere char arrays, you should use a C functions like scanf() or fgets() to read your terminal input. The latter enables a length limited read-out of characters from FILE * stream, which could be e.g. stdin.
I hope there is no mistake in here.
Offline
I was just seeing what would happen. I'm amazed it ran without any error or crash.
Last edited by grimx (2014-06-25 05:16:45)
Arch 64Bit
x86_64 AMD Sempron(tm) Processor LE-1300
2 GIGS RAM, 500 GIG Hard Drive
NVIDIA GeForce 6160 SE integrated graphics
Offline
Now how can this run without hogging the next cin or crashing??
You got lucky. For fun, go back and change your code to print out name1 after you overrun name2. I'll bet it gets trashed.
For additional fun, create a function that puts some variable on the stack. Call that function after you enter name1, but before you print it. Ill'bet that trashes name1
in this program, name1 and name2 are allocated on the stack. Name1 is probably at the end of your stack frame and you overflowed beyond your frame. I am pretty sure that name1 is between name2 and the end of the stack frame, so overrunning name2's space should trash name1. The function call I proposed puts stuff on the stack at the end of your stack frame overwriting your name1 overrun. When that function returns, the part of name1 that was in that frame of the called function is gone.
This is a classic buffer overrun.
Last edited by ewaller (2014-06-25 15:12:58)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Pages: 1