Topic: const array initializatin
Author: deepblack@geocities.com (Lums Coelho)
Date: 1998/10/02 Raw View
On 30 Sep 1998 16:19:44 GMT, Terence Kelling
<kelling@arlut.utexas.edu> uttered the following words:
>
>There is a long way to go about achieving what you want. First you would
>allocate a block of memory big enough to hold all the elements in the array
>
>int number_of_elements = 10;
>T* base = malloc(sizeof(T) * number_of_elements);
>
>Then you would call placement new. This is a variation of new which allows
>you to construct an object at a specified location
>
>T* ptr = base;
>for (int i = 0; i < number_of_elements; i++) {
>
> new(ptr) T(123);
> ptr++;
>}
>
>Now you have to make sure you clean up after yourself
>
>delete base;
Will not work. You got the memory from malloc! I don't think that
using placement new changes anything. Use either
free(base);
or get the memory like
T* base = static_cast<T*>(new char[sizeof(T)*num_elem]);
Regards,
Lums Coelho.
*******
Translations: Any combination between German, English & Portuguese.
Contact me at deepblack@geocities.com
*******
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/02 Raw View
James Kuyper wrote:
>
> #include <memory>
> uninitialized_fill_n(base, number_of_elements, T(123));
That won't work if there isn't a copy constructor. However, according to
my reading, the following will work, since the constructor in question
takes only one parameter:
uninitialized_fill_n(base, number_of_elements, 123);
That invokes new(...) T(123) a number of times.
--
Ciao,
Paul
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/10/02 Raw View
Lums Coelho wrote:
> T* base = static_cast<T*>(new char[sizeof(T)*num_elem]);
There are no reasons for this char array (think about it as
a string) to be suitable for a static_cast<T*>.
Use malloc (sizeof(T)*num_elem) or
operator new(sizeof(T)*num_elem).
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1998/10/02 Raw View
On 02 Oct 98 12:35:47 GMT, Lums Coelho <deepblack@geocities.com> wrote:
>On 30 Sep 1998 16:19:44 GMT, Terence Kelling
>>int number_of_elements = 10;
>>T* base = malloc(sizeof(T) * number_of_elements);
>>delete base;
Oops! Oops!
>Will not work. You got the memory from malloc! I don't think that
>using placement new changes anything. Use either
>
>free(base);
Oops!
Forgot to call dtors for all the objects in the array.
>or get the memory like
>
>T* base = static_cast<T*>(new char[sizeof(T)*num_elem]);
The memory returned from "new char[]" might not be properly aligned.
The memory returned from malloc and operator new is properly
aligned for any object -- the most stringest alignment.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1998/10/04 Raw View
sbnaran@localhost.localdomain.COM (Siemel Naran) writes:
|> >or get the memory like
|> >
|> >T* base = static_cast<T*>(new char[sizeof(T)*num_elem]);
|>
|> The memory returned from "new char[]" might not be properly aligned.
|> The memory returned from malloc and operator new is properly
|> aligned for any object -- the most stringest alignment.
Strange that nobody has mentioned it, but static_cast< T* >( char* )
isn't legal. You should get a compiler error. (And of course, you're
right about the alignment problem.)
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: brownsta@concentric.net (Stan Brown)
Date: 1998/09/30 Raw View
[posted and emailed]
hiranabe@esm.co.jp (Kenji Hiranabe) wrote:
>Is there a way to initialize an array of T with a specified
>constructor ?
> void f() {
> T t[10](123); // no such syntax ?
> ...
> }
As far as I know, you can't do it with a plain C-style array.
You *can* do it with a vector<T>. The constructor is
explicit vector(size_type n, const T& value = T())
Reference: Musser & Saini, /STL Tutorial and Reference Guide/, page 275.
--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
http://www.concentric.net/%7eBrownsta/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/09/30 Raw View
There is a long way to go about achieving what you want. First you would
allocate a block of memory big enough to hold all the elements in the array
int number_of_elements = 10;
T* base = malloc(sizeof(T) * number_of_elements);
Then you would call placement new. This is a variation of new which allows
you to construct an object at a specified location
T* ptr = base;
for (int i = 0; i < number_of_elements; i++) {
new(ptr) T(123);
ptr++;
}
Now you have to make sure you clean up after yourself
delete base;
This seems to be quite a bit of effort to do what you want. It is in fact
the mechanics that the vector class in the Standard Template Library does for
you, and I would really recommend using that class instead.
Terence Kelling
[ Moderator's note: excessive quoting deleted. Please do not quote
more material than is necessary to understand the reply. -sdc ]
Kenji Hiranabe wrote:
> Is there a way to initialize an array of T with a specified
> constructor ?
>
> If T has a constructor T(int), I'd like to construct an array
> of T with T(int) constructor.
>
> void f() {
> T t[10](123); // no such syntax ?
> ...
> }
>
> If there are no such syntax, why not ?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/30 Raw View
Terence Kelling wrote:
> There is a long way to go about achieving what you want. First you would
> allocate a block of memory big enough to hold all the elements in the array
> int number_of_elements = 10;
> T* base = malloc(sizeof(T) * number_of_elements);
> Then you would call placement new. This is a variation of new which allows
> you to construct an object at a specified location
> T* ptr = base;
> for (int i = 0; i < number_of_elements; i++) {
> new(ptr) T(123);
> ptr++;
> }
Or, equivalently:
#include <memory>
uninitialized_fill_n(base, number_of_elements, T(123));
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: hiranabe@esm.co.jp (Kenji Hiranabe)
Date: 1998/09/29 Raw View
Is there a way to initialize an array of T with a specified
constructor ?
If T has a constructor T(int), I'd like to construct an array
of T with T(int) constructor.
void f() {
T t[10](123); // no such syntax ?
...
}
If there are no such syntax, why not ?
--
Java3D vecmath implementor http://www.esm.co.jp/java/vecmath/
Eiwa System Management, Inc. http://www.esm.co.jp/
Kenji Hiranabe E-Mail: hiranabe@NOSPAMesm.co.jp
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]