Re: Difference?

From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: 02/24/04


Date: Mon, 23 Feb 2004 23:20:27 -0800

mohanasundaram@msn.com (Mohanasundaram) writes:

> After posting this question I did some search and read somewhere that
> malloc is used for allocating memory for an object and calloc for
> allocating memory for an array of objects. The internal difference is
> that malloc will align the memory assuming that the memory allocated
> is for a single object and calloc will align the memory assuming that
> it is an array of objects. I think it makes more sense because why
> should there exists two calls both returning pointers to memory
> allocated which can be used in similar way with no other big
> difference.

Although I've seen similar claims, they're all false. The memory
allocated by malloc() is properly aligned for any object or array
of objects. Here is what the standard says:

     The order and contiguity of storage allocated by successive
     calls to the calloc, malloc, and realloc functions is
     unspecified. The pointer returned if the allocation succeeds
     is suitably aligned so that it may be assigned to a pointer
     to any type of object and then used to access such an object
     or an array of such objects in the space allocated (until
     the space is explicitly deallocated).

-- 
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Relevant Pages

  • Re: Whats the substitution of brk system call in POSIX standard?
    ... malloc() and mmap, I guess. ... imagine them being portable to OSes with different memory layouts. ... mallocis a library routine and is originally implemented with brk() ... By allocating from a continous ...
    (comp.unix.programmer)
  • Re: J4 - presentation/discussion on "Future of the COBOL Standard"
    ... there is obviously no problem with allocating the maximum ... array size: all that's actually needed in the compiled program is a ... gigantic physical memory spaces are no longer news. ... compilers that actually allocate space as part of the compiled unit will ...
    (comp.lang.cobol)
  • Re: Big arrays (confusion over FAQ 6.14)
    ... of an array you can create. ... Normally the amount of memory avai- ... if I am not completely off the mark the C89 standard doesn't re- ... wrote code that mallocs 50,000,000 bytes as a test and the malloc call ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... Malcolm McLean wrote, On 19/01/08 10:47: ... Imagine you've got 2GB installed and are allocating 20 bytes. ... The system is stressed and programs crash or terminate for lack of memory once a day. ... The chance of the computer breaking during this period is so so much higher, there is in this case no point checking the malloc(). ...
    (comp.lang.c)
  • Re: Better object orientation?
    ... the GC runs freeing up whatever memory it can (the GC is set up to run at most once per second, and any faster allocation results in simply expanding the heap). ... so, as first, the loop was pulling off about a 100MB/s allocation rate (allocating about 100MB between each GC pass). ... added "large object" allocator (currently, everything larger than 6kB is declared a 'large object', and thus the underlying memory is grabbed from malloc). ...
    (comp.std.c)