【问题标题】:Another coredump issue in CC中的另一个核心转储问题
【发布时间】:2015-06-17 00:58:26
【问题描述】:

当我用
g++ -g -o prueba prueba.cpp -lstdc++ -O3 -march=corei7 -mtune=corei7 -std=c++0x 编译我的代码时

g++ -g prueba.cpp调试后,我得到了这些:

prueba.cpp:24:6: error: ‘stoi’ is not a member of ‘std’
tm = std::stoi(string2);
     ^
prueba.cpp:34:7: error: ‘stoi’ is not a member of ‘std’
ler = std::stoi(string1);
      ^
prueba.cpp:77:8: error: ‘stoi’ is not a member of ‘std’
C[i]=std::stoi(string);
     ^

我将stoi 声明为std::stoi 的方式是基于this example

而破碎的方块是:

//////////////////////////////////////////
if( B != NULL )
{
    for(i=0;i<div;i++)
    {
        a=B[i][0];
        b=B[i][1];
        std::cout<<a<<"\t"<<b<<"\n";
        A[b][a]=A[b][a]+1;
        A[a][b]=A[a][b]+1;
    }   
    free(B);
}
//////////////////////////////////////////

这给了我分段错误的问题。但我看不出问题出在哪里或在哪里。

文件是:

LERhttps://app.box.com/s/oi52zw7j8w19txr4cau2pf4w3pzmcelm

RELhttps://app.box.com/s/bo2xwm2hviucx4jzarxv9ghg11j72goa

TMhttps://app.box.com/s/ofmhsttqujor6di0tm89tiiikm3ou1xj

完整代码为:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{
int a,b,div,value,k,i,j,tm,ler;  
char string[256];
char string1[256];
char string2[256];

FILE *TM = fopen("TM","r");
if(TM == NULL)
{  
    printf("Can't open %s\n","TM");
    exit(1);
}
fscanf(TM,"%255s",string2);
tm = std::stoi(string2);
fclose(TM);

FILE *LER = fopen("LER","r");
if(LER == NULL)
{  
    printf("Can't open %s\n","LER");
    exit(1);
}
fscanf(LER,"%255s",string1);
ler = std::stoi(string1);
fclose(LER);

div=ler/2;

int **A;
A = (int **)malloc(tm*sizeof(int*));
for(j=0;j<tm;j++)
{
    A[j]=(int*)malloc(tm*sizeof(int));
}

int **B;
B = (int **)malloc(div*sizeof(int*));
for(j=0;j<div;j++)
{
    B[j]=(int*)malloc(2*sizeof(int));
}

int *C;
C = (int*) malloc(ler*sizeof(int));

if( A != NULL )
{
    for(i=0;i<tm;i++)
    {
        for(j=0;j<tm;j++)
        {
            A[i][j]=0;
        }
    }
}

FILE *stream = fopen("REL","r");
if(stream == NULL)
{  
    printf("Can't open %s\n","REL");
    exit(1);
}

for(i=0;i<ler;i++)
{
        fscanf(stream,"%255s",string);
        C[i]=std::stoi(string);
}
fclose(stream);

if( C != NULL )
{
    k=0;
    for(i=0;i<div;i++)
    {
        for(j=0;j<2;j++)
        {
            B[i][j]=C[k];
            k++;
        }
    }
    free(C);
}
//////////////////////////////////////////
if( B != NULL )
{
    for(i=0;i<div;i++)
    {
        a=B[i][0];
        b=B[i][1];
        std::cout<<a<<"\t"<<b<<"\n";
        A[b][a]=A[b][a]+1;
        A[a][b]=A[a][b]+1;
    }   
    free(B);
}
//////////////////////////////////////////
for(i=0;i<tm;i++)
{
    for(j=0;j<tm;j++)
    {
        cout<<A[i][j]; 
    }
    cout<<"\n"; 
}
free(A);
}

【问题讨论】:

  • 在调试器中运行它并找出它崩溃的确切位置 - 也可以为您提供原因的线索。其他选择是添加打印以缩小位置。
  • 谢谢@John3136,我添加了打印以缩小范围。已发布部分是本文的第一块。
  • stoi 是否按预期运行?
  • 你有没有保证 A!=NULL 在那个时候(不太可能是问题的根源,但你仍然在检查 B 为什么不检查 A)?
  • @BLUEPIXY 我用它将数字字符串转换为数字数字,并且当我在发出块之前打印矩阵时......是的,它有效

标签: c++ c segmentation-fault coredump


【解决方案1】:

我认为您需要对外部接受的值添加检查(使用assert 可能是一个开始)喜欢:

  • 检查tm&gt;0
  • ler&gt;0
  • C[i]&lt;tm
  • A[i]!=NULL
  • B[i]!=NULL

正如评论中提到的,它是一个一个问题:

在 LER 中,值应该从 0 到 tm-1
或在第 88 行使用B[i][j]=C[k]-1;

【讨论】:

  • 我想补充一点,使用gdb 或将您的输入分成几部分可以​​帮助您查明问题
猜你喜欢
  • 2021-06-03
  • 2018-07-28
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
相关资源
最近更新 更多