Thread: Trouble understanding Malloc, Calloc, and Realloc

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by matthayzon89 View Post
    so, calloc usually comes in handy when I want to allocate memeory for an array of structs and initialize all the elements to zero at the same time, basically???

    thanks
    The result of both the malloc(numelements * sizeof(element)) and calloc(numelements, sizeof(element)) is likely to be the same pointer. But malloc does not initialize memory so you may be left with a rather sizeable field of garbage. In some cases it won't matter, but sometimes it will.

    calloc() is handy when you want to be sure memory is "cleared" before processing. There are cases where (for example) not all elements in a struct are assigned values right away. These may be added by subsequent procedures or left entirely blank (it's common practice in Windows programming, btw) so you want to be sure they are not left with whatever garbage was in memory when you created them.

    For the question about 0 and null ... 0 is a number, null or NULL is a void pointer to memory address 0. It may be compiler dependent but there are situations where the distinction does matter. Best practice would seem to indicate that when testing pointers test them against NULL or null (depending on your compiler) and save the 0 for numbers.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Regarding 0 vs NULL: A portable program must treat them identically, which basically means assume that NULL is 0. The distinction rarely matters, however (off the top of my head the only thing I can think of is passing it as part of a variable argument list, or to a non-prototyped function).

    if(pointer == NULL) is always the same as if(pointer == 0), and the same as if(!pointer). It doesn't matter how a null pointer is represented on a system. The same goes for pointer = NULL vs pointer = 0; they are the same.

    I would recommend using NULL explicitly because it makes more sense semantically. But it's a matter of taste rather than syntactic correctness.

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @cas, you are completely right!
    @OP, you should learn to find and read doc.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    it is better to say malloc returns NULL on failure rather than 0.
    This is arguably true, but "malloc returns NULL if it fails, not 0" is definitely false, in cases where NULL is defined to be 0 rather than ((void*)0) or something else. If you want to avoid arguing over minor details, just say that malloc returns a null pointer on failure. Whichever null pointer constant is used, so be it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, realloc, calloc
    By newbie30 in forum C Programming
    Replies: 25
    Last Post: 08-13-2009, 01:14 PM
  2. Difference BW malloc() and calloc() and realloc()
    By svelmca in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 05:59 AM
  3. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM