Re: Difference?
From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: 02/24/04
- Next message: Richard Heathfield: "Re: Comments requested: brief summary of C"
- Previous message: Mohanasundaram: "Re: Difference?"
- In reply to: Mohanasundaram: "Re: Difference?"
- Next in thread: Stephen S M WONG: "Re: Difference?"
- Reply: Stephen S M WONG: "Re: Difference?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Richard Heathfield: "Re: Comments requested: brief summary of C"
- Previous message: Mohanasundaram: "Re: Difference?"
- In reply to: Mohanasundaram: "Re: Difference?"
- Next in thread: Stephen S M WONG: "Re: Difference?"
- Reply: Stephen S M WONG: "Re: Difference?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|