Cin command and pointers

Pretty simple question but it troubles me:

So I have this code below in where 'char Example[10]' can contain 10 characters. But if I use cin command and pointer to store the input and then print it out, the program doesnt really care if the input is more than 10 characters long, it stores and prints all the extra characters too (unless it has spaces).

I understand that because it uses a pointer so it then just goes to that address and stores everything without caring the maximum amount of characters.

The question is: Where it actually stores the extra data and can this fuck up or overwrite some data which is preserved to addresses following the actual address?

(Even if this might not be anyhow right-kind of coding, Im still interested to hearing the answer)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstring>

using namespace std;

char Example[10];

int main ()
{
  cin >> Example;
  cout << Example;
  return 0;
}
Last edited on
Hello and welcome to this forum :)

I just compiled this on both cpp.sh and visual studio 2013/2015 And it did not store more than 10 characters. I entered around 20, it cut them off and only stored 10, then it printed out only 10.
Thanks,

Im using codeblocks (+mingw) and Windows command prompt to test it.

If I input f.ex. 1234567890123456

it also prints out 1234567890123456.
use <string> its better
@Zarman that has nothing to do with this. He is clearly just asking a question about a specific behaviour.
So I have this code below in where 'char Example[10]' can contain 10 characters. But if I use cin command and pointer to store the input and then print it out, the program doesnt really care if the input is more than 10 characters long, it stores and prints all the extra characters too (unless it has spaces).

The overload for operator<< for this particular case is on char*. There is no size information available to it, so there is no way for it to respect the size of the array.

I understand that because it uses a pointer so it then just goes to that address and stores everything without caring the maximum amount of characters.

The question is: Where it actually stores the extra data and can this fuck up or overwrite some data which is preserved to addresses following the actual address?

The answer is: It results in undefined behavior. Don't use arrays without limiting the input extraction to a size that doesn't end up overrunning the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

char Example[10];

int main()
{
    cin >> std::setw(sizeof(Example)-1) >> Example;
    cout << Example;
    return 0;
}


Or use std::string as Zarman suggested.

TarikNeaj wrote:
@Zarman that has nothing to do with this. He is clearly just asking a question about a specific behaviour.

Any chance you can lay off being the post police when it doesn't add anything but noise to the thread?




Last edited on
Thanks Cire. That made it pretty much clear.

Edit: 20.1.2016:

I add this link here because it explains it too pretty well:
http://stackoverflow.com/questions/15642820/why-does-this-work-using-cin-to-read-to-a-char-array-smaller-than-given-input
Last edited on
Topic archived. No new replies allowed.