473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Can I assume the memory is continuous?

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.

Sep 13 '07 #1
18 2047
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
Sep 13 '07 #2
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

Sep 13 '07 #3
On 2007-09-13 13:11:15 -0400, Adrian <nn**@bluedreamer.comsaid:
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)

Sep 13 '07 #4
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
Sep 13 '07 #5
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.
Sep 13 '07 #6
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.
Sep 13 '07 #7
On Sep 13, 12:22 pm, Juha Nieminen <nos...@thanks.invalidwrote:
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
Sep 13 '07 #8
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

Sep 13 '07 #9
Adrian wrote:
On Sep 13, 12:22 pm, Juha Nieminen <nos...@thanks.invalidwrote:
>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.
Sep 13 '07 #10
On Sep 13, 8:22 pm, Juha Nieminen <nos...@thanks.invalidwrote:
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.
And what's the difference? The usual definition of a "smart
pointer" is a class type which supports the usual pointer
operations. Iterators certainly fit the bill in that regard.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 14 '07 #11
James Kanze wrote:
And what's the difference? The usual definition of a "smart
pointer" is a class type which supports the usual pointer
operations. Iterators certainly fit the bill in that regard.
The definition of "smart pointer" I have heard is one which
uses reference counting for automatically freeing the memory
allocated behind the pointer.
Sep 14 '07 #12
In message <46**********************@news.song.fi>, Juha Nieminen
<no****@thanks.invalidwrites
>James Kanze wrote:
>And what's the difference? The usual definition of a "smart
pointer" is a class type which supports the usual pointer
operations. Iterators certainly fit the bill in that regard.

The definition of "smart pointer" I have heard is one which
uses reference counting for automatically freeing the memory
allocated behind the pointer.
Is std::auto_ptr a smart pointer? Is boost::scoped_ptr? Neither of those
uses reference counting, but both are in some sense "smart" - their
destructors automatically delete the referenced objects (which may
involve more than just "freeing the memory") and they enforce particular
(different) contracts regarding ownership.

--
Richard Herring
Sep 14 '07 #13
Richard Herring wrote:
> The definition of "smart pointer" I have heard is one which
uses reference counting for automatically freeing the memory
allocated behind the pointer.

Is std::auto_ptr a smart pointer? Is boost::scoped_ptr? Neither of those
uses reference counting, but both are in some sense "smart" - their
destructors automatically delete the referenced objects (which may
involve more than just "freeing the memory") and they enforce particular
(different) contracts regarding ownership.
No need to nitpick. I believe that you understood what I meant.
Sep 14 '07 #14
In message <46**********************@news.song.fi>, Juha Nieminen
<no****@thanks.invalidwrites
>Richard Herring wrote:
>> The definition of "smart pointer" I have heard is one which
uses reference counting for automatically freeing the memory
allocated behind the pointer.

Is std::auto_ptr a smart pointer? Is boost::scoped_ptr? Neither of those
uses reference counting, but both are in some sense "smart" - their
destructors automatically delete the referenced objects (which may
involve more than just "freeing the memory") and they enforce particular
(different) contracts regarding ownership.

No need to nitpick. I believe that you understood what I meant.
I understood the literal meaning of what you posted. I still don't
understand what point you were trying to make.

--
Richard Herring
Sep 17 '07 #15
On Sep 14, 4:00 pm, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
And what's the difference? The usual definition of a "smart
pointer" is a class type which supports the usual pointer
operations. Iterators certainly fit the bill in that regard.
The definition of "smart pointer" I have heard is one which
uses reference counting for automatically freeing the memory
allocated behind the pointer.
And where did you hear this definition. I've never heard it.
The definition of a smart pointer is simply a user defined type
which behaves like a pointer (at least in some contexts or some
ways). Always has been, at least.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 18 '07 #16
James Kanze wrote:
And where did you hear this definition. I've never heard it.
http://en.wikipedia.org/wiki/Smart_pointer

