Learner
Learner

Reputation: 11

malloc function memory management

malloc() function forms a single block of memory (say 20 bytes typecasted to int), so how it can be used as an array of int blocks like as calloc() function? Shouldn't it be used to store just one int value in whole 20 bytes (20*8 bits)?

Upvotes: 1

Views: 152

Answers (4)

Andreas Hadjigeorgiou
Andreas Hadjigeorgiou

Reputation: 194

What malloc returns back to you is just a pointer to the starting memory address where the contiguous block of memory was allocated.

The size of the contiguous block of memory that you allocated using malloc depends on the argument you passed into malloc function. http://www.cplusplus.com/reference/cstdlib/malloc/

If you want to store int variable then you will do it by defining the pointer type you use to be of an int type.

example:

int p*; //pointer of type integer
size_t size = 20;
p = (int *) malloc(size); //returns to pointer p the memory address

after this, using the pointer p the programmer can access int (4 byte precision) values.

calloc only difference against malloc is that calloc initiallizes all values at this memory block to zero.

Upvotes: 0

Lundin
Lundin

Reputation: 213408

There's an abstract concept in C formally known as effective type, meaning the actual type of the data stored in memory. This is something the compiler keeps track of internally.

Most objects in C have such an effective type at the point when the variable is declared, for example if we type int a; then the effective type of what's stored in a is int.

Meaning it is legal to do evil things like this:

int a;
double* d = (double*)&a;
*(int*)d = 1;

This works because the effective type of the actual memory remains an int, even though we pointed at it with a wildly incompatible type. As long as we access it with the same type as the effective type, all is well. If we access the data using the wrong type, very bad things will happen, such as program crashes or dormant bugs.

But when we call malloc family of functions, we only tell them to reserve n number of bytes, with no type specified. This memory is guaranteed to be allocated in adjacent memory cells, but nothing else. The only difference between malloc and calloc is that the latter sets all values in this raw memory to zero. Neither function knows anything about types or arrays.

The returned chunk of raw memory has no effective type. Not until the point when we access it, then it gets the effective type which corresponds to the type used for the access.

So just as in the previous example, it doesn't matter which type of pointer we set to point at the data. It doesn't matter if we write int* i = malloc(n); or bananas_t* b = malloc(n);, because the pointed-at memory does not yet have a type. It does not get one until at the point where we access it for the first time.

Upvotes: 1

dbush
dbush

Reputation: 223699

There is nothing special about memory returned from malloc compared to memory returned from calloc, other that the fact that the bytes of the memory block returned by calloc are initialized to 0. Memory returned by malloc does not have to be used for a single object but may also be used for an array.

This means that the following are equivalent:

 int *p1 = malloc(3 * sizeof(int));
 p1[0] = 1;
 p1[2] = 2;
 p1[3] = 3;
 ...
 int *p2 = calloc(3, sizeof(int));
 p2[0] = 1;
 p2[2] = 2;
 p2[3] = 3;

Both will return 3 * sizeof(int) bytes of memory which can be used as an array of int of size 3.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

(say 20 bytes typecasted to int)

No, the returned memory is given as a pointer to void, an incomplete type.

We assign the returned pointer to a variable of pointer to some type, and we can use that variable to access the memory.

Quoting C11, chapter §7.22.3, Memory management functions

[....] 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 pointer returned points to the start (lowest byte address) of the allocated space. [....]

Since the allocated memory is contiguous, pointer arithmetic works, just as in case of arrays, since in arrays also, elements are placed in contiguous memory.

One point to clarify, a pointer is not an array.

Upvotes: 6

Related Questions