error when using free

This is a discussion on error when using free within the C Programming forums, part of the General Programming Boards category; i'm getting a segmentation fault when unallocating memory using free. int *array; array = (array *)malloc(size); .. .. if(array != ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    error when using free

    i'm getting a segmentation fault when unallocating memory using free.

    int *array;

    array = (array *)malloc(size);
    ..


    ..
    if(array != NULL) {free(array);}

    i'm doing a lot of stuff in between.

    what conditions would give a seg fault here?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    sorry - should read

    array = (int *)malloc(size);

    using gdb the seg fault occurs at the free function!

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ummm, is size an initialized variable? what happens when you try to free right after malloc ()?
    hello, internet!

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    yep, size is initialised. the prog works fine withour the free - i just don't want any leaks.

    if i put the free directly after the malloc its ok.

    what could i be doing in between?

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks salem, but its a variable size not the size of an int.

    my problem is with free.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by rotis23
    thanks salem, but its a variable size not the size of an int.

    my problem is with free.
    ehh? are you making an array of ints or one int? if its one int then its

    malloc (sizeof (int))

    if its an array of ints then its

    malloc (sizeof (int) * numberofints)

    nothing else makes sense for an int * variable.
    hello, internet!

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    ok, the size has already been calculated.

    int size,numberofints;

    numberofints = whatever

    size = sizeof (int) * numberofints;

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: error when using free

    Originally posted by rotis23
    i'm getting a segmentation fault when unallocating memory using free.

    int *array;

    array = (array *)malloc(size);
    ..


    ..
    if(array != NULL) {free(array);}

    i'm doing a lot of stuff in between.

    what conditions would give a seg fault here?
    Did you try to free the array twice?
    Remember that the free function doesn't assign a NULL pointer to the array.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    only 1 free for that array.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    in nested loops i have the following twice:

    array[array_count] = atoi(number);
    array++;

    array_count is initialised at start (before you ask):

    int array_count = 0;

    the array is used to check if it contains a certain number in a seperate function:

    if(test_contain(array,number,array_count) == 1){...


    int test_contain(int *arrayin,int numberin,int count) {
    int i;
    for(i=0;i<count;i++) {
    if(arrayin[i] == numberin) {
    return 0;
    }
    }
    return 1;
    }

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    sorry typed/copied it wrong. should be array_count++;

    i'll try what you said. lot of code though.

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    i'm human alright..



    ... i make mistakes..



    ..ok, i admit it. i didn't set numberofints properly!!!!!!!!!!!!!!

    back to basics for me. thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 05:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM

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