0

I have the following struct which I wanted to initialize

struct Box{
    int *dimval;
    int no;
    int dim;

    Box(dim){
        this->dim = dim;
        dimval = new int[dim]
    }

}

now in my main function. I wanted to initialize an array of Box struct, but this is problematic with my implementation.

int main(){
    Box *boxes;
    int num_box, dim;

    cin>>num_box>>dim;

    boxes = new Box[num_box](dim);// I know this is devil here. 
}

I want to have a dynamic array containing num_box Box items, each being initialized with a dynamic array of dim long. How can I do that?

yizzlez
  • 8,757
  • 4
  • 29
  • 44
Daniel
  • 1,484
  • 5
  • 24
  • 42
  • You need a default constructor if you want to do that... – yizzlez May 06 '14 at 00:01
  • but with default constructor, I cannot pass dim parameter in it – Daniel May 06 '14 at 00:02
  • 3
    Simply do not use raw arrays and use `std::vector` instead. – Chnossos May 06 '14 at 00:06
  • I am practicing writing c style code with c++, so stl data type is not preferred. how do you achieve this with a c code? – Daniel May 06 '14 at 00:14
  • @Daniel `I am practicing writing c style code with c++` Why? Why not just write straight C code instead of wasting time with C++, and in the process, change the tag to `C` instead of `C++`. – PaulMcKenzie May 06 '14 at 00:18
  • @PaulMcKenzie. I wanted to practice playing with pointers(for learning), and possibly use some library functions in c++ – Daniel May 06 '14 at 00:21
  • There are no constructors in C so I do not see how this is useful practice for C-style code – M.M May 06 '14 at 00:22
  • @Daniel, it's a good idea to practice with pointers IMO, but managing arrays is a relatively tough task that you'd never have to perform yourself in practice. I understand your point of view, but I humbly suggest that you spend your time working with pointers to single objects, or with objects simple enough that they can have a default constructor. Writing *good* collections is harder than it looks like. Even the standard ones run into [unexpected issues](http://llvm.org/bugs/show_bug.cgi?id=16238) once in a while. – zneak May 06 '14 at 00:24

1 Answers1

3

You cannot create an array of a type unless it has a default constructor, and then you can't initialize each of them. However, you can initialize a vector with a default object, which is pretty much what you're asking for here.

#include <vector>

int num_box, dim;
cin >> num_box >> dim;
vector<Box> boxes(num_box, Box(dim));

Note that you need a copy constructor to handle the copying of dimval...

#include <algorithm>

struct Box
{
    // ...

    Box(const Box& that)
    {
        this->dim = that.dim;
        this->no = that.no;
        this->dimval = new int[dim];
        copy(that.dimval, that.dimval + that.dim, this->dimval);
    }
};

... but you can use the default copy constructor if you simply replace your dimval pointer with a vector, too, since vectors handle copy (that would fix a leak, too).

struct Box
{
    vector<int> dimval;
    int no;
    int dim;

    Box(int dim)
    : dimval(dim)
    {
        this->dim = dim;
    }
}
zneak
  • 134,922
  • 42
  • 253
  • 328
  • any way to do it under the context of dynamic allocated pointer? I am maintaining my version of dynamic array instead of using stl vector – Daniel May 06 '14 at 00:11
  • 1
    You cannot invoke a constructor other than the default constructor when you create an array with `new`, and there needs to be a default constructor to be able to create an array with `new`. Your only other solution is to use a placement new, but this really opens a can of worms. – zneak May 06 '14 at 00:12
  • Anyways, here's how you'd do it, but I personally believe that you would shoot yourself in the foot if this is for anything more than learning purposes: http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new – zneak May 06 '14 at 00:14
  • @Daniel Why you use your own version of `std::vector` if you can freely choose between the two ? – Chnossos May 06 '14 at 00:14
  • @Daniel `I am maintaining my version of dynamic array instead of using stl vector ` and while you're doing that, some other C++ programmer is using `std::vector` and has made more progress in finishing their program. – PaulMcKenzie May 06 '14 at 00:22
  • @Chnossos, I am practicing with pointers. so I'd like to get rid of any pre-written libraries if possible – Daniel May 06 '14 at 00:24
  • @PaulMcKenzie, yeah I know, but this is for practising purpose, and I wanted to get hangs on the underlying concepts instead of just relying on libraries. – Daniel May 06 '14 at 00:26
  • @Daniel - `I'd like to get rid of any pre-written libraries` - so are you going to write your own `stream` library next? – PaulMcKenzie May 06 '14 at 00:26
  • @Daniel - `I wanted to get hangs on the underlying concepts instead of just relying on libraries` Your "goal" isn't focused at all. Unless you're talking about bare C++ syntax, almost everything is a part of some library. This goes for C as well as C++. Are you now going to write your own allocator instead of using `new`? Are you going to write your own C-string functions such as strlen(), strcpy()? Are you going to write your own file I/O functions? I could go on and on. – PaulMcKenzie May 06 '14 at 00:29
  • @PaulMcKenzie, please make your own answer if you want to be condescending. I'd like to keep the comments on my answers friendly. You're not helping anyone learn anything if you only try to show them that you're better than them. – zneak May 06 '14 at 00:29
  • @zneak - Can you point out explicitly where I am showing I am "better"? I am just pointing out the vastness of not having to use "any libraries" (the OP's words, not mine). – PaulMcKenzie May 06 '14 at 00:39
  • @PaulMcKenzie: you are right in every aspect, we cannot live with libraries... but relying on library won't teach u much for learning purpose and one day you would possibly develop your own libraries, the pains here may pay off in the long run. – Daniel May 06 '14 at 00:49
  • @Daniel - Well, others have mentioned that it is not easy, and attempting to answer such a question as "how to write a dynamic array" goes well beyond posting a code snippet in the answer box. I believe you should first start out with learning the basics of memory management and pointers. If you don't know about copy constructors, assignment operators, exception safety, etc., then you need to know these things before embarking on writing dynamic array classes. Open up the `vector` header for your compiler and you will see it is not simple. – PaulMcKenzie May 06 '14 at 00:54