Gaps in memory

Discussion in 'C++' started by Sulsa, Feb 13, 2005.

  1. Sulsa

    Sulsa Guest

    Does memory allocated by opperator new has gaps, or is it one big block
    of memory??
    Sulsa, Feb 13, 2005
    #1
    1. Advertising

  2. Sulsa wrote:
    > Does memory allocated by opperator new has gaps, or is it one big block
    > of memory??


    One contiguous block.
    Gianni Mariani, Feb 13, 2005
    #2
    1. Advertising

  3. Sulsa

    Phlip Guest

    Gianni Mariani wrote:
    > Sulsa wrote:
    > > Does memory allocated by opperator new has gaps, or is it one big block
    > > of memory??

    >
    > One contiguous block.


    For most uses you can consider the block contiguous. However, given...

    Foo * pFoo = new Foo[15];

    ....C++ defines and passes this assertion:

    assert(pFoo + 1 == &pFoo[1]);

    However, C++ often pads data structures such that each aligns to some
    boundary of addresses. So, this assertion is well-formed, but is it always
    true?

    assert(reinterpret_cast<char*>(pFoo) + sizeof *pFoo ==
    reinterpret_cast<char*>(&pFoo[1])

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Feb 14, 2005
    #3
  4. "Phlip" <> wrote in message
    news:xMTPd.5501$...
    >> > Does memory allocated by opperator new has gaps, or is it one big block
    >> > of memory??

    >> One contiguous block.

    >
    > However, C++ often pads data structures such that each aligns to some
    > boundary of addresses. So, this assertion is well-formed, but is it always
    > true?
    >
    > Foo * pFoo = new Foo[15];
    > assert(reinterpret_cast<char*>(pFoo) + sizeof *pFoo ==
    > reinterpret_cast<char*>(&pFoo[1])


    Yes, this assertion is always true, as the size of the object is
    specifically defined to hold it true:
    "When applied to a class, the result is the number of bytes in an object
    of that class
    including any padding required for placing objects of that type in an
    array. [...]
    This implies that the size of an array of n elements is n times the size
    of an element." (5.3.3/2)

    It should be noted that yes, operator new allocates the contiguous block of
    memory, and yes,
    the allocated block can have gaps in the sense of trap bits, immutable areas
    and so on.

    IIRC, "array of unsigned char" is the only type which can be safely treated
    as the truly contiguous block of bits.

    Sincerely yours,
    Ruslan Abdikeev.
    Ruslan Abdikeev, Feb 14, 2005
    #4
  5. On 14/2/05 10:43 AM, Sulsa wrote:
    > Does memory allocated by opperator new has gaps, or is it one big block
    > of memory??


    It can be addressed as one contiguous block.

    An operating system like Windows, which is capable of increasing your
    apparent memory by plonking some of it onto the hard disk, might
    actually create 'gaps' in your allocated memory, but that's transparent
    to your program.
    Richard Cavell, Feb 14, 2005
    #5
  6. Sulsa

    Sulsa Guest

    Richard Cavell wrote:
    > On 14/2/05 10:43 AM, Sulsa wrote:
    >
    >> Does memory allocated by opperator new has gaps, or is it one big
    >> block of memory??

    >
    >
    > It can be addressed as one contiguous block.
    >
    > An operating system like Windows, which is capable of increasing your
    > apparent memory by plonking some of it onto the hard disk, might
    > actually create 'gaps' in your allocated memory, but that's transparent
    > to your program.


    ok, thanks to everyone for your help
    Sulsa, Feb 14, 2005
    #6
  7. Sulsa

    Phil Staite Guest

    Depends on what you mean.

    If you mean simply for one allocation, say:

    char* buffer = new char[1024];

    Then yes, all 1k bytes are contiguous as far as your program/process is
    concerned.

    However, if you do:

    char* alpha = new char[1024];
    char* beta = new char[1024];

    There is no guarantee that alpha and beta are anywhere near one another,
    which one comes "first" (lower address) in memory, etc.

    Finally, if you want to get down into the guts of the machine, on modern
    paged virtual memory systems there's no guarntee that any non-trivial
    sized allocation is actually contiguous in physical memory. (or even in
    the same physical pages after being swapped out and back) The memory
    controller hardware could be mapping virtual memory addresses from your
    process/program to just about anywhere in physical memory. So if you're
    talking about hardware interfaces you'll have to go to your
    OS/environment and find out about "pinning" pages in memory etc.

    -Phil


    Sulsa wrote:
    > Does memory allocated by opperator new has gaps, or is it one big block
    > of memory??
    Phil Staite, Feb 16, 2005
    #7
    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. Arthur Dent
    Replies:
    1
    Views:
    1,053
    Jim Gibson
    Dec 3, 2003
  2. greg
    Replies:
    4
    Views:
    366
  3. Mark247
    Replies:
    1
    Views:
    610
    Marrow
    Sep 3, 2004
  4. Andrey
    Replies:
    6
    Views:
    362
    Andrew Koenig
    Jun 24, 2004
  5. Replies:
    1
    Views:
    529
    Jonathan N. Little
    Nov 7, 2006
Loading...

Share This Page