简体   繁体   English

堆栈粉碎终止程序

[英]stack smashing terminates program

I am learning C++ and was given the task to create a program that allows user to modify an array with 10 integers in it.我正在学习 C++,并被赋予创建一个程序的任务,该程序允许用户修改其中包含 10 个整数的数组。 if user gives an index out of range program will exit.如果用户给出的索引超出范围,程序将退出。 Program works with negative numbers and all numbers with in the range.程序适用于负数和范围内的所有数字。 when I enter a number like 10 which is above the range I get:当我输入一个超过范围的数字时,例如 10:

* stack smashing detected * : terminated *检测到堆栈粉碎* :终止

I am new to this and any help will be much appreciated.我对此很陌生,任何帮助将不胜感激。

#include <iostream>
#include <array>
using namespace std;

int main()
{
    array<int, 10> myData; // creates array size 10
    int i = 0;
    int v = 0;

    for (unsigned int n = 0; n < myData.size(); n++) // makes all elements 1
    {
        myData[n] = 1;
    }

    do
    {
        for (unsigned int a = 0; a < myData.size(); a++)
        {
            cout << myData[a] << " ";
        }
        cout << endl << "Input index: ";
        cin >> i;
        cout << endl << "Input value: ";
        cin >> v;
        myData[i] = v;
    } while (i >= 0 && i < myData.size());
    {
        cout << endl << "Index out of range: Exit " << endl;
    }
    return 0;
}

When I run the program, I get this:当我运行程序时,我得到了这个:

1 1 1 1 1 1 1 1 1 1
Input index: 10

Input value: 4

Index out of range: Exit
*** stack smashing detected ***: <unknown> terminated
[1]    56 abort (core dumped)  ./edit

You are accessing memory which is not part of your array hence that error message.您正在访问不属于您的数组的内存,因此该错误消息。 You should first validate the index before assigning the value using subscript operator [].在使用下标运算符 [] 分配值之前,您应该首先验证索引。

Here's your code snippet ( commented ) that caused the problem:这是导致问题的代码片段(已注释):

cin >> v;
myData[i] = v; // Direct assignment without validating i
               // i needs to be validated before this assignment

There are a number of things that I'd like to point out:我想指出以下几点:

For initialization with the same value, you don't need a loop because std::array::fill() member function does exactly that.对于具有相同值的初始化,您不需要循环,因为std::array::fill()成员函数正是这样做的。

Example:例子:

std::array<int, 10> data;
data.fill( 1 );

You are using std::array that means you are at least using C++11.您正在使用std::array这意味着您至少在使用 C++11。 So, for array traversals, you can use C++11's range-for loop like this:因此,对于数组遍历,您可以像这样使用 C++11 的range-for循环:

for ( const auto& i : data )
{
    std::cout << i << ' ';
}

You might want to look at auto specifier if you're not already familiar with it.如果您还不熟悉auto 说明符,您可能想看看它。

I don't know your reasoning for using do-while loop here.我不知道你do-while这里使用do-while循环的原因。 You can use a simple while infinite loop ( for learning purposes ) breaking it on an invalid index input using if-else for validating index before assignment.您可以使用一个简单的while无限循环(用于学习目的)在无效索引输入上使用if-else在分配之前验证索引来打破它。

For example:例如:

while ( true )
{
    // Print array here...

    std::cin >> index;
    if ( /* index is out of bounds */ )
    {
        std::cerr << "ERROR: Out-of-range index!\n";
        break; // Exit from loop here on invalid index
    }
    else
    {
        std::cin >> value;
        data[ index ] = value;
    }
}

Please do take a look at std::array::at() member function which performs bounds checking and throws an exception on violation.请看一下std::array::at()成员函数,它执行边界检查并在违规时抛出异常。


I'm not sure what you're doing with this part because braces around std::cout are redundant here:我不确定你对这部分做了什么,因为std::cout周围的大括号在这里是多余的:

while(i >= 0  && i < myData.size());    // do-while ends here
{
  cout << endl <<"Index out of range: Exit "<< endl;
}

Maybe, you're confusing do-while with while loop.也许,您将do-whilewhile循环混淆了。


Please don't forget to format your code in future.请不要忘记将来格式化您的代码。 Use your IDE's code formatting features or you can use any online code formatting sites too (eg http://format.krzaq.cc/ ) while posting your code on SO.使用您的 IDE 的代码格式化功能,或者您也可以使用任何在线代码格式化站点(例如http://format.krzaq.cc/ ),同时在 SO 上发布您的代码。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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