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
>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. :)
Alright, here is the doubling of the allocated memory:
temp1 =(char **)malloc(n*sizeof(char));
if(temp1 == NULL)
temp2 =(int *)malloc(n*sizeof(int));
if(temp2 == NULL)
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 == 'B')
/*makes sure the last occurence of title is fully over written with nulls*/
/*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] == '\"')
/* THE PROBLEM ARISES IN THE INDEX_OF(TITLE) FUNCTION */
/*status is changed to unavailable*/
and now for the index_of(title) function where the seg fault occurs while accessing "books"
int index_of(char *key)
int index =-1;
if(key == NULL)
/* THIS IS WHERE IT SEG FAULTS */
if(strcmp(key,books[t])) == 0)
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.
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. :)
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:
/* Old: temp1 =(char **)malloc(n*sizeof(char)); */
temp1 = (char **)malloc(n*sizeof(title));
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
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!