首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++错误:“未分配被释放的指针”

C++错误:“未分配被释放的指针”
EN

Stack Overflow用户
提问于 2022-06-05 22:51:35
回答 2查看 1.3K关注 0票数 0

我从不在代码中调用free,所以我不知道为什么会出现这个错误。

我编写了一个程序来查找一个数字的整数分区。效果很好。然后,我决定尝试使用指向向量的指针,以避免不必要地在循环迭代之间复制我的std::vector<ints>。它编译(使用g++ -std=c++11 -o main main.cpp)。当我执行程序时,我得到以下错误消息:

代码语言:javascript
运行
复制
main(5629,0x115dcfe00) malloc: *** error for object 0x7ffed1c05a30: pointer being freed was not allocated
main(5629,0x115dcfe00) malloc: *** set a breakpoint in malloc_error_break to debug

我为什么要犯这个错误?提前谢谢你。

这是我的密码:

代码语言:javascript
运行
复制
#include <stdexcept>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using std::cout;            using std::endl;
using std::vector;          

typedef vector<int> ints;

void print_ints(vector<int>);
void print_ints_vec(vector<vector<int>>);
void int_part(int, vector<vector<int>>&);

int main() 
{
    vector<vector<int>> partition;
    int_part(5, partition);
    print_ints_vec(partition);

    return 0;
}

void int_part(int sum, vector<vector<int>>& res)
{
    vector<int> init_xs = vector<int>{sum};
    vector<int>* xs = &init_xs; // POINTER INITIALIZED TO vector<int>
    int current_sum = sum;

    while (true) 
    {
        current_sum = accumulate(xs->begin(), xs->end(), 0); //

        if (current_sum == sum)
        {
            res.push_back(*xs); // 
            vector<int> next_xs;
            vector<int>::iterator it = find(xs->begin(), xs->end(), 1); //
            if (it == xs->begin()) return; //
            copy(xs->begin(), it, back_inserter(next_xs)); //
            next_xs[next_xs.size() - 1] -= 1; //
            xs = &next_xs; // POINTER REASSIGNED TO ANOTHER vector<int>
        }
        else 
        {
            int tail = xs->back(); //
            int diff = sum - current_sum;
            int m = std::min(tail, sum - tail);
            int next_tail = current_sum + m > sum ? diff : m;
            xs->push_back(next_tail); //
        }
    }
}

void print_ints(ints v) // PRINT UTILITY
{
    cout << "[ ";
    for (const int& n : v) { cout << n << "; "; }
    cout << "]" << endl;
}

void print_ints_vec(vector<ints> v) // PRINT UTILITY
{
    cout << "[ \n";
    for (const vector<int>& xs : v) { cout << "  "; print_ints(xs); }
    cout << "]" << endl;
}
EN

Stack Overflow用户

发布于 2022-06-05 22:58:40

代码语言:javascript
运行
复制
xs = &next_xs;

是不好的,因为next_xs是一个局部变量,其生存期在作用域结束时结束。在作用域结束后,在指定指向有效对象的指针之前,取消引用xs是非法的。

在这种情况下,看起来应该直接修改向量以避免复制。

代码语言:javascript
运行
复制
void int_part(int sum, vector<vector<int>>& res)
{
    vector<int> xs = vector<int>{sum};
    int current_sum = sum;

    while (true) 
    {
        current_sum = accumulate(xs.begin(), xs.end(), 0); //

        if (current_sum == sum)
        {
            res.push_back(xs); // 
            vector<int> next_xs;
            vector<int>::iterator it = find(xs.begin(), xs.end(), 1); //
            if (it == xs.begin()) return; //
            xs.erase(it, xs.end());
            xs[xs.size() - 1] -= 1; //
        }
        else 
        {
            int tail = xs.back(); //
            int diff = sum - current_sum;
            int m = std::min(tail, sum - tail);
            int next_tail = current_sum + m > sum ? diff : m;
            xs.push_back(next_tail); //
        }
    }
}
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72511624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档