Segmentation faults!

This is a discussion on Segmentation faults! within the C Programming forums, part of the General Programming Boards category; here is the basic idea of my program, its a library takes a file of titles, BORROW, RETURN, SHELVE, lines ...

  1. #1
    Chris Gat
    Guest

    Segmentation faults!

    here is the basic idea of my program, its a library takes a file of titles, BORROW, RETURN, SHELVE, lines each line indicating an operation. In my first implementation i used a 2d array to store my titles, and the number of titles was limited. Now i have to use malloc() to store the information. When i increase the size of allocated memory i start running into problems indirectly. For example, once i have done increased the size of the allocated memory i can print out the entire library without a problem. Then i can continuely access each element for searching purposes without a problem until a certain point. ie I would search the library maybe 3 times and on the third time when i try to access book 67 i get a segmentation fault. It seems to come from nowhere, a second ago i could access it and then suddenly i cant????? Has anyone had experience with a problem like this and might happen to know where the problems may lie? if you would like me to post my code i will, but im basically looking for someone that my have come across this problem before. Also, i programming in strictly ansi c if that matters. CHEERS

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Also, i programming in strictly ansi c if that matters.
    If you do then I applaud you.

    First, if you could post code that would be nice. Preferably something that is bare bones and still demonstrates the problem. If you can find the line where the segv is happening then comment it. The problem with these errors is that they can happen anywhere and be caused by something anywhere earlier in the code. A segmentation violation is what happens when you dereference an invalid pointer.

    As for your implementation, it seems to me that a binary tree would be better suited to your needs than a resizable 2D array. But that is neither here nor there since that wasn't your question.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Chris Gat
    Guest

    some code

    Alright, here is the doubling of the allocated memory:
    if(bookline>=(n-2))
    {
    n=2*n;
    temp1 =(char **)malloc(n*sizeof(char));
    if(temp1 == NULL)
    {
    printf("ERROR\n");
    return (1);
    }
    temp2 =(int *)malloc(n*sizeof(int));
    if(temp2 == NULL)
    {
    printf("ERROR\n");
    return (1);
    }
    for(i=0;i<n/2;i++)
    {
    temp1[i]=books[i];
    temp2[i]=status[i];
    }
    free(books);
    free(status); /*Questionable?*/
    books = temp1;
    status = temp2;
    }
    After that has happened, the final book has been added to the library and we now have a list of BORROW lines coming from the input, so we are continously entering a the BORROW if statement below.
    else if(buffer[0] == 'B')
    {

    /*makes sure the last occurence of title is fully over written with nulls*/
    for(nullify=0;nullify<80;nullify++)
    {
    title[nullify]='\0';
    }

    /*continues to assign a copy the buffer into books until two quotes have been met*/
    while(indicator != 2)
    {
    /*the position is 7 spaces over to account for BORROW*/
    title[pos] = buffer[7+pos];
    if(buffer[7+pos] == '\"')
    {
    indicator++;
    }
    pos++;
    }
    /* THE PROBLEM ARISES IN THE INDEX_OF(TITLE) FUNCTION */
    /*status is changed to unavailable*/
    status[index_of(title)]=0;
    }

    and now for the index_of(title) function where the seg fault occurs while accessing "books[]"

    int index_of(char *key)
    {
    int index =-1;
    int t;
    if(key == NULL)
    {
    return -1;
    }

    for(t=0;t<bookline;t++)
    {

    /*printf("book:%d\n",t);*/
    /* THIS IS WHERE IT SEG FAULTS */
    if(strcmp(key,books[t])) == 0)
    {
    return (t);
    }
    }
    return (index);
    }
    It must be pointed out that these are the only options "touching" books and that in B if statement i have successfully printed(accessed) all the books until suddenly when i try and access a certain book i get a seg fault. I know this is not the best implementation, i am still new to C and the ins and outs. Either way cheers for your time Prelude.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Chris, can you show us what the variables are defined as (books, status, temp1 etc).

    This looks strange
    >temp1 =(char **)malloc(n*sizeof(char));
    Your malloc()'ing sizeof a char... so is temp1 a pointer to array of chars or is it supposed to be pointer to array of char pointers?

    As Prelude suggested, a different approach might be in order. I understand you're new and trying to learn, but doing things the hard way isn't always the best thing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest

    I figured it out!

    I declared books and temp1 as a double pointer because I wanted each index to point to an array of characters aka a string. The problem lied in the fact that I was only allocating space for a char, not a title([code] char *title=(char *)malloc(80*sizeof(char)[\code]), by placing "title" in the sizeof parenthesises i fixed the seg fault. I'm not really sure why it was messing up as it did, but the problem is fixed. Visually, i did this:
    [code]
    /* Old: temp1 =(char **)malloc(n*sizeof(char)); */
    temp1 = (char **)malloc(n*sizeof(title));
    [\code]

    Thanks a lot for the help/suggestions guys, also the links posted by prelude will help me ask my question the right way the next time I post. Cheers

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I figured it out!

    >Thanks a lot for the help/suggestions guys, also the links posted by prelude will help me ask my question the right way the next time I post.
    And try to get the code tags right next time You're using the wrong slash!
    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. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  2. oldiofclose.c and segmentation faults
    By sd_padilla in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 01:24 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 06:23 AM
  4. segmentation faults pervade
    By ezwise in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 02:17 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

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