473,395 Members | 2,443 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,395 software developers and data experts.

Dynamic Memory allocation

how can I find the memory allocated dynamically? is there any
possibility of finding it?
Jun 27 '08 #1
19 1660
smarty wrote:
how can I find the memory allocated dynamically? is there any
possibility of finding it?
It depends on what you mean. Compilers and OSes are pretty much free to
handle dynamic memory allocations any way they wish as long as they conform
to the standard.

The simplest way, of course, is to simply use the pointer returned by a
function such as malloc
char* Buffer;
Buffer = malloc( 100 );

Make sure you call free on buffer.

Other this this you'll either need to specify what it is you are actually
trying to achieve. It may be that you are refereing to dynamic memory that
the OS deals with, which is very OS specfic.
--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #2

"smarty" <cs*******@gmail.comwrote in message
how can I find the memory allocated dynamically? is there any
possibility of finding it?
unsigned char *ptr;

ptr = malloc(100); /* grab 100 bytes of memory */

printf("%p\n", ptr); /* print out the location of the memory in
human-readable format */

Note that this information is almost always useless to you. As long as you
know that ptr points to 100 bytes, there is no reason to know where in
physical memory it is stored.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 27 '08 #3
smarty <cs*******@gmail.comwrote:
how can I find the memory allocated dynamically? is there any
possibility of finding it?
You shouldn't have to find it, you always must keep the pointers
you got when you allocated the memory. That's the only safe way
to know.

You may find that on some systems there are system-specific
methods to get a list of memory allocated by your program.
But that could also be memory that has been allocated by
libraries you use etc. (and you're always using at least
the C library) and which you should neither use direcctly
nor free yourself.

What exactly is the problem you have that would require you
to find allocated memory for which you didn't got a pointer?
If it's just because you lost track of memory you allocated
then the only reasonable answer is: repair your program so
that this can't happen.
Best regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #4
On 13 May, 11:22, "Malcolm McLean" <regniz...@btinternet.comwrote:
"smarty" <csmgsa...@gmail.comwrote in message
how can I find the memory allocated dynamically? is there any
possibility of finding it?

unsigned char *ptr;

ptr = malloc(100); /* grab 100 bytes of memory */

printf("%p\n", ptr); /* print out the location of the memory in
human-readable format */
This should be printf("%p\n", (void *)ptr) ;

Jun 27 '08 #5
smarty <cs*******@gmail.comwrites:
how can I find the memory allocated dynamically? is there any
possibility of finding it?
Your question is unclear; I can think of several things you might be
asking. Do you want to find out *how much* memory has been allocated
dynamically? Do you want to determine the address of a particular
chunk of dynamically allocated memory? Or of all dynamically
allocated memory? Just within your program or elsewhere?

Please post again, stating your question more clearly, and tell us
*why* you wnat to do whatever it is you're trying to do. It's very
likely that the answer is going to be either "You can't do it", or
"You can't do it portably", or "You allocated it, you have to keep
track of it".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jun 27 '08 #6

"smarty" <cs*******@gmail.comwrote in message
news:74**********************************@p25g2000 pri.googlegroups.com...
how can I find the memory allocated dynamically? is there any
possibility of finding it?
not really, unless you write your own allocator (or use one with these
capabilities).
now, the above may seem silly, but I will claim that there "are" valid
reasons for having ones' own allocator(s). in my case, it is for purposes of
garbage collection and dynamic typing.

