Dynamic Memory allocation

Discussion in 'C Programming' started by smarty, May 13, 2008.

  1. smarty

    smarty Guest

    how can I find the memory allocated dynamically? is there any
    possibility of finding it?
     
    smarty, May 13, 2008
    #1
    1. Advertising

  2. smarty

    Jim Langston Guest

    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
     
    Jim Langston, May 13, 2008
    #2
    1. Advertising

  3. smarty <> 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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 13, 2008
    #3
  4. On 13 May, 11:22, "Malcolm McLean" <> wrote:
    > "smarty" <> 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) ;
     
    Spiros Bousbouras, May 13, 2008
    #4
  5. smarty <> 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) <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"
     
    Keith Thompson, May 13, 2008
    #5
  6. smarty

    cr88192 Guest

    "smarty" <> wrote in message
    news:...
    > 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).
     
    cr88192, May 14, 2008
    #6
  7. On 13 May, 11:08, smarty <> 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
     
    Nick Keighley, May 14, 2008
    #7
  8. smarty

    smarty Guest

    On May 13, 8:59 pm, Keith Thompson <> wrote:
    > smarty <> 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?
     
    smarty, May 14, 2008
    #8
  9. smarty

    Ian Collins Guest

    smarty wrote:
    > On May 13, 8:59 pm, Keith Thompson <> wrote:
    >> smarty <> 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.
     
    Ian Collins, May 14, 2008
    #9
  10. > 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>
     
    Szabolcs Borsanyi, May 14, 2008
    #10
  11. Szabolcs Borsanyi <-heidelberg.de> writes:
    >> 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) <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"
     
    Keith Thompson, May 14, 2008
    #11
  12. >> 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
     
    Szabolcs Borsanyi, May 14, 2008
    #12
  13. Szabolcs Borsanyi <-heidelberg.de> writes:
    >>> 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) <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"
     
    Keith Thompson, May 14, 2008
    #13
  14. smarty

    sandy Guest

    On May 14, 9:35 pm, CBFalconer <> wrote:
    > 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**
     
    sandy, May 14, 2008
    #14
  15. smarty

    sandy Guest

    On May 15, 3:13 am, "Malcolm McLean" <> wrote:
    > "sandy" <> wrote> 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.
    >
    > --


    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.
     
    sandy, May 15, 2008
    #15
  16. smarty

    Jim Langston Guest

    smarty wrote:
    > On May 13, 8:59 pm, Keith Thompson <> wrote:
    >> smarty <> 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?


    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
     
    Jim Langston, May 15, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Hartmann

    structs and dynamic memory allocation

    Roman Hartmann, Nov 4, 2003, in forum: C Programming
    Replies:
    11
    Views:
    522
    Roman Hartmann
    Nov 5, 2003
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    720
    Eric Sosman
    Mar 22, 2005
  3. Ken
    Replies:
    24
    Views:
    3,901
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,207
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page