Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Gaps in memory

Reply
Thread Tools

Gaps in memory

 
 
Sulsa
Guest
Posts: n/a
 
      02-13-2005
Does memory allocated by opperator new has gaps, or is it one big block
of memory??
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-13-2005
Sulsa wrote:
> Does memory allocated by opperator new has gaps, or is it one big block
> of memory??


One contiguous block.

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      02-14-2005
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


 
Reply With Quote
 
Ruslan Abdikeev
Guest
Posts: n/a
 
      02-14-2005

"Phlip" <(E-Mail Removed)> wrote in message
newsMTPd.5501$(E-Mail Removed) 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.


 
Reply With Quote
 
Richard Cavell
Guest
Posts: n/a
 
      02-14-2005
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.
 
Reply With Quote
 
Sulsa
Guest
Posts: n/a
 
      02-14-2005
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
 
Reply With Quote
 
Phil Staite
Guest
Posts: n/a
 
      02-16-2005
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??

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why gaps in my meny in IE? greg HTML 4 01-29-2006 02:19 AM
Newsgroup Gaps slownut Computer Support 8 10-03-2004 09:02 PM
XSLT White Space Gaps Under Images Mark247 XML 1 09-02-2004 11:47 PM
log parsing: gaps between transactions Arthur Dent Perl 1 12-03-2003 12:20 AM
Writing audio cd's without gaps??? Rob Mitchell Computer Support 2 06-30-2003 11:39 PM



Advertisments