Gaps in memory 
July 23rd, 2005, 01:51 AM
| | | |
Does memory allocated by opperator new has gaps, or is it one big block
of memory?? | 
July 23rd, 2005, 01:51 AM
| | | | 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. | 
July 23rd, 2005, 01:51 AM
| | | | 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. | 
July 23rd, 2005, 01:51 AM
| | | | 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 | 
July 23rd, 2005, 01:51 AM
| | | | 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. | 
July 23rd, 2005, 01:52 AM
| | | | 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 | 
July 23rd, 2005, 01:55 AM
| | | | 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] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|