unknown seg fault after malloc

This is a discussion on unknown seg fault after malloc within the C Programming forums, part of the General Programming Boards category; does anyone have any idea why this gives a segmentation fault? (and please tell me why) Code: char *cut(char *instr, ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    unknown seg fault after malloc

    does anyone have any idea why this gives a segmentation fault?
    (and please tell me why)

    Code:
    char *cut(char *instr, char delim, int field, int blen)
    {
    printf("monkeys\n");
            int i=0,curfield=0,startindex=0,len=str_length(instr);
            char *outstr;
            outstr = (char*) malloc ( sizeof(char) * str_length(instr) );
    //printf("monkeys\n");  ( if i have this here, it runs... if it is commented out it has a seg fault )
            while( (instr[startindex]!='\0') && (curfield!=field) && (i<len) )
            {
                    if(instr[startindex]==delim)
                            curfield++;
                    startindex++;
            }
            if(blen==0)
            {
                    i=0;
                    while( (instr[startindex+i]!=delim) && (startindex+i<len) && (i<str_length(instr)) )
                    {
                            outstr[i]=instr[startindex+i];
                            i++;
                    }
            }
            else if(blen>0)
            {
                    i=0;
                    while( (instr[startindex+i]!=delim) && (i<blen) && (startindex+i<len) && (i<str_length(instr)) )
                    {
                            outstr[i]=instr[startindex+i];
                            i++;
                    }
            }
            outstr[i] = '\0';
    printf("monkeys\n");
            return outstr;
    }

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    If where you pointed out is really failing, lets see the str_length function.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    oah, sorry

    Code:
    int str_length(char *str)
    {
      int i;
      while(str[i] != '\0')
        i++;
      return i;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by seaking1 View Post
    oah, sorry

    Code:
    int str_length(char *str)
    {
      int i;
      while(str[i] != '\0')
        i++;
      return i;
    }
    And why do you believe i isn't (say) -123887612?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    there's an error
    (i just deleted the =0, i guess i thought it was a for)

    that was my problem

    (I feel stupid)
    Last edited by seaking1; 02-25-2009 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 04:46 PM
  2. Unknown Seg Fault and Malloc/Realloc Problems
    By DonFord81 in forum C Programming
    Replies: 6
    Last Post: 12-01-2008, 11:49 PM
  3. unknown seg fault reason.
    By egoveneror2 in forum C Programming
    Replies: 16
    Last Post: 04-15-2008, 03:30 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21