Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

c++ - Another coredump issue in C

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<<"	"<<b<<"
";
        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
","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
","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
","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<<"	"<<b<<"
";
        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<<"
"; 
}
free(A);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...