1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Malloc & Calloc difference in terms of memory allocated

Discussion in 'C' started by swapnaoe, Apr 19, 2007.

  1. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hi People,
    Please find my queries mention below.
    --------------------------------------------------------------------------------------------------------
    How is memory allocated by malloc?
    Is the memory allocated contiguous? Is so, What if the requested number of bytes are not available contigously?
    Is casting really required?? If u don't cast the pointer what will be the disadvantage?
    What is the difference between calloc and malloc in terms of memory allocation?
    --------------------------------------------------------------------------------------------------------
    I will be happy if some body could give me a diagramatic representation of the memory allocated by these two functions(calloc & malloc).

    Regards
    Swapna
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Did you read the documentation?
    One must presume that this applies to a contiguous array, not so?
    'Nuff said?

    Casting the return of malloc is necessary in C++ because of stronger typing constraints. However, malloc should not be used in C++, so the question is (should be) moot.

    In C, the void pointer is intrinsically convertible. Casting in C may actually hide potential errors if future changes to code (pointer types) are not made completely and thoroughly.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available and use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object in the heap and returns a valid pointer to that item.

     
  4. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Lemme first admit that i have never gone thru the documentaion.. have always referred books.. thats it...Request you to pls be patient with my questions

    When i worked on my VC++ complier I found this:
    int *p;
    p= malloc(8);
    No errors
    ----------------------------------------
    int *p;
    p= malloc(6);
    Run-time error
    -----------------------------------------
    Do I have to always use sizeof() with malloc? Cannot I allocate an integer array of 4 bytes with the following code assuming int on my system occupies 4 bytes?
    int *p;
    p=malloc(16);

    assume, p=0; then is p+1=4 ? or p+1= 1?
    If p+1=4 then what is the difference between calloc and malloc except for 0-byte filling.
    ---------------------------------------------------------------------------------------------------

    Hope my questions are not silly.. :(

    Swapna
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Why dont you try the following as share with us what happens?

    int *p;
    p= calloc(6);
     
  6. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    #include<stdlib.h>
    void main()
    {
    int *p;
    p= calloc(6);
    }
    -------------------Configuration: swap - Win32 Debug--------------------
    Compiling...
    swap.c
    c:\documents and settings\sharmask\swap.c(5) : error C2198: 'calloc' : too few actual parameters
    Error executing cl.exe.

    swap.obj - 1 error(s), 0 warning(s)
    ------------------------------------------------------------------------------------------------------
    That says it all...
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    It isn't that your questions are silly, it's that your attitude is silly. Tutorial material is to give you a view of how to use the language. Reference material is to give you a precise lesson on how to use a specific part of the language. You need to look up malloc and you need to look up calloc. Look at the declarations. Look at the return type and the arguments to see what their types are, and what their values are for. Read the explanatory material concerning them. Read about their gotchas and how they tell you that they failed, when they fail.

    It is silly, when you're reading a book, to pop into a forum and ask what a word means. Use the dictionary. It comes in the form of pressing the help key, or typing "man malloc", or typing "malloc" into a Google search bar. Learning to do that is part of being a big boy.

    When you have done that, and still can't seem to understand malloc/calloc, or have some problem using them, I will be glad to answer your questions.
     
    Last edited: Apr 19, 2007
  8. swapnaoe

    swapnaoe New Member

    Joined:
    Oct 26, 2006
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hello DaWei,
    Yup u r right... I did search many websites for a clear explanation on these functions.. but i was not satisfied with their explanation. All the explanations gave information only on the syntax and their differences.
    I always try to give my max effort for a problem. When i am helpless and exhaustive i seek help in forums.
    Please try to help me out... Liked your last line.. :)
    "When you have done that, and still can't seem to understand malloc/calloc, or have some problem using them, I will be glad to answer your questions."
     
  9. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    See Swapna, M not much with VC++, but i can very well tell you about malloc and calloc in Turbo C.


    int *p;
    p=(int*)malloc(sizeof(int));

    this statement will allocate 2 bytres of memory to p and will return a void pointer which is then typecasted by (int*).
    to allocate memory to a block using malloc requires looping of statement....
    This allocation is successful tries to allocate memory sequentiall but if it fails returns a null pointer.


    Calloc:

    int *p;
    p=(in t*)calloc(n,sizeof(int));
    n represents no. of blocks.
    this is useful to allocate memory to a block in a single statement.
    Both alocates memory sequentiall......

    I hope that this would clear your doubts. Although i have explainned in Turbo, concepts would be same in VC++ too, i think.......
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    No loop is required to allocate a block of memory.

    iPtr = (int *) malloc (64 * sizeof (int)); will allocate a block of 64 integers, regardless of their size on the system in question.

    Since there are typically 16 or more bytes of memory overhead to manage a malloc'd block, it is inadvisable to allocate dynamic memory in small chunks.
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    The major difference is what is told in the MSDN
     
  12. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Is it compulsory or a must to cast the return value to a specify data type ?

    Thanks for your explanation.
     
  13. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Ummmmm, read post #2?
     
  14. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    I have read the post but i don't understand because of my poor english. I am a Malaysian.

    Thanks for oyur patient and help.

    Your help is greatly appreciated by me and others.
     

Share This Page