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

A simple way to track available memory in C/C++ and why is there so many different types of it?

P: n/a
I'm a bit confused about memory usage, and for some reason I wasn't
able to find a single point-of-call to get the amount of memory
available.

If we take, for instance, the Windows platform:

There is
* Virtual memory you can allocate (VirtualAlloc)
* Global memory you can allocate (GlobalAlloc)
* Local memory you can allocate (LocalAlloc)
* Heap memory you can allocate (HeapAlloc)
* Dynamic memory allocated via malloc/calloc
* Memory allocated via new (which I presume is the same malloc)

Why are there so many different types of memory, and what is the best
way to return the amount of memory available to allocs/new?

What I need is to check on memory leaks in my program, since I'm
writing my own memory manager.
Also if there's an ANSI C function to return it cross-platform, it'll
be great if anyone could hint on its name.

Any info from the monsters of C++ will be appreciated.
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 26 Jul 2004 18:01:27 -0700, fu**********@yahoo.com (Office Drone)
wrote in comp.lang.c++:
I'm a bit confused about memory usage, and for some reason I wasn't
able to find a single point-of-call to get the amount of memory
available.

If we take, for instance, the Windows platform:
This newsgroup discusses the C++ language which is defined by a
platform-independent ANSI/ISO/IEC standard. It does not take, for
instance, the Windows platform.
There is
* Virtual memory you can allocate (VirtualAlloc)
* Global memory you can allocate (GlobalAlloc)
* Local memory you can allocate (LocalAlloc)
* Heap memory you can allocate (HeapAlloc)
The four things you mention above are not part of the C++ language and
are not discussed here. You need a Windows programming group to talk
about them.
* Dynamic memory allocated via malloc/calloc
* Memory allocated via new (which I presume is the same malloc)
You presume incorrectly, allocating memory via new or new [] is not at
all the same as allocating it with malloc/calloc/realloc.
Why are there so many different types of memory, and what is the best
way to return the amount of memory available to allocs/new?
There are three types of memory that exist in a C++ program. Static,
automatic, and dynamic. The latter comes from new or
malloc/calloc/realloc. Whatever you get from the Windows API
functions is not a C++ issue.
What I need is to check on memory leaks in my program, since I'm
writing my own memory manager.
After the huge list of memory allocation functions, both standard and
non-standard above, why do you think you need to write your own?
Aren't any of them good enough for you?
Also if there's an ANSI C function to return it cross-platform, it'll
be great if anyone could hint on its name.
This is a C++ newsgroup, and there are significant parts of the
current ANSI/ISO C library that are not part of C++, so why are you
asking about C functions here?

In any case, no, there is no such function in standard C or C++. If
there were, it would be pretty useless in a modern multi-tasking
environment. By the time such a function returned a value to you, the
amount of available memory could have changed.
Any info from the monsters of C++ will be appreciated.


Start by reading the FAQ for this newsgroup, link in my signature.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

P: n/a
malloc and new call HeapAlloc on MSWindows.

-JKop
Jul 22 '05 #3

P: n/a
JKop wrote:

malloc and new call HeapAlloc on MSWindows.


Not so. There may be some libaries that do that, but not all.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4

P: n/a
Pete Becker posted:
JKop wrote:

malloc and new call HeapAlloc on MSWindows.

Not so. There may be some libaries that do that, but not

all.


I see.

-JKop
Jul 22 '05 #5

P: n/a
Pete Becker <pe********@acm.org> wrote in message news:<41***************@acm.org>...
JKop wrote:

malloc and new call HeapAlloc on MSWindows.


Not so. There may be some libaries that do that, but not all.


How do you detect memory leaks then, like when you need to return the
Heap size on that exact moment?
I know other languages have a single function that returns the value
of total memory available to the process. Isn't it not available in C
or C++? I believe it could be the same API call to the kernel for all
of them.

And also, do you know if the heap size (unless set in the hard way)
can extend to the entire virtual memory available?
Jul 22 '05 #6

P: n/a
Office Drone wrote:

Pete Becker <pe********@acm.org> wrote in message news:<41***************@acm.org>...
JKop wrote:

malloc and new call HeapAlloc on MSWindows.

Not so. There may be some libaries that do that, but not all.


How do you detect memory leaks then,


You keep track of the blocks of memory that you've allocated, and when
the application terminates you check whether there are any blocks you
allocated that haven't been freed.
like when you need to return the
Heap size on that exact moment?
That's a completely separate question.
I know other languages have a single function that returns the value
of total memory available to the process.
Maybe. That information is pretty much useless, since it can change
without any action on the part of the application. Unless, of course,
those other languages artificially limit the amount of memory available
to your application, which is possible, but definitely not a good idea.
Isn't it not available in C
or C++? I believe it could be the same API call to the kernel for all
of them.

And also, do you know if the heap size (unless set in the hard way)
can extend to the entire virtual memory available?


At the system level, the amount of memory available to an application
depends on how much memory other applications are using at the moment.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7

P: n/a
Buy yourself a copy of BoundsChecker. It's worth the price.
For our unix platform we have purify. BOth did the job.

Office Drone wrote:
I'm a bit confused about memory usage, and for some reason I wasn't
able to find a single point-of-call to get the amount of memory
available.

If we take, for instance, the Windows platform:

There is
* Virtual memory you can allocate (VirtualAlloc)
* Global memory you can allocate (GlobalAlloc)
* Local memory you can allocate (LocalAlloc)
* Heap memory you can allocate (HeapAlloc)
* Dynamic memory allocated via malloc/calloc
* Memory allocated via new (which I presume is the same malloc)

Why are there so many different types of memory, and what is the best
way to return the amount of memory available to allocs/new?

What I need is to check on memory leaks in my program, since I'm
writing my own memory manager.
Also if there's an ANSI C function to return it cross-platform, it'll
be great if anyone could hint on its name.

Any info from the monsters of C++ will be appreciated.

--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-******@ti.com
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.