0

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.

Ian Abbott
  • 15,083
  • 19
  • 33
  • 3
    Yes, it is a pointer to a block of memory that is linear and contiguous. The size of that block is determined by the function parameter that was passed to `malloc`. – Andreas Wenzel Aug 11 '20 at 14:45
  • why you dont read the man7 describing the `malloc` system call? Anyway The `malloc()` function allocates `size` bytes (which passed at call time) and returns a pointer to the allocated memory. it could be an array of integers or array of structs or any thing else! The memory is not initialized. keep reading here [malloc_man7](https://man7.org/linux/man-pages/man3/malloc.3.html) – Adam Aug 11 '20 at 14:52

5 Answers5

1

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

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

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

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.

  1. You cant use `sizeof operator to get the size of that memory chunk.
  2. the reference of the pointer is not the same as reference to the memory chunk. (address of the array always give the reference to first element of the array)
0___________
  • 60,014
  • 4
  • 34
  • 74
0

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • How block of memory being the array is distinct from the one allocated by the "malloc"? – 0___________ Aug 11 '20 at 15:01
  • @P__J__ same by it's properties, just as you answered. :) – Sourav Ghosh Aug 11 '20 at 15:11
  • Nope, you can't distinguish a pointer to an array as local or global variable from a pointer to a memory chunk returned by `malloc`. Of course the memory regions of heap, stack, and data regions differ. But that's implementation dependend. – harper Aug 11 '20 at 15:12
  • I did not write about the memory chunk, which does not have any properties. – 0___________ Aug 11 '20 at 15:12
  • @harper that is what I do not like in this answer. Memory chunk does not have any properties. – 0___________ Aug 11 '20 at 15:14
  • OK, about that, it's the way to access the memory. The memory block does not have any properties, true. I wanted to make it clear that the returned pointer cannot be used as an array variable. – Sourav Ghosh Aug 11 '20 at 15:17
-1

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.

Default value of malloc

Why malloc may be indeterminate from SO

More about calloc and malloc from SO

humble_barnacle
  • 460
  • 1
  • 4
  • 19