用C的另一个核心转储问题 [英] Another coredump issue in C

查看:146
本文介绍了用C的另一个核心转储问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译我的code。与结果
G ++ -g -o prueba prueba.cpp -lstdc ++ -O3 -march = corei7 -mtune = corei7 -std =的C ++ 0x

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

  prueba.cpp:24:6:错误:Stoi旅馆'不是'性病'中的一员
TM =的std :: Stoi旅馆(字符串2);
     ^
prueba.cpp:34:7:错误:Stoi旅馆'不是'性病'中的一员
LER =的std :: Stoi旅馆(字符串1);
      ^
prueba.cpp:77:8:错误:Stoi旅馆'不是'性病'中的一员
C [i] =的std :: Stoi旅馆(字符串);
     ^

我宣布 Stoi旅馆的std :: Stoi旅馆的方法的基础上的这个例子

和破碎块是:

  ////////////////////////////////////// ////
如果(B!= NULL)
{
    对于(i = 0; I< D​​IV,我++)
    {
        A = B [I] [0];
        B = [I] [1];
        性病::法院LT&;< A<<\\ t的<< B<<\\ n;
        A [B] [A] = A [B] [A] +1;
        A [A] [B] = A [A] [B] +1;
    }
    免费(B);
}
//////////////////////////////////////////

这是给我段错误的问题。但我看不出有什么或问题出在哪里。

该文件是:

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

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

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

满code是:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&malloc.h所GT;
#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&string.h中GT;使用命名空间std;诠释的main()
{
诠释A,B,DIV,价值,K,I,J,TM,LER;
字符字符串[256];
字符字符串1 [256];
字符字符串2 [256];FILE * TM =的fopen(以旧换新,R);
如果(TM =​​= NULL)
{
    的printf(无法打开%s \\ n,以旧换新);
    出口(1);
}
的fscanf(TM,%255秒,字符串2);
TM =的std :: Stoi旅馆(字符串2);
FCLOSE TM;FILE * LER =的fopen(LER,R);
如果(LER == NULL)
{
    的printf(无法打开%s \\ n,LER);
    出口(1);
}
的fscanf(LER,%255秒,字符串1);
LER =的std :: Stoi旅馆(字符串1);
FCLOSE(LER);DIV = LER / 2;INT ** A;
A =(INT **)的malloc(TM *的sizeof(INT *));
为(J = 0; J<以旧换新; J ++)
{
    A [J] =(INT *)malloc的(TM *的sizeof(INT));
}INT ** B:
B =(INT **)的malloc(DIV * sizeof的为(int *));
为(J = 0; J< D​​IV; J ++)
{
    B〔J] =(INT *)malloc的(2 * sizeof的(INT));
}INT * C;
C =(INT *)malloc的(LER *的sizeof(INT));若(a!= NULL)
{
    对于(i = 0; I<以旧换新;我++)
    {
        为(J = 0; J<以旧换新; J ++)
        {
            A [I] [J] = 0;
        }
    }
}FILE *流=的fopen(REL,R);
如果(流== NULL)
{
    的printf(无法打开%s \\ n,REL);
    出口(1);
}对于(i = 0; I< LER,我++)
{
        的fscanf(流,%255秒,字符串);
        C [i] =的std :: Stoi旅馆(字符串);
}
FCLOSE(流);如果(C!= NULL)
{
    K = 0;
    对于(i = 0; I< D​​IV,我++)
    {
        为(J = 0; J&2; J ++)
        {
            B〔I] [J] = C [K];
            ķ++;
        }
    }
    免费(C);
}
//////////////////////////////////////////
如果(B!= NULL)
{
    对于(i = 0; I< D​​IV,我++)
    {
        A = B [I] [0];
        B = [I] [1];
        性病::法院LT&;< A<<\\ t的<< B<<\\ n;
        A [B] [A] = A [B] [A] +1;
        A [A] [B] = A [A] [B] +1;
    }
    免费(B);
}
//////////////////////////////////////////
对于(i = 0; I<以旧换新;我++)
{
    为(J = 0; J<以旧换新; J ++)
    {
        COUT<< A [I] [J]。
    }
    COUT<<\\ n;
}
免费(A);
}


解决方案

我想你需要添加你的外部接受的值进行检查(使用断言可能是一个启动)喜欢的:


  • 检查 TM大于0

  • LER大于0

  • C [1] - ; TM

  • A [I]!= NULL

  • B [I]!= NULL

作为mentionned在评论它的关闭一个问题:

在LER值应该是从0到TM-1 结果
或者使用 B [I] [J] = C [K] -1; 在行88

when I compile my code with
g++ -g -o prueba prueba.cpp -lstdc++ -O3 -march=corei7 -mtune=corei7 -std=c++0x

After debugging with g++ -g prueba.cpp, I got these:

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);
     ^

The way I declare stoi as std::stoi was based on this example.

And the broken block is:

//////////////////////////////////////////
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);
}
//////////////////////////////////////////

which is given me the problem with segmentation fault. But I don't see what or where is the problem.

The files are:

LER https://app.box.com/s/oi52zw7j8w19txr4cau2pf4w3pzmcelm

REL https://app.box.com/s/bo2xwm2hviucx4jzarxv9ghg11j72goa

TM https://app.box.com/s/ofmhsttqujor6di0tm89tiiikm3ou1xj

The full code is:

#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);
}

解决方案

I would think you would need to add check on your externally accepted values (using assert might be a start) Like :

  • checking tm>0
  • ler>0
  • C[i]<tm
  • A[i]!=NULL
  • B[i]!=NULL

As mentionned in the comment it an off by one issue :

in LER the values should be from 0 to tm-1
or use B[i][j]=C[k]-1; at line 88

这篇关于用C的另一个核心转储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