Connecting Tech Pros Worldwide Help | Site Map

Gaps in memory

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:51 AM
Sulsa
Guest
 
Posts: n/a
Default Gaps in memory

Does memory allocated by opperator new has gaps, or is it one big block
of memory??

  #2  
Old July 23rd, 2005, 12:51 AM
Gianni Mariani
Guest
 
Posts: n/a
Default 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.

  #3  
Old July 23rd, 2005, 12:51 AM
Ruslan Abdikeev
Guest
 
Posts: n/a
Default 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.


  #4  
Old July 23rd, 2005, 12:51 AM
Phlip
Guest
 
Posts: n/a
Default 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


  #5  
Old July 23rd, 2005, 12:51 AM
Richard Cavell
Guest
 
Posts: n/a
Default 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.
  #6  
Old July 23rd, 2005, 12:52 AM
Sulsa
Guest
 
Posts: n/a
Default 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
  #7  
Old July 23rd, 2005, 12:55 AM
Phil Staite
Guest
 
Posts: n/a
Default 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]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,840 network members.