Gaps in memory

P

Phlip

Gianni said:
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])
 
R

Ruslan Abdikeev

Phlip said:
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.
 
R

Richard Cavell

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.
 
S

Sulsa

Richard said:
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
 
P

Phil Staite

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top