Thread: array out of bound seg fault

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    array out of bound seg fault

    Code:
    int read_word(int fld_delim, int rec_delim, int eor_delim, char field[MAXSIZE], FILE *fp) {
    
     int p, i;
            p = i = 0;
    
            while ( (p=getc(fp))!= EOF ) {
                   if( p == fld_delim) {
                          field[i++] = '\0';
                          return FLD_DELIM;
                   }
    
                   if( p == rec_delim) {
                          field[i++] = '\0';
                          return REC_DELIM;
                   }
                   if ( p == eor_delim ) {
                          field[i++] = '\0';
                          return EOR_DELIM;
                   }
    
                   if ( i <= MAXSIZE ){
                     printf("i=%d",i);
                          field[i++] = p; // <-- I get array out of bound here
                   printf("field= %s\n",field);
                   }
            }
            field[i++] = '\0';
            return FINISH;
    }
    Thanks for the help!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So stop writing to memory you don't own. What more help do you want?

    An array is defined like so:
    int array[10];
    and is accessed:
    array[0] through to array[9]

    Accessing array[10] is going past the end, and is therefore out of bounds.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. seg fault on unix platform
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-08-2001, 12:04 PM