运行C ++代码时出现分段错误SIGSEGV

时间:2020-01-22 17:06:31

标签: c++ segmentation-fault

执行C ++代码时出现错误

错误是: 下等人停止了,因为它收到了来自操作系统的信号。

信号名称:SIGSEGV 信号含义:分段错误

IDE:QT创建者

我知道在头文件中声明结构的2维数组的方式存在问题 但是不知道到底是什么!

有人可以解释一下吗。.

请注意,我计划以递归或迭代的方式创建Datamatrix类的多个实例作为我的工作的一部分。 每个实例都包含二维结构数组。 因此,欢迎任何帮助说明。 我还没有探索C ++的许多领域!还在学习:)

请参见下面的代码

main.cpp ----------

#include <iostream>
#include "datamatrix.h"
#include "utils.h"

using namespace std;

void printArray(int items[],int size)
{
    for(int j=0;j<size;j++)
    {
       cout<<items[j]<<" ";
    }
    cout<<endl;
}

int main()
{
    cout << "Hello there" << endl;

    int a[250];
    for(int i=0;i<250;i++)
    {
        a[i] = i+1;
    }
    printArray(a,250);

    DataMatrix *matrix = new DataMatrix();

    Utils u;
    u.myFunction(matrix); **<-- All goes well till here**
    cout<<"End of Main method"; **<-- This line of code is never reached**

    return 0;

}

datamatrix.h ------------

#ifndef DATAMATRIX_H
#define DATAMATRIX_H
#include <string>
using namespace std;
struct KVPair { **<--- This is the line where issue comes**
    string key;
    double value;
};
class DataMatrix
{
public:
    DataMatrix();
    KVPair matrix[250][130];
};

#endif // DATAMATRIX_H

datamatrix.cpp --------------

#include "datamatrix.h"

DataMatrix::DataMatrix()
{
    for(int i=0;i<250;i++)
    {
        for(int j=0;j<130;j++)
        {
            string k = "f"+i+1;
            matrix[i][j].key = k;
            matrix[i][j].value = i+5;
        }

    }

}

utils.h --------

#ifndef UTILS_H
#define UTILS_H

#include "datamatrix.h"

class Utils
{
public:
    Utils();
    DataMatrix myFunction(DataMatrix* dm);
};

#endif // UTILS_H

utils.cpp ---------

#include "utils.h"
#include <algorithm>
#include <time.h>
#include <math.h>
#include "datamatrix.h"

Utils::Utils()
{

}

DataMatrix Utils::myFunction(DataMatrix* dm)
{
    printf("inside myFunction ");
}

只是想添加控制台输出(请参见下面的控制台输出)

你好 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5 7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168169170171172173174175175176177178179180180181182183184185186187 188 189 190 191 192 193 194 194 195 197 197 198 199 200 200 202 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 247 248249250 在myFunction内部

预先感谢

2 个答案:

答案 0 :(得分:1)

以下行:

string k = "f"+i+1;

导致问题。它使地址远远超出了字符串文字"f"(这是一个无效地址),然后将其传递给std::string构造函数,该构造函数从该地址读取以确定字符串的长度并导致崩溃。 / p>


一种解决方法是:

string k = "f" + std::to_string(i + 1);

答案 1 :(得分:0)

消除来自gcc -Wall的一个警告可以防止崩溃。一种方法是在Utils::myFunction中添加一个返回值,如下所示。

DataMatrix Utils::myFunction(DataMatrix* dm)
{   
    printf("inside myFunction ");
    return *dm;
}

似乎main在堆栈上为DataMatrix分配了空间,并期望对Utils::myFunction的调用将填充该空间。以下gdb会话摘录的二进制文件中,在return中添加了Utils::myFunction语句,从而增强了这一主张。

(gdb) break main.cpp:30
Breakpoint 1 at 0xfaa: file main.cpp, line 30.
(gdb) run > /dev/null
Starting program: main > /dev/null

Breakpoint 1, main () at main.cpp:30
30      u.myFunction(matrix);
(gdb) print matrix
$1 = (DataMatrix *) 0x7ffff7e95010
(gdb) set $matrix = (DataMatrix *) ($rbp - 0x13d640)
(gdb) print matrix.matrix[249][129].key
$2 = "\030J\017\vw\b\200"
(gdb) print $matrix.matrix[249][129].key
$3 = <incomplete sequence \303>
(gdb) next
31      cout<<"End of Main method";
(gdb) print $matrix.matrix[249][129].key
$4 = "\030J\017\vw\b\200"
(gdb) 

在我的测试中,崩溃发生在尝试取消分配由任意堆栈内容组成的未初始化KVPair的过程中。其详细信息未在之前的gdb会话摘录中透露,但要点是,在Utils::myFunction返回除void之外的值之后,DataMatrix的两个副本同意最后一个数组元素的内容(在我的测试中,第一个数组元素在析构函数中处理)。以下摘录自gdb的摘录解释了$rbp使用的$matrix偏移量。

   0x0000555555554fc7 <+183>:       lea    -0x13d640(%rbp),%rax
   0x0000555555554fce <+190>:       mov    %rax,%rdi
   0x0000555555554fd1 <+193>:       callq  0x555555555092 <DataMatrix::~DataMatrix()>

另一种防止崩溃的方法是将Utils::myFunction声明为返回void,在这种情况下,main不会在堆栈上分配空间,也不会调用{{ 1}}在DataMatrix中的析构函数。