Thread: What is the difference between these two? in C

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    What is the difference between these two? in C

    hello,
    what is the difference between calloc() and malloc()??in C ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you keep posting in C++ if you have questions about C?
    The difference between them is that calloc fills the memory with 0s, which malloc does not. Also, calloc takes the size of the object you want to allocate and how many of them. Malloc only takes the entire size of the block you want to allocate.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C programming forum.
    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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    malloc() gives no guarantee of the state of the content allocated, calloc() guarantees that it's set to binary zero. [Which for most architectures means that pointers are NULL and that floating point values are zero].

    There is also a difference in the way that amount of memory you allocate is specified.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Basically, this is calloc() defined with malloc().
    Code:
    void *calloc_with_malloc(size_t n, size_t size) {
        void *data = malloc(n * size);
        memset(data, 0, n * size);
        return data;
    }
    calloc() can do things differently, however. For example, I think some platforms can deal with lots of small chunks differently from one big chunk, and calloc() could take advantage of that. Of course, I can't think of any reason for this, since the chunks would have to be contiguous.

    Generally, I use malloc(), even for arrays. I only use calloc() if I need zero-ed memory. I guess you could use calloc() whenever you need memory allocated for an array -- or even when you need just one element zeroed, just as you can use fread() and fwrite() for one block.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM