Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memory allocated by malloc an array - same as allocated by calloc?

The language of the C standard is sometimes not very clear and consistent.

  1. Is object allocated by malloc an array. Common sense says YES, but the standard uses different wording for calloc and malloc
  • calloc : "The calloc function allocates space for an array of nmemb objects, each of whose size is size."
  • malloc: "The malloc function allocates space for an object whose size is specified by size"

Generally an array is an object too.

I am looking for precise answers - not general common sense deliberations and real-life usages.

like image 534
0___________ Avatar asked Mar 05 '26 18:03

0___________


1 Answers

Yes, both functions return memory for objects with allocated storage duration. This is first mentioned in section 6.2.4p1 of the C standard regarding Storage Duration of Objects:

An object has a storage duration that determines its lifetime. There are four storage durations: static, thread, automatic, and allocated. Allocated storage is described in 7.22.3.

And further in section 7.22.3, where it mentions both array and non-array objects:

The order and contiguity of storage allocated by successive calls to the aligned_alloc, 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 with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

It should also be noted that such memory returned by these functions does not have an effective type until such time when it is written , which essentially means you can store whatever you want in such memory. This is spelled out in section 6.5p6:

The effective type of an object for an access to its stored value is the declared type of the object, if any. 87) If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.

87 ) Allocated objects have no declared type

like image 155
dbush Avatar answered Mar 08 '26 07:03

dbush