Hi
When we use malloc then the memory allocated is continuous or not?
Is there anyway to know it?
Hi
When we use malloc then the memory allocated is continuous or not?
Is there anyway to know it?
It is continous only why because u are giving array type var......
Yes malloc() allocates memory in contiguous location.
To verify it see this program
#include<stdio.h>
void main()
{
int *p;
int i;
p=(int *)malloc(5);
i=1;
while(i <5)
printf("\t%u",p++);
}
Now depending upon the computer processor architecture(32,64 bit).the specified size of memory would be allocated for an integer. Suppose 32 bit machine (sizeof of int = 2 bytes) and starting location of allocated space be 1000.
Then the output would be 1000 1002 1004 1006 1008
Memory allocated by individual malloc() calls should be contiguous. That doesn't hold for multiple calls, though. For example, given the code
all 10 bytes of p should be contiguous and all 20 bytes of q should be contiguous, but there's no guarantee that p and q form a contiguous 30-byte block.Code:char *p = malloc(10); char *q = malloc(20);