简体   繁体   中英

Able to Access Elements with Index Greater than Array Length

The following code seems to be running when it shouldn't. In this example:

#include <iostream>
using namespace std;
int main()
{
    char data[1];
    cout<<"Enter data: ";
    cin>>data;
    cout<<data[2]<<endl;
}

Entering a string with a length greater than 1 (eg, "Hello"), will produce output as if the array were large enough to hold it (eg, "l"). Should this not be throwing an error when it tried to store a value that was longer than the array or when it tried to retrieve a value with an index greater than the array length?

The following code seems to be running when it shouldn't .

It is not about "should" or "shouldn't" . It is about "may" or "may not" .

That is, your program may run, or it may not.

It is because your program invokes undefined behavior . Accessing an array element beyond the array-length invokes undefined behavior which means anything could happen.

The proper way to write your code is to use std::string as:

#include <iostream>
#include <string>

//using namespace std;  DONT WRITE THIS HERE

int main()
{
    std::string data;
    std::cout<<"Enter data: ";

    std::cin>>data; //read the entire input string, no matter how long it is!

    std::cout<<data<<std::endl; //print the entire string

    if ( data.size() > 2 ) //check if data has atleast 3 characters
    {
         std::cout << data[2] << std::endl; //print 3rd character 
    }
}

在编译或在其他机器上编译时,它可能在不同参数下崩溃,因为根据文档,该代码的运行给出未定义的结果。

It is not safe to be doing this. What it is doing is writing over the memory that happens to lie after the buffer. Afterwards, it is then reading it back out to you.

This is only working because your cin and cout operations don't say: This is a pointer to one char, I will only write one char. Instead it says: enough space is allocated for me to write to. The cin and cout operations keep reading data until they hit the null terminator \\0 .

To fix this, you can replace this with:

std::string data;

C++ will let you make big memory mistakes.

Some 'rules' that will save you most of the time:

1: Don't use char[] . Instead use string .

2: Don't use pointers to pass or return argument. Pass by reference, return by value.

3: Don't use arrays (eg int[] ). Use vectors . You still have to check your own bounds.

With just those three you'll be writing some-what "safe" code and non-C-like code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM