Thread: sizeof, calloc and free questions

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Wink sizeof, calloc and free questions

    Hi,

    Anyone could elaborate the function of sizeof, calloc and free with the following examples:

    #include <stdio.h>

    struct date {
    int hour, minute, second;
    };

    int x;

    x = sizeof (struct date); //return a value?

    struct date *date_pointer;

    date_pointer = (struct date *) calloc(10, sizeof(struct date)); // What is this statement mean???

    free (date_pointer); //When to use it?

    Also, I want to know why "sizeof array / sizeof array[0]" can return the numbers of elements?

    #include <stdio.h>

    main()
    {
    int array[] = {5, 4, 3, 2, 1, 10, 8, 9, 89, 0, 1};

    printf("The no. of elements in the array[] is %d\n", sizeof array / sizeof array[0]);

    return 0;
    }

    BTW, is typedef equal to struct?

    Thanks for any help.

    gogo
    Last edited by gogo; 10-24-2001 at 02:09 AM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    sizeof() returns the number of bytes the item in the brackets is. Used to find the amount to read from files, for memory allocation ect.
    Small note, the sum of the eleements(items within) a structure is not always the same as the size of the whole structure.

    calloc, like malloc is a function to allocate memory. Calloc takes the number of elements then the size of an element. All memory calloc'ed is set to zero rether than just inheriting the values in the memory. Some times it is better to allocate memory as teh program runs. For example if you don't know how much you will need. Memory that was malloc'ed or calloc'ed can be realloc'ed to increase or decrease its size.

    When the memory is no longer needed it must be free'ed. This returns the memory. Use free as soon as you no longer need the memory. (for each malloc, calloc there should be a free)

    >>date_pointer = (struct date *) calloc(10, sizeof(struct date)); // What is this statement mean???
    It means you want enough memory to hold ten date structures.
    It is almost the same as
    struct date date_pointer[10];
    as calloc was used all elements in the 10 struct date will be set to zero.
    The (struct date *) is a 'type cast' to tell that the date_pointer if incremented (ie date_pointer++) or indexed (ie date_pointer[2]) the compiler will know to move by the sizeof(struct date) and so will find the start of a structure.

    If you send the unindexed array (ie date_pointer) to a function it will send a pointer to the WHOLE memory block. So the size returned from sizeof(date_pointer) would be 10 times the size of one structuret.(in your code you calloc for 10)
    if you send in the indexed array (ie date_pointer[0]) you are sending in the first element only and so it will be the size of one structure.
    So
    "sizeof(array)/sizeof(array[0])"
    is the same as
    total memory/element or structure size

    Clear as mud?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > BTW, is typedef equal to struct?
    No, you can use typedef to create a name for any existing type, not just structures.

    Eg.
    typedef unsigned short int uint_16;
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    got it.

    Thanks for help.

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  2. Free the pointer
    By dunxton in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 03:50 AM
  3. segmentation fault (calloc, free)
    By kentadams in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 08:50 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. free()
    By falconetti in forum C Programming
    Replies: 2
    Last Post: 03-05-2002, 10:56 PM