Reputation: 11
I have a doubt. When malloc()
function returns a pointer, is it a pointer to a linear block of memory (similar to array)? Or is it something else?
I would like to know the structure of that memory.
Upvotes: 0
Views: 761
Reputation: 490
It does not return pointer to an array.
malloc(size_t n)
returns a pointer to a contiguous memory location specified by the parameter n
.
Note that it is opposed to calloc(size_t nmem, size_t size)
which initializes each bit to zero while malloc()
leaves the value as indeterminate as suggested in comments.
Why malloc may be indeterminate from SO
More about calloc and malloc from SO
Upvotes: -1
Reputation: 153348
malloc function return pointer to an array right?
size_t n = ...;
void *p = malloc(n);
When the returned pointer is a null pointer, the allocation failed and the pointer should not be dereferenced. It does not need to be free'd.
if (p == NULL) Handle_Failure();
A successful void *malloc(size_t n)
call does return a pointer, a void *
that can be assigned to any object pointer type. A cast is not needed. p
is not a pointer to an array, just a void *
. The allocated memory can be the destination of a copy of any object including arrays.
my_type *p = malloc(n);
Use the pointer to store the contents of an array, int
, or any object. As long as the initial allocation was big enough, it does not matter. The pointer meets the alignment requirements for all object types. When done, free it exactly once. Then do not use the value in the pointer. Yet p
can be re-assigned.
int foo(const struct abc *x) {
struct abc *p = malloc(sizeof *p);
if (p == NULL) {
return 1;
}
*p = *x;
bar(p);
free(p);
return 0;
}
Allocation of 0 bytes is a special case and can be done. Should malloc()
return a null pointer or not, the pointer should not be dereferenced. No *p
.
Once code gets p
, code does not have a portable way to get the size of memory allocated. Code should keep track of the original n
as needed. malloc()
uses a size_t
argument.
Note: free(NULL)
is OK
Upvotes: 1
Reputation: 123448
A pointer only points to a single object of the pointed-to type (although that object may be an aggregate type) or function. That object may be the first object in a larger sequence like an array, but you can't know that from the pointer itself. Given code like
char x;
char *p1 = &x;
char *p2 = malloc( sizeof *p2 * 10 );
There's no way to know from the pointers themselves that p1
points to a single standalone object while p2
points to the first in a sequence of objects. You have to keep track of that information separately.
This is true for pointers to aggregate types like
char a[20];
char (*p3)[20] = &a;
char (*p4)[20] = malloc( sizeof *p4 * 10 );
Same deal as above - both p3
and p4
point to a single 20-element array of char
. In p4
's case, it's pointing to the first in a sequence of 20-element arrays of char
, but again you can't know that from the value of p4
itself.
Note that malloc
and calloc
don't operate in terms of objects, they operate in terms of bytes - you tell them how many bytes of memory you want to reserve, but they have no idea what type of object or sequence of objects is going to occupy that memory. They also need some way to keep track of what's been allocated, so many implementations will reserve some extra memory on each allocation for bookkeeping purposes.
Upvotes: 1
Reputation: 134286
When
malloc();
function returns a pointer is it a pointer to a linear blocks of memory
Yes, and it's a void
pointer.
(similar to array).
No. It's not an array, or in other words, it's not the pointer to the first element of an array of size passed to malloc()
. It's a memory region/ block, virtually contiguous, but the returned pointer (or the variable storing the return value) will not have properties of an array.
would like to know the structure of that memory
If you only want to use the returned memory location, you need not bother. You can just store the returned pointer to a variable of a pointer to complete type, and use that variable to perform operations (read from/ write to) that memory location.
Upvotes: 0
Reputation: 67476
I would like to know the structure of that memory.
There is no structure. Just the memory chunk
(similar to array?)
the memory chunk is exactly the same as array.
The difference is only that reference of that chunk is a pointer.
Upvotes: 1