All of lore.kernel.org
 help / color / mirror / Atom feed
* Basic question about malloc
@ 2015-04-23  5:53 Abhishek Bist
  0 siblings, 0 replies; 3+ messages in thread
From: Abhishek Bist @ 2015-04-23  5:53 UTC (permalink / raw)
  To: kernelnewbies

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"

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Basic question about malloc
  2015-04-23  1:52 慕冬亮
@ 2015-04-23  3:03 ` Valdis.Kletnieks at vt.edu
  0 siblings, 0 replies; 3+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2015-04-23  3:03 UTC (permalink / raw)
  To: kernelnewbies

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!

1) malloc() is a userspace function, not kernel.

2) There are multiple malloc() implementations out there - the one you're
using is *probably* glibc's, but there are others (including debugging
memory allocators that return strictly the number of bytes allocated, with
canaries on either end to trap misuse of memory).

> But the result has no feature about a ,b ,c!

What do you mean by "no feature", and what were you expecting? When I run it, I
get:

% ./a.out
a = f35010
b = f35040
c = f35070

which tells me that malloc() returned 3 pointers that are 48 bytes
apart (which is *not* the same thing as "there are 48 bytes safe to use").
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150422/7387473f/attachment.bin 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Basic question about malloc
@ 2015-04-23  1:52 慕冬亮
  2015-04-23  3:03 ` Valdis.Kletnieks at vt.edu
  0 siblings, 1 reply; 3+ messages in thread
From: 慕冬亮 @ 2015-04-23  1:52 UTC (permalink / raw)
  To: kernelnewbies

Hi,
     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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150423/4e083a0d/attachment.html 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-04-23  5:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-23  5:53 Basic question about malloc Abhishek Bist
  -- strict thread matches above, loose matches on Subject: below --
2015-04-23  1:52 慕冬亮
2015-04-23  3:03 ` Valdis.Kletnieks at vt.edu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.