473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

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

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
7 3005
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
malloc and new call HeapAlloc on MSWindows.

-JKop
Jul 22 '05 #3
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
0
by: Sharath | last post by:
Quality Globe is Glad to Offer you the Fast Track course on Automation, QTP Basics and Advanced, and Quality Center Starting Date: June 4th, 2007 Timings: 10 AM to 3:30 PM Duration: 50 Hours ...
0
by: Sharath | last post by:
"Inspired" by the huge success of our first two automation fast track batches We are forced to start third fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first three automation fast track batches We are forced to start fourth fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first four automation fast track batches We are forced to start fifth fast track automation batch ...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.