【问题标题】:Error code C6262 consider moving data to heap错误代码 C6262 考虑将数据移动到堆
【发布时间】:2020-10-21 03:34:51
【问题描述】:

此代码 C6262 警告不断出现,导致程序出现问题,我尝试寻找可能的解决方案,但我很难理解。如果有人能帮我解决这个问题,如果你能指出我可以改进的代码中的任何错误部分,我将不胜感激。

#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;

class sorting {
private:
int size, elements;
int arr[5000], x;


public:
void sort() {
    cout << "Enter number of desired elements for the 1st set" << ">"; cin >> elements;
    arr[elements];
    half(); cout << endl;
    bubble();

    
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";

    }

    

}
void half() {
    for (int i = 0; i < elements / 2; i++) {
        arr[i] = i + 1;
    }
    for (int i = elements / 2; i < elements; i++) {
        arr[i] = rand();
    }
    cout << "This is the elements of the 1st set: ";
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";
    }
}
void random() {
    for (int i = 0; i < elements; i++) {
        arr[i] = i + 1;

    }

    random_shuffle(&arr[0], &arr[elements]);

    cout << "This is the elements of the 2nd set: ";

    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";

    }

}
void ascend_descend() {
    int x = elements / 2;
    arr[0] = x;
    for (int i = 0; i < elements / 2; i++) {
        arr[i + 1] = x - 1;
        x--;
    }
    for (int i = elements / 2; i < elements; i++) {
        arr[i] = i + 1;
    }
    cout << "This is the elements of the 3rd set: ";
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";
    }



};
void bubble() {
    for (int i = 0; i < elements; i++) {
        int temp;
        for (int j = i + 1; j < elements; i++) {
            if (arr[j] < arr[i]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }

        }

    };
}
  };

int main()
{   
    sorting sortObject;

    sortObject.sort();
    return 0;
}

【问题讨论】:

  • This warning indicates that stack usage that exceeds a preset threshold (constant_2) has been detected in a function. docs.microsoft.com/en-us/cpp/code-quality/c6262?view=vs-2019 考虑一个向量。此外,arr[elements]; 并没有按照您的想法执行,您的程序也不会按原样编译。
  • 您的代码格式还有很多不足之处。您不应在函数末尾的 } 之后放置分号。
  • 你认为arr[elements]; 行是做什么的?它不会调整数组的大小...它只返回对索引为“elements”的元素的引用...但您不对其进行任何操作,因此代码是多余的。

标签: c++


【解决方案1】:

在类声明的顶部有int arr[5000], x;,它将在堆栈上分配整数数组。您的编译器警告您该数组可能不适合堆栈,您应该在堆上分配它。有关堆栈与堆的解释,请参阅this。假设您仍想使用带有指针的 c 样式数组(请参阅我的第二个解决方案以了解更惯用的方法),您将在构造函数中分配数组,并在析构函数中释放它。

class sorting
{
int  size;
int  elements;
int* arr;
int  x;

public:
    sorting()
    {
        arr = new int[5000];
    }

    ~sorting()
    {
        delete arr;
    }
/*the rest of your code...*/
};

此解决方案更多地采用手动内存管理方式,除非您有充分的理由这样做,否则我建议您使用std::vector 而不是手动分配您的数组。这将跟踪元素和大小。因此,您的成员变量将如下所示:

class sorting
{
private:
    int x;
    int elements;
    vector<int> arr;
}

然后您可以使用arr.push_back(int) 向向量添加元素。 arr[i] 访问和设置它们,arr.size() 获取数组中有多少元素。

【讨论】:

  • 你的例子应该遵守三/五规则
  • @JHBonarius @Alan Birtles 我理解为什么unique_ptr 和 3/5 规则在这里很重要,但这会使实际问题复杂化,并且操作似乎是初学者。如果 op 不知道堆和堆栈之间的区别,并且他们的代码有类似 arr[elements]; 的行,那么谈论 3 规则,并且智能指针似乎是一个不好的起点,无论是否惯用。我宁愿他们学习用new在堆上分配内存是什么意思,然后再发现我们为什么使用智能指针和3的规则。
  • 对我来说,这种逻辑听起来像是“你需要先学会使用打字机才能开始使用文字处理器”,因此我不同意。您应该根据最新的见解进行教学。原始指针只是出于罕见的边缘情况和遗留原因而出现在该语言中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 2021-12-07
  • 2020-02-03
  • 2017-06-26
  • 2020-07-28
  • 2011-01-20
  • 1970-01-01
相关资源
最近更新 更多