【问题标题】:How can I locate error: *** Error in `./a.out': free(): invalid next size (fast): 0x09767150 ***如何定位错误:*** `./a.out' 中的错误:free():下一个尺寸无效(快速):0x09767150 ***
【发布时间】:2015-06-07 21:01:57
【问题描述】:

我的 main.cpp 和 grid.h 编译成功。当我在调整网格大小后使用函数填充时会出现问题。这在运行程序后会产生错误:

* `./a.out' 中的错误:free(): invalid next size (fast): 0x0871d150 *

main.cpp

#include "grid.h"
#include <cstdlib>

#if 1
    #define log(x) std::cout << x << std::endl;
#else
    #define log(x) 
#endif  

#ifdef _GLIBCXX_CSTDLIB
    #define clear system("clear");
    #define reset system("reset");
#else
    #define clear
    #define reset
#endif

std::pair<const int, const int> LARGE  = std::make_pair(8,8);
std::pair<const int, const int> MEDIUM = std::make_pair(6,6);
std::pair<const int, const int> SMALL  = std::make_pair(4,4); 


int main(){
    clear
    grid<char> a(LARGE,'#');
    grid<int>  b(4,5,   9 );
    a.resize(4,8);
    b.resize(MEDIUM);
    b.fill(8);
    log(a);
    log(b);
    return 0;
}

grid.h

#ifndef GRID_H
#define GRID_H

#include <iostream>
#include <vector>

template<typename type>
class grid{
    public://Functions
        grid(std::pair<const type, const type> dimension, type filler = 0) : rows(dimension.first), cols(dimension.second){
                matrix.assign(rows, std::vector<type>(cols, filler));
        };
        grid(const int _rows, const int _cols, type filler = 0) : rows(_rows), cols(_cols){
                matrix.assign(rows, std::vector<type>(cols, filler));
        };
        void resize(std::pair<const type, const type> dimension){
            rows = dimension.first, cols = dimension.second;
            matrix.resize(rows, std::vector<type>(cols));
        };
        void resize(const int _rows, const int _cols){
            rows = _rows, cols = _cols;
            matrix.resize(rows, std::vector<type>(cols));
        };
        void fill(type filler){
            for(int r = 0; r < rows; r++){
                for(int c = 0; c < cols; c++){
                    matrix[r][c] = filler;
                }
            }
        };
    public://Operators
        friend std::ostream& operator<<(std::ostream& out, grid& g){
            for(int r = 0; r < g.rows; r++){
                for(int c = 0; c < g.cols; c++){
                    out << g.matrix[r][c];
                }out << std::endl;
            }return out;
        };
        //Variables
        std::vector<std::vector<type>> matrix;
        int  rows;
        int  cols;
};

#endif//GRID_H

控制台输出

dylan@Aspire-one:~$ ./a.out
########
########
########
########

888888
888888
888888
888888
888888
888888

*** Error in `./a.out': free(): invalid next size (fast): 0x09767150 ***
Aborted (core dumped)
dylan@Aspire-one:~$ 

【问题讨论】:

  • 您可以使用像 valgrind 这样的内存调试工具来查看不平衡的free() 调用。
  • 你确定这是你的真实代码吗?一方面,######## 不应该存在(你只有一个log!),另外我无法重现崩溃。
  • 哦,我看不到任何错误。
  • 我更改了代码。这个编辑后的版本应该是我实际拥有的。我仍然遇到同样的错误。

标签: c++ runtime-error


【解决方案1】:

Error in './a.out': free(): invalid next size (fast): 0x0871d150

此错误总是意味着您的程序损坏了它的堆(例如通过写入超出堆分配缓冲区的末尾)。

找到这个问题的标准方法是在 Valgrind 下运行你的程序,它会直接指出问题所在。

Address sanitizer (gcc -fsanitize=address -g ...) 是另一种查找此问题的标准工具。

【讨论】:

  • 我可以运行地址清理程序。你能教我找什么吗?
  • 这充其量只是一条评论。此“答案”中没有任何内容可以确定问题。
【解决方案2】:

您的调整大小方法不正确。如果您已经有 N 行并将大小调整为 X 行,则任何现有行都不会更改为新的列大小。然后,当您使用它们时,您就失败了。

【讨论】:

  • 你能告诉我如何解决这个问题吗?
  • 我应该改用assign吗?
  • 如果您不关心性能或矩阵的旧内容,您可以先清除向量。然后调整大小将做你想要的。否则,您应该调整外部向量的大小,然后循环并一次调整一列的大小
  • 清除二维向量有效,但其他方法无效:void resize(const int _rows, const int _cols){ rows = _rows, cols = _cols; matrix.resize(rows); for(int i = 0; i &lt; rows; i++){ matrix[i].resize(cols); } };
  • 我错了。两种方法都有效。我忘了更改这两个调整大小功能。
猜你喜欢
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多