Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dynamic Memory allocation

Reply
Thread Tools

Dynamic Memory allocation

 
 
smarty
Guest
Posts: n/a
 
      05-13-2008
how can I find the memory allocated dynamically? is there any
possibility of finding it?
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      05-13-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-13-2008
smarty <(E-Mail Removed)> wrote:
> 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 ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      05-13-2008
On 13 May, 11:22, "Malcolm McLean" <(E-Mail Removed)> wrote:
> "smarty" <(E-Mail Removed)> wrote 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) ;

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-13-2008
smarty <(E-Mail Removed)> writes:
> 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) (E-Mail Removed) <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"

 
Reply With Quote
 
cr88192
Guest
Posts: n/a
 
      05-13-2008

"smarty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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).



 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-14-2008
On 13 May, 11:08, smarty <(E-Mail Removed)> wrote:

> 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

 
Reply With Quote
 
smarty
Guest
Posts: n/a
 
      05-14-2008
On May 13, 8:59 pm, Keith Thompson <(E-Mail Removed)> wrote:
> smarty <(E-Mail Removed)> writes:
> > 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?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-14-2008
smarty wrote:
> On May 13, 8:59 pm, Keith Thompson <(E-Mail Removed)> wrote:
>> smarty <(E-Mail Removed)> writes:
>>> 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.
 
Reply With Quote
 
Szabolcs Borsanyi
Guest
Posts: n/a
 
      05-14-2008
> 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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
structs and dynamic memory allocation Roman Hartmann C Programming 11 11-05-2003 07:53 AM



Advertisments