Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I assume the memory is continuous?

Reply
Thread Tools

Can I assume the memory is continuous?

 
 
linq936
Guest
Posts: n/a
 
      09-13-2007
Hi,
When I use a vector iterator I normally do this way:

vector<int>::iterator itr = vec.begin();
for (; itr != vec.end(); ++itr ){
}

Note I am comparing if itr equals to vec.end(). I am wondering if I
could use "less than":

for (; itr < vec.end(); ++itr ){
}

I know this is not good in the system that virtual memory is not
used since memory can be segmented. But if we just assume the program
only runs on virtual memory system, then it is ok to write the code
using "less than". Am I right? As I see in virtual memory allocated
memory is always continuous.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-13-2007
linq936 wrote:
> Hi,
> When I use a vector iterator I normally do this way:
>
> vector<int>::iterator itr = vec.begin();
> for (; itr != vec.end(); ++itr ){
> }
>
> Note I am comparing if itr equals to vec.end(). I am wondering if I
> could use "less than":
>
> for (; itr < vec.end(); ++itr ){
> }
>
> I know this is not good in the system that virtual memory is not
> used since memory can be segmented. But if we just assume the program
> only runs on virtual memory system, then it is ok to write the code
> using "less than". Am I right? As I see in virtual memory allocated
> memory is always continuous.


Sure. Beware, though, that if you change your container to, say,
std::list, it's not going to work. Why do you really want the '<',
anyway?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Adrian
Guest
Posts: n/a
 
      09-13-2007
Vectors guarantee that the elements are in 1 contiguous block of
memory.

So for vectors you can use iter < vector.end().

Since iterators act as smart pointers this will always work.

It will not work for containers like map, set, list though as there is
not such guarantee about where elements are located in memory.


Adrian

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-13-2007
On 2007-09-13 13:11:15 -0400, Adrian <(E-Mail Removed)> said:

> Vectors guarantee that the elements are in 1 contiguous block of
> memory.
>
> So for vectors you can use iter < vector.end().
>


The question isn't whether the memory is in one block, but whether the
container's iterator is a random access iterator. That's required for
vector and for deque (which doesn't guarantee a contiguous block).
Random access iterators support <. Other iterators do not.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-13-2007
linq936 wrote:

> Hi,
> When I use a vector iterator I normally do this way:
>
> vector<int>::iterator itr = vec.begin();
> for (; itr != vec.end(); ++itr ){
> }
>
> Note I am comparing if itr equals to vec.end(). I am wondering if I
> could use "less than":
>
> for (; itr < vec.end(); ++itr ){
> }


Yes, you can.

> I know this is not good in the system that virtual memory is not
> used since memory can be segmented. But if we just assume the program
> only runs on virtual memory system, then it is ok to write the code
> using "less than". Am I right?


No. In the above, using "less than" is _always_ ok regardless of the
platform.

What makes operator< work in your example is that std::vector<>::iterator is
a random access iterator. As such it must support operator<, and for
iterators into the same sequence it will tell you which element comes
first. See clause [24.1.5].

Thus, you could use "less than" also for std::deque<>::iterator.


On the other hand, what is wrong with != in the code? It is clearly more
idiomatic for iterating over a sequence (since other iterator categories do
not have operator<).

> As I see in virtual memory allocated memory is always continuous.


Irrelevant. You seem to think that std::vector<>::iterator is a pointer.
That may be the case, but is not guaranteed by the standard.

On the other hand, std::vector<> (as opposed to all other sequence types)
guarantees that memory is contiguous. That, however, is entirely unrelated
to the question whether operator< can be used with std::vector<>::iterator.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-13-2007
Adrian wrote:
> Since iterators act as smart pointers this will always work.


You mean iterators act as *pointers*, not as *smart pointers*,
which are a bit different.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-13-2007
linq936 wrote:
> Note I am comparing if itr equals to vec.end(). I am wondering if I
> could use "less than":
>
> for (; itr < vec.end(); ++itr ){


Actually I think that if you use an iterator which does not support
comparison with "less than", you will get a compilation error (this will
happen eg. with a list iterator). If it compiles, it should work as you
expect.
 
Reply With Quote
 
Adrian
Guest
Posts: n/a
 
      09-13-2007
On Sep 13, 12:22 pm, Juha Nieminen <(E-Mail Removed)> wrote:
> Adrian wrote:
> > Since iterators act as smart pointers this will always work.

>
> You mean iterators act as *pointers*, not as *smart pointers*,
> which are a bit different.


I meant smart as in them knowing about user types not as in boost etc


 
Reply With Quote
 
Adrian
Guest
Posts: n/a
 
      09-13-2007
> The question isn't whether the memory is in one block, but whether the
> container's iterator is a random access iterator. That's required for
> vector and for deque (which doesn't guarantee a contiguous block).
> Random access iterators support <. Other iterators do not.


Knew I should have checked the standard before posting

Thanks

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-13-2007
Adrian wrote:
> On Sep 13, 12:22 pm, Juha Nieminen <(E-Mail Removed)> wrote:
>> Adrian wrote:
>>> Since iterators act as smart pointers this will always work.

>> You mean iterators act as *pointers*, not as *smart pointers*,
>> which are a bit different.

>
> I meant smart as in them knowing about user types not as in boost etc


I don't even understand what that means.
 
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
Can I assume fgets won't modify last bytes of output array if unused ? Francis Moreau C Programming 17 01-04-2011 01:33 AM
Date#parse assume a EU format - can it assume a US format? Josh Sharpe Ruby 1 09-21-2010 09:16 AM
Assume program under constant attack Tomás Ó hÉilidhe C Programming 7 01-22-2008 07:46 PM
The CIT200 VOIP phone I bought will not establish connection to the USB base. It just beeps - busy signal I assume. Carl Rossman VOIP 0 03-16-2007 10:29 PM
Is it safe to assume default value from dynamic memory allocation? howa C++ 7 12-03-2006 07:44 AM



Advertisments