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

Dynamic Memory allocation

P: n/a
how can I find the memory allocated dynamically? is there any
possibility of finding it?
Jun 27 '08 #1
Share this Question
Share on Google+
19 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.