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

O

Office Drone

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.
 
J

Jack Klein

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.
 
O

Office Drone

Pete Becker said:
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?
 
P

Pete Becker

Office said:
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.
 
B

Billy N. Patton

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

Office said:
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, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top