 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pierre Espenan Guest
|
Posted: Mon Dec 29, 2003 10:58 am Post subject: large arrays versus standard containers |
|
|
In general, is it true that standard containers, vectors etc. are preferred
to large arrays? If yes, what are the reasons for this preference?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Dec 30, 2003 12:50 am Post subject: Re: large arrays versus standard containers |
|
|
In message <UNJHb.22980$Pg1.11239 (AT) newsread1 (DOT) news.pas.earthlink.net>,
Pierre Espenan <whistling_rabbit (AT) yahoo (DOT) com> writes
| Quote: | In general, is it true that standard containers, vectors etc. are preferred
to large arrays? If yes, what are the reasons for this preference?
|
Many systems have limited stack space and using a std::vector instead of
a raw array ensures relatively low demand for stack space. OTOH there
are reasons to go the other way, particularly for arrays of types that
are not default initialised (if the programmer does not want time spent
on immediate initialisation of all values). These days the difference in
performance is usually small. Newcomers to programming have no need to
learn about arrays until relatively late in the learning process because
vector and string will meet their needs (well they would if only we
could ensure that all the Standard Library functions that use char* were
overloaded to accept std::string as well)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Tue Dec 30, 2003 12:50 am Post subject: Re: large arrays versus standard containers |
|
|
"Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> wrote
| Quote: | In general, is it true that standard containers, vectors etc. are
preferred to large arrays?
|
Yes. There is no reason to use builtin arrays. You can even use an array
class template when you need a fixed sized array.
| Quote: | If yes, what are the reasons for this preference?
|
because C style arrays are very very error-prone to use and because
std::vector is just as fast and because it is much much more handy.
br
Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Tue Dec 30, 2003 12:58 am Post subject: Re: large arrays versus standard containers |
|
|
"Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> writes:
| Quote: | In general, is it true that standard containers, vectors etc. are
preferred to large arrays?
|
In general, standard containers are preferred to arrays, regardless of
size. One sometimes makes an exception for very small arrays which are
themselves members of something which will be in a large array. But
except when order of initialisation is an issue, that's about it.
| Quote: | If yes, what are the reasons for this preference?
|
The standard containers are first class objects, which work like
everything else in the language.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Tue Dec 30, 2003 5:11 am Post subject: Re: large arrays versus standard containers |
|
|
"Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> skrev i en meddelelse
news:UNJHb.22980$Pg1.11239 (AT) newsread1 (DOT) news.pas.earthlink.net...
| Quote: | In general, is it true that standard containers, vectors etc. are
preferred
to large arrays? If yes, what are the reasons for this preference?
I would actually prefer std:: containers in most situations, regardless of |
size.
One exception could be when i need smaller, fixed-size vectors or if I want
to avoid dynamic memory allocation.
As to why, the answer lies in the added safety (when talking about vectors).
Kind regards
Peter Koch Larsen
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Wagner Bruna Guest
|
Posted: Tue Dec 30, 2003 5:16 am Post subject: Re: large arrays versus standard containers |
|
|
Hi,
"Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> wrote
| Quote: | In general, is it true that standard containers, vectors etc. are preferred
to large arrays? If yes, what are the reasons for this preference?
|
This is discussed in the FAQ:
http://www.parashift.com/c++-faq-lite/
But I'm not sure what do you mean by "large" arrays: are you referring
to the common C practice of creating static sized arrays with "big
enough" space?
--
Wagner
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Tue Dec 30, 2003 5:18 am Post subject: Re: large arrays versus standard containers |
|
|
On Mon, 29 Dec 2003 05:58:42 -0500, Pierre Espenan wrote:
| Quote: | In general, is it true that standard containers, vectors etc. are preferred
to large arrays? If yes, what are the reasons for this preference?
|
1. The memory used in a large array is the maximum that you will even need
to ensure no overflows, etc... while the memory reqd. for containers is
approximately what is actully required.
2. Better cache efficiency (possibly)!
3. Accessor functions.
4. Possibly debugging facilities provided by the labrary provider.
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Tue Dec 30, 2003 12:41 pm Post subject: Re: large arrays versus standard containers |
|
|
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote
[snip]
| Quote: | . OTOH there
are reasons to go the other way, particularly for arrays of types that
are not default initialised (if the programmer does not want time spent
on immediate initialisation of all values).
|
tr1::array<X,size> does not initialize values by default AFAIR.
br
Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Tue Dec 30, 2003 12:49 pm Post subject: Re: large arrays versus standard containers |
|
|
"Peter Koch Larsen" <pkl (AT) mailme (DOT) dk> writes:
| Quote: | "Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> skrev i en meddelelse
news:UNJHb.22980$Pg1.11239 (AT) newsread1 (DOT) news.pas.earthlink.net...
In general, is it true that standard containers, vectors etc. are
preferred to large arrays? If yes, what are the reasons for this
preference?
I would actually prefer std:: containers in most situations,
regardless of size. One exception could be when i need smaller,
fixed-size vectors or if I want to avoid dynamic memory allocation.
|
More generally applicable, I think, is the order of initialization
issue. Almost all of my C style arrays are of PODs, using {...}
initialization with constant expressions. That way, I know that they
will be initialized before any dynamic initialization starts.
| Quote: | As to why, the answer lies in the added safety (when talking about
vectors).
|
Safety? Neither are really safe. Both allow verification. In
practice, I think some implementations of std::vector do verify -- in
the past I think there was also an implementation of C++ in which C
style arrays were bounds checked (Centerline). But in practice, buffer
overflow is a latent problem with both.
I think that the real answer is first, that std::vector behaves like
every other type in the system, with value semantics, and second, that
it encompasses the size, which with C style arrays must be passed
separately (and explicitely). The latter is a simple convenience; the
former, on the other hand, makes reasoning about the code significantly
simpler.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Dec 30, 2003 7:23 pm Post subject: Re: large arrays versus standard containers |
|
|
In message <3ff10584$0$18693$afc38c87 (AT) news (DOT) optusnet.com.au>, Thorsten
Ottosen <nesotto (AT) cs (DOT) auc.dk> writes
| Quote: | "Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:8c8+BFBm1A8$EwDG (AT) robinton (DOT) demon.co.uk...
[snip]
. OTOH there
are reasons to go the other way, particularly for arrays of types that
are not default initialised (if the programmer does not want time spent
on immediate initialisation of all values).
tr1::array<X,size> does not initialize values by default AFAIR.
|
The original question as I understood it concerned choosing between
std::vector and raw C-style arrays. The UDT you reference is a wrapping
of a raw array to make it more STL algorithm friendly. It provides a
genuine choice where the stack usage becomes the major issue for
sequences whose size is known at compile time.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Gottman Guest
|
Posted: Wed Dec 31, 2003 1:05 pm Post subject: Re: large arrays versus standard containers |
|
|
"Peter Koch Larsen" <pkl (AT) mailme (DOT) dk> wrote
| Quote: |
"Pierre Espenan" <whistling_rabbit (AT) yahoo (DOT) com> skrev i en meddelelse
news:UNJHb.22980$Pg1.11239 (AT) newsread1 (DOT) news.pas.earthlink.net...
In general, is it true that standard containers, vectors etc. are
preferred
to large arrays? If yes, what are the reasons for this preference?
I would actually prefer std:: containers in most situations, regardless of
size.
One exception could be when i need smaller, fixed-size vectors or if I
want
to avoid dynamic memory allocation.
As to why, the answer lies in the added safety (when talking about
vectors). |
Have you looked at the boost array class? (See
[url]http://www.boost.org/libs/array/index.htm)[/url]. This is an STL compliant
fixed-size array wrapper. Its as efficient as a C-style array, but it can
be returned from a function or passed as a parameter to a function without
suffering pointer-decay like an ordinary array.
Joe Gottman
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|