now, these allocators tend not to be "do everything" allocators (and thus
don't really "replace" malloc), but are more often specialized for specific
tasks and expected usage patterns (object size and density, allocation/free
pattern, object lifespan, ...).

so, one may end up with any number of such specialized allocators located
throughout a project (some of which may infact be built on top of malloc,
which may infact be a fairly simple and effective way of adding specialized
functionality).

Jun 27 '08 #7
On 13 May, 11:08, smarty <csmgsa...@gmail.comwrote:
how can I find the memory allocated dynamically? is there any
possibility of finding it?
maybe wrapper malloc() and free() and store info
about who what was allocated.

--
Nick Keighley

Jun 27 '08 #8
On May 13, 8:59 pm, Keith Thompson <ks...@mib.orgwrote:
smarty <csmgsa...@gmail.comwrites:
how can I find the memory allocated dynamically? is there any
possibility of finding it?

Your question is unclear; I can think of several things you might be
asking. Do you want to find out *how much* memory has been allocated
dynamically? Do you want to determine the address of a particular
chunk of dynamically allocated memory? Or of all dynamically
allocated memory? Just within your program or elsewhere?

Please post again, stating your question more clearly, and tell us
*why* you wnat to do whatever it is you're trying to do. It's very
likely that the answer is going to be either "You can't do it", or
"You can't do it portably", or "You allocated it, you have to keep
track of it".
I came across a situation where i have to find the availability of
free dynamic memory that can be "malloc"ed. May be this is what a
memory manager does when malloc is called. Can i implement this as a
program? How?
Jun 27 '08 #9
smarty wrote:
On May 13, 8:59 pm, Keith Thompson <ks...@mib.orgwrote:
>smarty <csmgsa...@gmail.comwrites:
>>how can I find the memory allocated dynamically? is there any
possibility of finding it?

Please post again, stating your question more clearly, and tell us
*why* you wnat to do whatever it is you're trying to do. It's very
likely that the answer is going to be either "You can't do it", or
"You can't do it portably", or "You allocated it, you have to keep
track of it".

I came across a situation where i have to find the availability of
free dynamic memory that can be "malloc"ed. May be this is what a
memory manager does when malloc is called. Can i implement this as a
program? How?
You'll have to ask on a group for your platform, there isn't any
standard way to do this.

--
Ian Collins.
Jun 27 '08 #10
smarty wrote:
>
.... snip ...
>
I came across a situation where i have to find the availability
of free dynamic memory that can be "malloc"ed. May be this is
what a memory manager does when malloc is called. Can i implement
this as a program? How?
Just malloc what you need and check the returned value. If it is
NULL that memory is not available.

Some non-standard systems used to have routines to return the
amount available. Today, most systems use virtual memory, so there
is no practical limit.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #11
smarty wrote:
>
Just malloc what you need and check the returned value. If it is
NULL that memory is not available.
I would not do that. The standard has no guarantee for a correct
program to run,
just that if it runs, then the output is correct.
<off>
Modern systems (like linux) tend to be overoptimistic in the malloc
call and they
check only the availability of the addressing space, but not the
physical
memory or swap space. On the first write the kernel will think about
how to
acquire the memory, and kill someone if it does not succeed otherwise.

As for me, I usually ask the system of the total memory (there are
system calls
on most platforms that return this information) and my programs assume
that
all (or 80%) is available. And systems with lazy allocation take that
quite well.
</off>
Jun 27 '08 #12
Szabolcs Borsanyi <bo******@thphys.uni-heidelberg.dewrites:
>smarty wrote:

Just malloc what you need and check the returned value. If it is
NULL that memory is not available.
I would not do that. The standard has no guarantee for a correct
program to run,
just that if it runs, then the output is correct.
<off>
Modern systems (like linux) tend to be overoptimistic in the malloc
call and they
check only the availability of the addressing space, but not the
physical
memory or swap space. On the first write the kernel will think about
how to
acquire the memory, and kill someone if it does not succeed otherwise.

As for me, I usually ask the system of the total memory (there are
system calls
on most platforms that return this information) and my programs assume
that
all (or 80%) is available. And systems with lazy allocation take that
quite well.
</off>
Can you please format your text to a maximum of, say, 72 columns?
It appears that you're writing lines up to about 81 columns,
and something somewhere is shortening them to about 70 columns.
The result is the alternating long and short lines seen above,
which are difficult to read.

If you're on a Unix-like system, you can filter your paragraphs
through something like "fmt -72" or "fmt -70", or perhaps your
text editor or newsreader has a "fill" command.

Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #13
>Can you please format your text to a maximum of, say, 72 columns?
It appears that you're writing lines up to about 81 columns,
and something somewhere is shortening them to about 70 columns.
The result is the alternating long and short lines seen above,
which are difficult to read.
Sorry, I forgot about that.
Apologies
Szabolcs
Jun 27 '08 #14
Szabolcs Borsanyi <bo******@thphys.uni-heidelberg.dewrites:
>>Can you please format your text to a maximum of, say, 72 columns?
It appears that you're writing lines up to about 81 columns,
and something somewhere is shortening them to about 70 columns.
The result is the alternating long and short lines seen above,
which are difficult to read.

Sorry, I forgot about that.
Apologies
Szabolcs
You also forgot to leave the attribution line in place. (I'm a bit
sensitive about that, since one poster here removes attribution lines
deliberately.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #15
Szabolcs Borsanyi wrote:
>smarty wrote:

Just malloc what you need and check the returned value. If it is
NULL that memory is not available.

I would not do that. The standard has no guarantee for a correct
program to run,
smarty did not write anything above. I wrote the portion you
quoted. Please take care with your attribution snipping, and
ensure that quoted material is properly attributed.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #16
On May 14, 9:35 pm, CBFalconer <cbfalco...@yahoo.comwrote:
smarty wrote:

... snip ...
I came across a situation where i have to find the availability
of free dynamic memory that can be "malloc"ed. May be this is
what a memory manager does when malloc is called. Can i implement
this as a program? How?

Just malloc what you need and check the returned value. If it is
NULL that memory is not available.

Some non-standard systems used to have routines to return the
amount available. Today, most systems use virtual memory, so there
is no practical limit.
What do you mean by "most systems use virtual memory, so there is no
practical limit" ?
Even virtual memory has a limit ? Will it not be restricted by the
size of your address space, PRACTICALLY ?
And don't you think, your heap will be full and will return a NULL
over a period of continuous memory allocations.

Kindly correct me if I am wrong somewhere.

Regards,
SandeepKsinha.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted fromhttp://www.teranews.com**
Jun 27 '08 #17

"sandy" <sa***********@gmail.comwrote
What do you mean by "most systems use virtual memory, so there is no
practical limit" ?
He means the amount of memory available is so large in relation to the
amount that is likely to be requested that there will never be a problem.
He's right in so far as if you run typical 1990s programs like
wordprocessors or spreadsheets on 2008 machines, the memory take is tiny.
However people are always finding new things to do with computers, like
embedding little video clips in personal webpages, and so the resources tend
to be used.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #18
On May 15, 3:13 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"sandy" <sandeepksi...@gmail.comwroteWhat do you mean by "most systems use virtual memory, so there is no
practical limit" ?

He means the amount of memory available is so large in relation to the
amount that is likely to be requested that there will never be a problem.
He's right in so far as if you run typical 1990s programs like
wordprocessors or spreadsheets on 2008 machines, the memory take is tiny.
However people are always finding new things to do with computers, like
embedding little video clips in personal webpages, and so the resources tend
to be used.

--
I am not arguing, but not convinced completely.
When you are talking about today's machine memory allocation
policies,
then why would you consider the memory hoppers ( Applications ) of
1990's.

I think, the demand and supply should be in today's context and in
sync.
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
Regards,
SandeepKsinha.
Jun 27 '08 #19
smarty wrote:
On May 13, 8:59 pm, Keith Thompson <ks...@mib.orgwrote:
>smarty <csmgsa...@gmail.comwrites:
>>how can I find the memory allocated dynamically? is there any
possibility of finding it?

Your question is unclear; I can think of several things you might be
asking. Do you want to find out *how much* memory has been allocated
dynamically? Do you want to determine the address of a particular
chunk of dynamically allocated memory? Or of all dynamically
allocated memory? Just within your program or elsewhere?

Please post again, stating your question more clearly, and tell us
*why* you wnat to do whatever it is you're trying to do. It's very
likely that the answer is going to be either "You can't do it", or
"You can't do it portably", or "You allocated it, you have to keep
track of it".

I came across a situation where i have to find the availability of
free dynamic memory that can be "malloc"ed. May be this is what a
memory manager does when malloc is called. Can i implement this as a
program? How?
I once heard of one implementation to find out how much memory could be
malloced, they called malloc with a HUGE number and checked for error. They
released any memory that was actually allocated and kept chaning the number
til they found the largest amount they could allocate.

I was always suspicious of this method especially with page swapping and
such and the OS would be paging things in and out of memory for all the
memory allocations.

One method may be to simply determine how much memory you are going to need
on the outside, try to malloc it. If it is malloced release it and
continue. This may adversely effect memory management with memory holes and
such, I don't know. It depends a lot of your OS and how it handles dynamic
memory allocation requests.
--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.