Dynamic Memory allocation

S

smarty

how can I find the memory allocated dynamically? is there any
possibility of finding it?
 
J

Jim Langston

smarty said:
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.
 
J

Jens Thoms Toerring

smarty said:
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
 
S

Spiros Bousbouras

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) ;
 
K

Keith Thompson

smarty said:
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".
 
C

cr88192

smarty said:
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).
 
N

Nick Keighley

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

smarty

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

Ian Collins

smarty said:
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.
 
S

Szabolcs Borsanyi

smarty said:
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>
 
K

Keith Thompson

Szabolcs Borsanyi said:
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.
 
S

Szabolcs Borsanyi

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
 
K

Keith Thompson

Szabolcs Borsanyi said:
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.)
 
S

sandy

smarty wrote:

... snip ...


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

sandy

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

Jim Langston

smarty said:
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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top