Thread: ***Stack smashing detected***: ./a.out terminated aborted core dumped

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    5

    ***Stack smashing detected***: ./a.out terminated aborted core dumped

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct person{
        char name[100];
        char address[100];
        char IDnumber[20];
        int  age;
    };
    void copystructure(char structure[100],struct person *ptr);
    
    void main(void){
    
        FILE *fp1;
        fp1=fopen("file1.txt","r");
        char ch;
        int i=0,j=0;
        char tmp[100];
        struct person *ptr=NULL;
        struct person arr[100];
        ptr=&arr[0];
        while((ch=fgetc(fp1))!=EOF){
            switch(ch){    
                case'\n': copystructure(tmp,ptr); ptr=&arr[j++];  break;
                default: tmp[i]=ch;i=i+1;
            }
        }
    }
    void copystructure(char structure[100],struct person *ptr){
    
        int i,k,j=0;
        char tmp[100];
        ptr=(struct person *)malloc(sizeof(struct person));
        for(i=0;structure[i]!='\0';i++,j++){
            if(structure[i]==','){
                break;
            }
         tmp[j]=structure[i];    
        }
        tmp[j]='\0';
        j=0;
        strcpy(ptr->name,tmp);
        for(i=i+1;structure[i]!='\0';i++,j++){
            if(structure[i]==','){
                break;
            }    
         tmp[j]=structure[i];
        }
        tmp[j]='\0';
        j=0;
        strcpy(ptr->address,tmp);
        for(i=i+1;structure[i]!='\0';i++,j++){
            if(structure[i]==','){
                break;
            }
         tmp[j]=structure[i];
        }
        tmp[j]='\0';
        j=0;
        strcpy(ptr->IDnumber,tmp);
        for(i=i+1;structure[i]!='\0';i++,j++){
         tmp[j]=structure[i];
        }
        j=0;
        k=structure[i-2]-'1'+1;
        k=k*10+(structure[i-1]-'1'+1);
        ptr->age=k;
        free(ptr);
    }
    file1.txt is shown below
    Code:
    Name1,Address1,IDnumber1,1
    Name2,Address2,IDnumber2,2
    Name3,Address3,IDnumber3,3
    Name4,Address4,IDnumber4,4
    Name5,Address5,IDnumber5,5

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    So what's your question?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    What is the copystructure function supposed to do? If you are trying to copy a particular suggestion from another thread that you started, don't: you do not even seem to understand the idea behind the parser, and frankly it is both unnecessarily complicated and potentially more space-inefficient. Go with fgets and sscanf. You may have "liked" my replies, but you are not doing what I suggested, i.e., to write a function to parse for just one record with fgets and sscanf, and then use that function to parse all the records in a loop.

    By the way, this:
    Code:
    void main(void)
    should be:
    Code:
    int main(void)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pthread and malloc -Aborted (core dumped)
    By Hassan Ahmed in forum C Programming
    Replies: 11
    Last Post: 07-12-2013, 11:24 PM
  2. Aborted (core dumped)? Error when running
    By Brian Justice in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2012, 10:35 PM
  3. *** stack smashing detected ***
    By chakra in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 09:12 PM
  4. *** stack smashing detected ***
    By Martin_HS in forum C Programming
    Replies: 9
    Last Post: 05-29-2009, 04:01 AM
  5. help on *** stack smashing detected ***
    By jodelson in forum C Programming
    Replies: 8
    Last Post: 08-16-2007, 06:25 PM