Thread: appcrash segmentation fault char* inside struct

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    appcrash segmentation fault char* inside struct




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct info info;
    struct info
    {
        char* name;
    };
    
    void function1();
    void function2();
    void function3(info* urInfo);
    void printLength(info* INFO) { printf("%d",strlen(INFO->name)); }
    
    int main()
    {
        int stop;
        function1();
        
        function2();
        
        info* info_;
        function3(info_); //this allocates memory for ... info_
        
        
        printf("____________________");
        printLength(info_);//not correct :/ appcrash :/ segmentation fault
       
    
        scanf("%d",&stop);
        return 0;
    }
    
    void function1()
    {
            info* myInfo;
        myInfo = (info*) malloc(sizeof(info));
        
        
        char* c;
        c = (char*) malloc(100);
    
        int i;
        for(i=0;i<100;i++)
        {
            c[i] =  'n';
            printf("%c",c[i]);
        }
        c[i]  = '\0';
        
        printf("%d",strlen(c));
        
        myInfo->name = c;
        
        printf("\n strlen(myInfo->name) = %d \n",strlen(myInfo->name)); ///correct
        
        printf("\n < End of function1 >\n");
    }
    
    void function2()
    {
        info* urInfo = malloc(sizeof(info));
        urInfo->name = malloc(5);
        urInfo->name[0] = 'n'; urInfo->name[1] = 'j'; urInfo->name[2] = 'm'; urInfo->name[3] = 'a';
        urInfo->name[4] = 's'; urInfo->name[5] = '\0';
        printf("%d",strlen(urInfo->name)); //correct
    }
    
    void function3(info* urInfo)
    {
        urInfo = malloc(sizeof(info));
        urInfo->name = malloc(5);
        urInfo->name[0] = 'n'; urInfo->name[1] = 'j'; urInfo->name[2] = 'm'; urInfo->name[3] = 'a';
        urInfo->name[4] = 's'; urInfo->name[5] = '\0';
        printf("%d",strlen(urInfo->name)); //correct
        
        
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    The obvious thing is that you keep overflowing the bounds of
    your arrays.

    If you malloc a 5 element array you only have elements [0] to [4] to play with.

    Also using an index variable immediately after you use it to initialise an array will mean array[index] is also outside the
    array bounds.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In function3() you allocate space for an info to a copy of the info * in main. That change is not reflected back to main. You would have to pass the info * by reference
    e.g.
    Code:
    void function3(info** urInfo)
    {
        *urInfo = malloc(sizeof(info));
        (*urInfo)->name = malloc(5);
        (*urInfo)->name[0] = 'n'; (*urInfo)->name[1] = 'j'; (*urInfo)->name[2] = 'm'; (*urInfo)->name[3] = 'a';
        (*urInfo)->name[4] = 's'; (*urInfo)->name[5] = '\0';
        printf("%d",strlen((*urInfo)->name)); //correct
    }
    
    int main() {
        info* info_;
        function3(&info_); //this allocates memory for ... info_
     }
    Kurt

    EDIT: Just noticed this is supposed to be C++ so a real reference could be used
    Last edited by ZuK; 04-24-2013 at 03:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault; struct and pointer problem
    By sangelion in forum C Programming
    Replies: 8
    Last Post: 09-19-2012, 02:07 PM
  2. Replies: 3
    Last Post: 07-30-2012, 08:12 AM
  3. Replies: 9
    Last Post: 06-02-2012, 01:36 PM
  4. Replies: 7
    Last Post: 11-05-2011, 05:11 PM
  5. Replies: 8
    Last Post: 12-08-2009, 02:47 AM