"In computer science, a smart pointer is an abstract data type that
simulates a pointer while providing additional features, such as
automatic garbage collection or bounds checking. These additional
features are intended to reduce bugs caused by the use of pointers while
retaining efficiency. Smart pointers typically keep track of the objects
they point to for the purpose of memory management."

"The use of pointers is a major source of bugs: the constant allocation,
deallocation and referencing that must be performed by a program written
using pointers makes it very likely that some memory leaks will occur.
Smart pointers try to prevent memory leaks by making the resource
deallocation automatic: when the pointer to an object (or the last in a
series of pointers) is destroyed, for example because it goes out of
scope, the pointed object is destroyed too."

Memory management seems to be the key concept.
Sep 18 '07 #17
Any single allocation in C++ is contiguous.
The relationship between multiple allocations
(either distinct objects or successive calls
to memory allocators) is outside the scope
of specified behavior. Specifically the
standards for C and C++ did provide for the
fact that they may run on segmented architectures.
Sep 18 '07 #18
On Sep 18, 12:15 pm, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
And where did you hear this definition. I've never heard it.
http://en.wikipedia.org/wiki/Smart_pointer
"In computer science, a smart pointer is an abstract data type that
simulates a pointer while providing additional features, such as
automatic garbage collection or bounds checking.
Which is exactly the definition I gave. A class (abstract data
type) which simulates a pointer while providing additional
features.
These additional
features are intended to reduce bugs caused by the use of pointers while
retaining efficiency. Smart pointers typically keep track of the objects
they point to for the purpose of memory management."
"The use of pointers is a major source of bugs: the constant allocation,
deallocation and referencing that must be performed by a program written
using pointers makes it very likely that some memory leaks will occur.
Smart pointers try to prevent memory leaks by making the resource
deallocation automatic: when the pointer to an object (or the last in a
series of pointers) is destroyed, for example because it goes out of
scope, the pointed object is destroyed too."
Memory management seems to be the key concept.
That's not what the first paragraph you quote says. What it
says is that memory management is a "typical" use. Which
implies that there are others. (And in fact, the first sentence
mentions one other: bounds checking.)

As usual, of course Wikipedia is of mixed quality. The first
paragraph above is actually fairly close to the truth. The
second is very debatable, and really only applies to C++, and
then only to one particular class of smart pointers in C++, and
arguably only to organisations which don't know how to manage
development in C++. Smart pointers exist outside of C++ (since
Java calls its pointers references, they are smart references in
Java), and have been used for many other purposes even in C++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 19 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Dobcsanyi | last post by:
Calling the following function with a large enough 'n' causes memory leak. import numarray as N def loop(n, m=100): for i in xrange(n): a = N.zeros((m,m)) N.matrixmultiply(a, a) If the...
11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
1
by: picard | last post by:
I have seen in various posts that there are tricks to increasing the largest continuous memory block available to an application on a windows machine. I want to prove this is possible using a...
4
by: Sharon | last post by:
I have an application that fails to allocate all its memory on physical memory. I have a Windows XP with 2 GByte RAM. Can anybody tell me how to configure the allowed physical memory for each...
2
by: billsahiker | last post by:
Is the capacity of collections, such as arraylist, limited by available RAM, or do they use virtual memory, caching to disk when needed? I tested this by writing a simple console application that...
2
by: Lambda | last post by:
I'd like to load a lot of data into a hashtable, when the memory is used up, I'll write the data to a file. I'm trying to use std::tr1::unordered_map to implement that. My question is if I...
19
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
21
by: google | last post by:
Hello, in embedded programming, different kinds of memory exist, e.g. RAM and ROM (Flash memory). For a class containing variables and constant values one might want to put the variables in RAM...
2
by: manojmohadikar2008 | last post by:
Hi All, We are observing a serious issue with the memory usage of Queue and its very critical issue which needs to be fixed. We have an application which runs two threads i.e. a Producer and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.