Connecting Tech Pros Worldwide Help | Site Map

Gaps in memory

Sulsa
Guest
 
Posts: n/a
#1: Jul 23 '05
Does memory allocated by opperator new has gaps, or is it one big block
of memory??
Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Gaps in memory


Sulsa wrote:[color=blue]
> Does memory allocated by opperator new has gaps, or is it one big block
> of memory??[/color]

One contiguous block.

Ruslan Abdikeev
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Gaps in memory



"Phlip" <phlip_cpp@yahoo.com> wrote in message
news:xMTPd.5501$hd7.2570@newssvr31.news.prodigy.co m...[color=blue][color=green][color=darkred]
>> > Does memory allocated by opperator new has gaps, or is it one big block
>> > of memory??[/color]
>> One contiguous block.[/color]
>
> 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])[/color]

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.


Phlip
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Gaps in memory


Gianni Mariani wrote:[color=blue]
> Sulsa wrote:[color=green]
> > Does memory allocated by opperator new has gaps, or is it one big block
> > of memory??[/color]
>
> One contiguous block.[/color]

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/bi...UserInterfaces


Richard Cavell
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Gaps in memory


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

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.
Sulsa
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Gaps in memory


Richard Cavell wrote:[color=blue]
> On 14/2/05 10:43 AM, Sulsa wrote:
>[color=green]
>> Does memory allocated by opperator new has gaps, or is it one big
>> block of memory??[/color]
>
>
> 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.[/color]

ok, thanks to everyone for your help
Phil Staite
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Gaps in memory


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:[color=blue]
> Does memory allocated by opperator new has gaps, or is it one big block
> of memory??[/color]
Closed Thread