Thread: using free()

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    using free()

    i dynamically allocated a multi dimmed array defined as

    char **cptr;

    first i issued malloc for cptr like:

    cptr = malloc(sizeof(char *) * 5);

    and then i issued malloc using a loop
    for each element of cptr like:

    cptr[ctr] = malloc(20);

    NOW, do i have to free() EACH ELEMENT of the array in a loop like:

    free(cptr[ctr]);

    or just issue one call ?:

    free(cptr);

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You have to do the reverse of the allocation. Loop through freeing each element and then free the array of pointers.
    zen

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    errors

    that's what i was trying to do.
    i free each element and then i free the array of pointers.

    but i keep getting a runtime error during the freeing of the elements !

    here is what i did:

    for (ctr = 0; ctr < MAX_ENTRY; ctr++)
    {
    printf("%s\n", cptr[ctr]);
    free(cptr[ctr]);
    }

    free(cptr);

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Don't mean to be rude here, but why are you even doing it this way? Why not use calloc() to allocate a multi-dimensional array?

    But speaking of your problem:

    I am pretty sure that what is happening is that your overwrite your first pointer (cptr) with another pointer cptr[0], who both point to the same place, and then you are trying to free the same address twice. Hope this make sense...
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    but why are you even doing it this way? Why not use calloc() to allocate a multi-dimensional array?
    malloc and calloc do the same exact thing:

    calloc( sizeof( somevar ), number_of_elements );

    Is the same as:

    malloc( sizeof( somevar ) * number_of_elements );

    The end result is the same thing. You gain nothing by calling 'calloc' over 'malloc', other than typing ',' instead of '*', and 'c' instead of 'm'.

    In this case, 'calloc' will not help you. If you want randomly sized strings, you still have to allocate them seperately:

    myStrings = calloc( sizeof( char* ), number_of_pointers );
    myStrings = malloc( sizeof( char* ) * number_of_pointers );

    They're the same thing.

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

  6. #6
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    I beg to differ...

    malloc() only takes the length of the memory to allocate:

    malloc(sizeof(int));

    when calloc() takes the number of elements to allocate and the length of individual element:

    calloc( 10/*elements*/, sizeof(int));

    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I beg to differ...

    malloc() only takes the length of the memory to allocate:
    Like I said, malloc and calloc work the same. The end result is exactly the same:

    x = calloc( num, size( x ) );
    x = malloc( num * size(x) );

    for( y=0; y<num; y++ ) x[y] ...

    In either case, the end result is EXACTLY the same.

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

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And both are true...

    void* calloc (size_t sizeObjCnt, size_t sizeObject);
    void* malloc (size_t sizeObject);









    _________________________

    1) Do not think dishonestly.
    2) The way is in training.
    3) Become aquainted with everey art.
    4) Know the ways of all professions.
    5) Distinguish between gain and loss in worldly matters.
    6) Develop intuitive judgement and understanding in every situation.
    7) Percieve the things that cannot be easily seen.
    8) Pay attention to even the smallest matters, don't allow the slightest forgetfulness.
    9) Do nothing that is of no use.

    ____________________________
    [ Japan, 16th century Samurai Code]
    Last edited by Sebastiani; 12-27-2001 at 01:39 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    If I might humbly point out, space allocated by calloc will be initialized to 0. Space allocated by malloc is uninitialized.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    hey

    so what about some answers regarding my original question please..... thanks

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    What you've posted seems ok (cptr[0] doesn't point to the same location as cptr). Make sure that the number of allocations match. You've allocated five pointers in your first post, does MAX_ENTRY equal five? The complete alloc/dealloc should look something like this -

    Code:
    #include <stdlib.h>
    
    #define MAX_ENTRY 5
    
    int main() {
    
    	int ctr;
    	char **cptr; 
    	cptr = malloc(sizeof(char *) * MAX_ENTRY); 
    	for (ctr = 0; ctr < MAX_ENTRY; ctr++) 
    	{ 
    		cptr[ctr] = malloc(20); 
     
    	} 
    
    	//Do stuff
    
    	for (ctr = 0; ctr < MAX_ENTRY; ctr++) 
    	{ 
    		free(cptr[ctr]); 
    	} 
    	
    	free(cptr);
        return 0;
    }
    zen

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    7.14.3.1 The calloc function

    Synopsis

    [#1]

    #include <stdlib.h>
    void *calloc(size_t nmemb, size_t size);

    Description

    [#2] The calloc function allocates space for an array of
    nmemb objects, each of whose size is size. The space is
    initialized to all bits zero.225

    Returns

    [#3] The calloc function returns either a null pointer or a
    pointer to the allocated space.

    7.14.3.3 The malloc function

    Synopsis

    [#1]

    #include <stdlib.h>
    void *malloc(size_t size);

    Description

    [#2] The malloc function allocates space for an object whose
    size is specified by size and whose value is indeterminate.

    Returns

    [#3] The malloc function returns either a null pointer or a
    pointer to the allocated space.


    That should clear up and arguments over malloc() vs. calloc().... also all bits 0 isn't guaranteed to mean it holds the actual value of 0.
    one fish two fish
    red fish blue fish

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the entire point. The point was:

    Don't mean to be rude here, but why are you even doing it this way? Why not use calloc() to allocate a multi-dimensional array?
    When you come in and say "Why are you even doin it this way?" and "Why not use calloc()..." it implies that 'calloc' is the better choice. In reality, using malloc and using calloc have the same thing. Furthermore, you could even use 'realloc' and end up with the same end result. That was my point. They all produce the same endresult: an allocated block of memory.

    In other words: They _ALL_ will produce "an array" of allocated data if you want them to. Furthermore, calloc an do exactly what malloc does if you tell it to only allocate 1 member. That is my point. They all function the same, there is no "wrong" way here.

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

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    Um... I don't think I missed any point. I wasn't trying to prove or disprove anyone, I was just posting what the standard said about the functions to clear up and misconceptions people had about them. Quzah you seem (to me, though I am probably very wrong) to think that I am trying to target you in most posts when actually I agree with most things you say... maybe you are reading too deep into things?
    one fish two fish
    red fish blue fish

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorry. I jump the gun some times. I was just saying that I wasn't confused on how they work, and that was my whole point.

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

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, 06: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