You are not logged in.

#1 2014-06-24 09:21:48

grimx
Member
Registered: 2010-07-31
Posts: 27

c++ issue

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

#2 2014-06-24 10:58:42

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,503
Website

Re: c++ issue

Maybe don't use an array of size six to read in text of an undefined length.

Offline

#3 2014-06-24 11:27:56

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: c++ issue

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

#4 2014-06-25 05:11:47

grimx
Member
Registered: 2010-07-31
Posts: 27

Re: c++ issue

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

#5 2014-06-25 15:11:18

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,346

Re: c++ issue

grimx wrote:

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

Board footer

Powered by FluxBB