By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

How can I know size of avaible memory in heap?

P: n/a
How can I know size of avaible memory in heap?
For example :
....
....
// size = N
cout << "Size of Heap = " << SizeOfHeap() << endl;
int* i = new int;
// size = N - sizeof(int)
cout << "Size of Heap = " << SizeOfHeap() << endl;
....
....

SizeOfHeap() == ???

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
If this function is OS/Compiler depend:
I use Linux RH 7.3 and gcc-4.0

Jul 23 '05 #2

P: n/a
On 20 Jun 2005 08:14:52 -0700, "Dima" <di**@inotech.ru> wrote:
How can I know size of avaible memory in heap?
For example :
...
...
// size = N
cout << "Size of Heap = " << SizeOfHeap() << endl;
int* i = new int;
// size = N - sizeof(int)
cout << "Size of Heap = " << SizeOfHeap() << endl;
...
...

SizeOfHeap() == ???


Two questions:

(1) What heap?
(2) What for??

The C++ language has no facilities for checking heap size; this is a
function of the operating system. Your question is therefore off-topic
in this newsgroup.

In general, the process heap will grow as needed. Unless you are doing
very low-level, close to the hardware things, you don't need (and
don't want) to know. If you need to allocate huge amounts of memory,
you will need to use OS-specific ways of allocating virtual memory.
Otherwise, just call new and delete and don't worry.

--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #3

P: n/a
On Mon, 20 Jun 2005 19:25:14 +0400, Dima <di**@inotech.ru> wrote:
If this function is OS/Compiler depend:
I use Linux RH 7.3 and gcc-4.0


Then just go open <malloc.h> and study the functions available to you.

--
Maxim Yegorushkin
Jul 23 '05 #4

P: n/a
Bob Hairgrove is clearly having a bad hair day.
Given that Dima didn't know the answer, how might he possibly know it
was mildly off topic ?
Altough Bob is pedantically correct that C++ doesn't specify how the
heap is managed the sort of problem that Dima is encountering proably
has a direct C++ solution.

I rather suspect that he is trying to write code that deal with low
memory situations.
In which case he might well benefit from learning about overloading new
for some sort of placement new implementation.

It's not just for when you are allocating huge amounts of memory. I
have seen several heap implementations get terribly fragmented and thus
get very slow. This can also lea to false reports of not enough memory.
If Dima is creating many objects of varying size, it is possible he
needs to understand this stuff.

DominiConnor
Quant Headhunter

Jul 23 '05 #5

P: n/a
"Dima" <di**@inotech.ru> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
How can I know size of avaible memory in heap?
For example :
...
...
// size = N
cout << "Size of Heap = " << SizeOfHeap() << endl;
int* i = new int;
// size = N - sizeof(int)
cout << "Size of Heap = " << SizeOfHeap() << endl;
...
...

SizeOfHeap() == ???


One solution to this problem I saw, but I didn't like, just
did malloc with huge amounts and determined when it
failed.

Basically you probably really want to determine if you have
enough heap to do something. If you think you'll need 10
megs of heap you could try allocating 10 megs of memory
in a malloc (remembering to release it if successful) and
check to see if it allowed it.

There is probably a better solution, but I believe it's OS
dependant.
Jul 23 '05 #6

P: n/a


do**********@gmail.com wrote:
Bob Hairgrove is clearly having a bad hair day.
Given that Dima didn't know the answer, how might he possibly know it
was mildly off topic ?
It's an observation, not a judgement (in this case)
although I suspect Dima didn't read the FAQ.

I rather suspect that he is trying to write code that deal with low
memory situations.
In which case he might well benefit from learning about overloading new
for some sort of placement new implementation.


Regular placement new doesn't make a lot of sense in this
context. It doens't care about memory, heap or otherwise.
There are special forms, but those are rather exotic.
Usually you just need an replacement operator new (which
is not an overload either), or an operator new for a
single class.

HTH,
Michiel Salters

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.