Thread: Creating array of objects w/o default constructor

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Still, I would like to know how to do this.
    I believe they use placement new. So they allocate a block of memory large enough to hold the data (or perhaps a little larger). Then, they use placement new to construct (via the copy constructor) each element one at a time.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Sort of. They use the allocator's construct() method - but there is pretty much no other way to implement it but placement new.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I think I've figured out the essentials, although the code I have may be MSVC specific.
    Code:
    #include <new>
    #include <iterator>
    #include <iostream>
    #include <memory>
    
    class foo {
       public:
       foo() {
          std::cout << "Default\n";
       }
    
       foo (foo const &) {
          std::cout << "Copy\n";
       }
    
       foo (int i) {
          std::cout << "Int\n";
       }
    
       foo & operator= (foo const &) {
          std::cout << "Assign\n";
          return *this;
       }
    
       ~foo() {
          std::cout << "Destroy\n";
       }
    };
    
    template <class Iter>
    typename std::iterator_traits<Iter>::value_type * make_array (Iter begin, Iter end) {
       std::cout << "Begin make_array\n";
    
       using std::iterator_traits<Iter>::value_type;
       std::allocator<value_type> alloc;
       value_type * result = alloc.allocate (end - begin);
    
       /*
       for (int i = 0; i < end - begin; ++i) {
          alloc.construct (result + i, begin[i]);
       }
       */
       std::uninitialized_copy (begin, end, result);
    
       std::cout << "End make_array\n";
       
       return  result;
    
    }
    
    int main (void) {
       foo * abc = new foo[3];
       for (int i = 0; i < 3; ++i) {
          abc[i] = i;
       }
    
       foo * asdf = make_array (abc, abc + 3);
    
       delete[] abc;
    
       /* 
       // Doesn't work.  Would have to use custom de-allocation code for the array
       std::cout << "Begin array delete\n";
       delete[] asdf;
       std::cout << "End array delete\n";
       */
    
       return 0;
    
    }
    I get the feeling it's impossible to craft an array in the manner I'm looking for such that it can be freed using delete[]. Of course, if the custom allocation is contained in an Object, the custom destructor will be encapsulated anyhow.

    Anyhow, here's the articles that were personally the most helpful
    http://gcc.gnu.org/onlinedocs/libstd...allocator.html
    http://www.scs.stanford.edu/~dm/home...s/c++-new.html
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #19
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by laserlight View Post
    That's not going to work for a vector though (only 10 elements?), and I doubt if vectors are typically implemented as a pointer to a pointer.
    that's why I always prefer array as my official container they offer great functionality with pointers. And they push you (the programmer) to your limits
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    that's why I always prefer array as my official container they offer great functionality with pointers.
    I was talking about implementation, not functionality. Vectors can be used to store pointers too, of course, and provide pointer-like access with iterators.

    And they push you (the programmer) to your limits
    Yes, your limits such that you do not take advantage of RAII
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  3. Replies: 2
    Last Post: 04-04-2007, 06:34 PM
  4. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  5. Creating array from data file...
    By Crankit211 in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 09:45 PM