C_String buffer overflow: array boundaries with cout

Dear C++ users,

I am currently learning C++ and need some precision about the cin/cout extract/insert operators and buffer overflow.

Here is a small program to illustrate my questions (I know the code below is not good programming style):

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

int main()
{
   using namespace std;
  
   char str[5] // C_Style string to hold 5 char including null.

   cout << "Please enter a string: ";
   cin >> str;

   cout << "You have entered: " << str;

   return 0;
}


Question 1:
My understanding is that if I enter abcdefg when prompted, I will provoke a buffer overflow and write over the array's boundary.
In the event that the program doesn't crash (as it happened on my computer), the output produced by cout gives abcdefg.
Shouldn't it be just abcde or only the 5 characters the array can hold?

Why do I get all the characters entered and not what can be hold by the array (only 5 characters)? (is it because of the absence of the null at the end?)

Question 2:
Could you please confirm my understanding below?
If I apply the extract (>>) operator on cin and the destination is a char array, all the characters found before the first blank will be sent to the array without checking boundaries. This is because the array is represented by a pointer and that pointer has no information about the size of the array.
Am I right?


Many, many thanks in advance for your patience, time and explanation!

kind regards
C++ doesn't provide bounds-checking, which effectively means the compiler will allow you to write beyond the buffer. With a buffer of 5 characters long, it'll fill up relatively quickly.

Question 2:
More or less, yeah. std::istream will continue to extract data and place it within the specified location until white-space is encountered.

Consider getline()[1].

References:
[1] http://www.cplusplus.com/reference/iostream/istream/getline/


Wazzak
Last edited on
My understanding is that if I enter abcdefg when prompted, I will provoke a buffer overflow and write over the array's boundary.


Correct.

In the event that the program doesn't crash (as it happened on my computer), the output produced by cout gives abcdefg.


It's likely, but there is no guarantee. It will display at least 'abcde' but may display more. It may even display additional garbage after the 'g'.

Shouldn't it be just abcde or only the 5 characters the array can hold?


Sort of. Memory is linear. What's happening is when you create this array is the compiler allocates 5 bytes in memory. If you write outside that array, you are still writing to memory, so the information is still stored... it's just being stored to memory that isn't allocated by the array (which means it either the memory it's writing to isn't allocated at all, or it's allocated by some other variable)

cin/cout don't have any knowledge of which memory is allocated and which isn't, so they just blindly keep reading/writing character until they reach the end of string data (determined by the null character).


If I apply the extract (>>) operator on cin and the destination is a char array, all the characters found before the first blank will be sent to the array without checking boundaries. This is because the array is represented by a pointer and that pointer has no information about the size of the array.
Am I right?


Yes, that is correct.
Many thanks to both of you for having answered so quickly!

Things are now clearer!

Thanks again!
Topic archived. No new replies allowed.