Thread: how free unsigned pointer array in best way (calloc)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    how free unsigned pointer array in best way (calloc)

    Hi

    how is the best way to free unsigned pointer array allocated with cmallc?

    Code:
      
      uint8_t *buf;
        buf = cs_calloc(ca->len + 13);


    i do like this , but i know this is not quite right , since i got compile warning :
    warning: passing argument 1 of ‘free’ makes pointer from integer without a cast

    Code:
    for (i = 0; i < ca->len + 13; i++) 
    {    
     free(buf[i]); 
    buf[i] = NULL;
    }
    
     free(buf);
    do i need to free each element of array like above? do i need to fill with null after same as above ? would appreciate someone correct how i wrote free

    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > buf = cs_calloc(ca->len + 13);
    If this is all you did, then the correct thing is just
    cs_free(buf);

    You don't look at the parameter to calloc, you look at the result.

    Each unique result ultimately needs exactly one call to free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Quote Originally Posted by Salem View Post
    > buf = cs_calloc(ca->len + 13);
    If this is all you did, then the correct thing is just
    cs_free(buf);

    then how cs_free() function should be? this is exactly what i asked (my question remains.) i mean do i need to free each of array elements (free (buf[i]) before free buf ? what's wrong about the free procedure i had wrote above that i get that compile warning :

    warning: passing argument 1 of ‘free’ makes pointer from integer without a cast
    as also wrote in my first post of this thread , free like this is it correct ?

    Code:
    for (i = 0; i < ca->len + 13; i++) 
    
    
    {    
    
     free(buf[i]); 
    
    buf[i] = NULL;
    
    }
    
     
    
     free(buf);
    Thanks in advance
    Last edited by nima; 02-25-2013 at 01:43 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is there something wrong with your eyes or reading skill?

    It's right there in the very thing you quoted!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    @salem
    Oh sorry if i didn't get your meaning , but i had wrote cs_calloc myself in my program , that's why i asked , how cs_free shall be (since i assuemed , when i write cs_calloc myself , i need to write cs_free function myself too...

    Still i don't know the reason that i got above compile warning , sorry if i am not sharp enough , but i think , there is nothing wrong about asking what u don't know it's wrong to don't know something and do not ask it.

    Thanks u so much for replying.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    i think now i get what u meant , u meant , it's not need to free the elements of arrays , i just need to free (buf) so i should remove that for loop for freeing buf elements . anyway u didn't say this directly to me , so i guess what u meant . lol
    Last edited by nima; 02-25-2013 at 02:52 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you get the memory in the first place using calloc() / malloc() / realloc(), then the way to free it is using free()

    You can put them inside wrappers like cs_calloc() and cs_free() if you want, that's up to you.

    Or you can call free() directly.

    The point is, you have
    ptr = someAllocationFunction();

    then later, you do
    free( ptr );

    It doesn't matter how much memory it is, what structure it contained, or whether you treated it as an array or not.

    ONE ALLOC needs ONE FREE

    Simples!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  2. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  3. Replies: 5
    Last Post: 12-05-2007, 04:39 PM
  4. segmentation fault (calloc, free)
    By kentadams in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 08:50 AM
  5. sizeof, calloc and free questions
    By gogo in forum C Programming
    Replies: 3
    Last Post: 10-25-2001, 05:32 AM