Thread: Segmentation faults out the ****

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    im really sorry that you had to type all that cause i understood everything upto

    /* sizeof(temp) will typically be 4 on 32 bit systems. You probably want a value of about 19 */

    and then you lost me.

    what does "4 on 32 bit system" mean?

    i mostly understand:

    /* undefined behaviour. The buffer pointed at by loadedfile is not initialised */

    do you mean that i have to say something like:

    loadedfile = "a" or something?

    if so can i initialise it to NULL?

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Quote Originally Posted by bithub
    First of all, you're not allocating 599 slots. You're allocating 600 * sizeof(char*) slots which probably ends up being 600 * 4 = 2400 depending on your machine.

    Second of all, how do you know the memory you've allocated is enough? You never check the file size beforehand, or keep track of how many bytes you are copying in to the loadedfile buffer.

    Here is a better version of your function
    Code:
    void ReadCatalogue(FILE *myfile)
    {
    	char		*loadedfile;
    	char		temp[512];
    	int		buffersize = 600;
    	int		bytescopied = 1; // start at 1 to account for null term
    
    	loadedfile = malloc(buffersize);
    	loadedfile[0] = '\0';  // so strcat() doesn't tank the first through the loop
    
    	while(fgets(temp,sizeof(temp),myfile))
    	{
    		bytescopied += strlen(temp);
    		if(bytescopied > buffersize)
    			break; // break from while loop if buffer isnt big enough to copy everything
    		strcat(loadedfile,temp);
    	}
    	printf(loadedfile);
    }
    i probably should have mentioned this earlier but two of the restraints i have are,
    1. Only runtime memory is to be used.
    an
    2. You have to know what your doing.

    im pretty sure the tutors would pick up on that code in no-time. sorry, normally i would use it and have you money in the mail, but i'd get more marks with what i have than if i dont know what the code does.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well I'd rather have you understand it anyways. I didn't realize this was homework.

    Basically, in your code you can stop the strcat() call from crashing your program by adding the following code after your malloc() statements:
    Code:
    loadfile[0] = 0;
    strcat() appends a string onto another string. In order to make loadfile an empty string, you just need to make the first character a null terminator.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Both functions still have the same problem of you not freeing anything you allocated. Whenever you allocate memory, you must free it some time in your program. Failure to do so is a BadThing(TM). Other than that, it's commented pretty good as to what it does. The main thing here is that to be a string, you have to have a null character in it. That's how functions like strlen find the end of the string to tell how long it is, or how strcat finds the end so it can begin copying at the end.

    You had allocated memory (with your malloc call), but you hadn't initialized any of it. As such, you have no idea what that memory you've just allocated actually contains. Therefore, you have no idea if there really is a null character in that memory, and if there is, where it is. Given that, where is strcat going to begin copying? It may not even find a null character (since you didn't initialize any of it), so it may run off the end of your allocated space and trash some other spot in memory.

    So, what they're doing is setting the first character of the allocated space to be the null character. This then allows you to use that space as a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    hey, quzah and bithub, ever consider tutoring first year uni students?

    i tells ya, you'ed be great at it.

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    hehe, you guys must be really getting tired of my noobishness.

    Code:
    char *Catalogue = malloc(sizeof(*Catalogue)*10);
    Catalogue = "a";
    printf("What file would you like to load? ");
    scanf("%9s",Catalogue);
    ReadCatalogue(myfile);

    segments here

    if i try catalogue[0] = "a"; like was suggested earlier, then i get this warning

    Code:
    warning: assignment makes integer from pointer without a cast
    this is really starting to ........ me off too, so im sorry for all those that read my code and start rocking in your chairs because of how bad it looks, but i dont do it on purpose.

    i also get the same problem here:

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ubber_C_Noob
    hehe, you guys must be really getting tired of my noobishness.

    Code:
    char *Catalogue = malloc(sizeof(*Catalogue)*10);
    Catalogue = "a";
    The fist line, you allocate some space for this pointer. The second line you make it point to a string constant, some place else. This causes a memory leak, because you now have nothing pointing at the space you just allocated.
    Quote Originally Posted by Ubber_C_Noob
    if i try catalogue[0] = "a"; like was suggested earlier, then i get this warning

    Code:
    warning: assignment makes integer from pointer without a cast
    this is really starting to ........ me off too, so im sorry for all those that read my code and start rocking in your chairs because of how bad it looks, but i dont do it on purpose.

    i also get the same problem here:
    If you want to manipulate a single character a time, you need single quotes. Double quotes are for entire strings. Thus, you'd do:
    Code:
    Catalogue[0] = 'a';
    Which would make the first character of the string 'Catalogue', which you just allocated, be the single character 'a'. This still wouldn't put a null character in anywhere. Just the letter 'a'.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    thats fixed the warning, does anyone have any idea how to fix the porblem with the segmenting?

    if i try the '&' thing infront of Catalogue in the fopen statement i get

    Code:
    warning: passing arg 1 of `fopen' from incompatible pointer type
    and im really hesitant to fiddle with the code any more than i have to cause just my luck, it'll fall apat in my hands

    i know you guys arent getting payed to do this, but if your here and are able to help, why not spread a little cheer by helping some-one in a real jam? pretty please with a cherry on top?

  9. #24
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    never mind, thanx guys i fixed it, but only with your expert help.

  10. #25
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    hey, just me again:

    new segfault and this one aint my fault

    Code:
    char *temp = malloc(sizeof(*temp)*10);
    temp[0]='a';
    scanf("%9s",temp);
    thats it, it segfaults at scanf and i dont know why.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you putting an 'a' in there?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    meh, was initalizing it to 'a' cause thats how i got the catalogue array thing to work

    the Catalogue Array thingy reads in a string from the kb, and uses it as the file name in the fopens() statement:

    Code:
    char *Catalogue = malloc(sizeof(*Catalogue)*10);
    Catalogue[0]='a';
    printf("What file would you like to load? ");
    scanf("%9s",Catalogue);
    myfile = fopen(Catalogue,"r+")

    as you can see i just copied it and changed the Catalogue to choice.

    the choice selecting statment does not have to do anything, it is just there to give the user a semblance of control , it reads in an int and then goes does nothing with it.
    Last edited by Ubber_C_Noob; 09-11-2005 at 10:33 AM.

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, 02:24 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 07:23 AM
  4. segmentation faults pervade
    By ezwise in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:17 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM