By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

Gaps in memory

P: n/a
Does memory allocated by opperator new has gaps, or is it one big block
of memory??
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sulsa wrote:
Does memory allocated by opperator new has gaps, or is it one big block
of memory??


One contiguous block.

Jul 23 '05 #2

P: n/a

"Phlip" <ph*******@yahoo.com> wrote in message
news:xM*****************@newssvr31.news.prodigy.co m...
> 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.
Jul 23 '05 #3

P: n/a
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/bi...UserInterfaces
Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

P: n/a
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
Jul 23 '05 #6

P: n/a
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??

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.