Home > Archive > Unix Programming > November 2005 > Heap and stack size?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Heap and stack size?
|
|
| Dharma 2005-11-13, 3:57 am |
| hi,
i want to write a c program to find the heap size, stack size. Is
there any way to
find?
thanks,
Dharma
| |
| Pascal Bourguignon 2005-11-13, 7:56 am |
| "Dharma" <dharmalingam@huawei.com> writes:
> hi,
> i want to write a c program to find the heap size, stack size. Is
> there any way to
> find?
In C? The best you can do is:
#include <stdio.h>
static unsigned long base_stack;
static unsigned long base_heap;
unsigned long stack(void){int x=0;return((unsigned long)(&x));}
unsigned long heap(void){void* x=sbrk(sizeof(int));return((unsigned long)x);}
#ifdef STACK_GOES_DOWN_AND_HEAP_GOES_UP
unsigned long stack_size(void){return(base_stack-stack());}
unsigned long heap_size(void){return(heap()-base_heap);}
#endif
void do_something(void){
int x[1000];
int* y=malloc(1000*sizeof(int));
printf("heap size=%lu stack size=%lu\n",heap_size(),stack_size());
free(y);
}
int main(int argc,char** argv){
base_stack=stack();
base_heap=heap();
printf("heap size=%lu stack size=%lu\n",heap_size(),stack_size());
do_something();
printf("heap size=%lu stack size=%lu\n",heap_size(),stack_size());
return(0);
}
/*
-*- mode: compilation; default-directory: "/tmp/" -*-
CFLAGS=-DSTACK_GOES_DOWN_AND_HEAP_GOES_UP=1 make -k hs;./hs
cc -DSTACK_GOES_DOWN_AND_HEAP_GOES_UP=1 hs.c -o hs
hs.c: In function `stack':
hs.c:6: warning: function returns address of local variable
hs.c: In function `heap':
hs.c:7: warning: initialization makes pointer from integer without a cast
hs.c: In function `do_something':
hs.c:15: warning: initialization makes pointer from integer without a cast
heap size=4 stack size=32
heap size=6368 stack size=4064
heap size=6372 stack size=32
Compilation finished at Sun Nov 13 11:48:01
*/
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Maxim Yegorushkin 2005-11-13, 6:58 pm |
|
Pascal Bourguignon wrote:
> "Dharma" <dharmalingam@huawei.com> writes:
>
>
> In C? The best you can do is:
[]
stack_size() won't report properly in other than the thread that
initialized base_stack.
| |
| Dharma 2005-11-15, 7:56 am |
| Iam getting link error for sbrk().
Can you explain me the Concept behind this.?
thanks,
Dharma
| |
| Maxim Yegorushkin 2005-11-15, 7:01 pm |
|
Dharma wrote:
> Iam getting link error for sbrk().
> Can you explain me the Concept behind this.?
You might not have it, since the man page says it's obsolete.
| |
| Gary R. Schmidt 2005-11-16, 3:59 am |
| Maxim Yegorushkin wrote:
> Dharma wrote:
>
>
>
> You might not have it, since the man page says it's obsolete.
>
Obsolete? Since when?
My Solaris 10 system still has it, with no indication of obsolescence in
the man pages.
What OS is it obsolete on?
Cheers,
Gary B-)
--
________________________________________
______________________________________
Armful of chairs: Something some people would not know
whether you were up them with or not
- Barry Humphries
| |
|
|
| Pascal Bourguignon 2005-11-16, 7:58 am |
| "Dharma" <dharmalingam@huawei.com> writes:
> Iam getting link error for sbrk().
> Can you explain me the Concept behind this.?
man brk
man sbrk
The concept is that the kernel allocates memory for the user process
in an increasing addresses order, from the low memory.
It's rather obsolete because nowadays we use more often mmap to
allocate (and deallocate with munmap) heap memory in random places
instead of just growing monotonically.
But mmap is used for a lot of things: shared memory, dynamic
libraries, etc. Sometimes there is an API to get the list of mmaped
blocks, but nothing standard AFAIK. It would be possible to count all
the pages that are mapped (trying to mmap each of them in turn and
counting the already mmaped errors).
On some systems, there is an API or a command to get it. For example,
on Darwin (MacOSX), you can use vmmap(1).
--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
| |
| Gary R. Schmidt 2005-11-16, 7:58 am |
| Maxim Yegorushkin wrote:
> Gary R. Schmidt wrote:
>
>
>
>
> http://www.die.net/doc/linux/man/man2/sbrk.2.html
>
XPGv6 obsoletes the function. Do you understand what that means?
Most commercial OSes will keep it to allow for backward compatibility.
At both binary and source code level.
For that matter, so will the BDSs and Linux.
Did a quick check - it's still in HPUX 11.2, Tru64 5.2, AIX 5.3, Solaris
10, OpenServer 6, UNIXWARE 7, RHAS 2, SUSE 8, and Fedora 4.
Cheers,
Gary B-)
--
________________________________________
______________________________________
Armful of chairs: Something some people would not know
whether you were up them with or not
- Barry Humphries
|
|
|
|
|