Basic question about malloc
Abhishek Bist
ishubist at gmail.com
Thu Apr 23 01:53:56 EDT 2015
On Thu, 23 Apr 2015 09:52:56 +0800, 慕冬亮 said :
I'm looking for that whether the object allocated by malloc is the
multiple of certain bytes!
Or how does the malloc allocate dynamic memory ??
I does an experiment in my computer!
#include<stdio.h>
#include<stdlib.h>
#define NUM 33
int main(int argc,const char *argv[])
{
char *a, *b, *c;
a = malloc(NUM);
b = malloc(NUM);
c = malloc(NUM);
printf("a = %x\n",a);
printf("b = %x\n",b);
printf("c = %x\n",c);
free(a);
free(b);
free(c);
return 0;
}
But the result has no feature about a ,b ,c!
Can someone tell me what's wrong with me?
My GCC version is gcc (Debian 4.9.2-10) 4.9.2
mudongliang
Hey,
ASFAIK you are looking for the number of bytes allocated to you after calling malloc.First of all malloc is not a function of a
kernel space. The dynamic memory allocation of a memory are generally done by brk(),sbrk(),mmap() etc .And you could easily check
these using a strace tools.[ strace ./a.out ].And in general the number fo bytes allocated to you from a pool is mentioned just
8 bytes behind (in case of character) the starting address allocated to you and you could check it by just derefrencing it.It is a
location which is generally fetched by the free call while freeing a memory from a pool.But there are a memory leakages in case of
dynamic memory allocation in C.
Please, correct me if I am wrong.
And i am bit uncertain about the so called " featuring of a, b and c"
More information about the Kernelnewbies
mailing list