2

I created a class and using multi-dimensional pointer as follows:

variable **v_mod;
v_mod = new variable *[3];
for(int i=0;i<3;i++)
{
    v_mod[i] = new variable [n];
}

and deleting pointer after using

for( int i = 0 ; i < 3 ; i++ )
{
    delete [] v_mod[i];
}
delete [] v_mod;

This is working perfectly fine but I am using many pointers. So can anyone help in writing a function in the class which helps in creating and deleting the pointers like

variable ** v_mod;
v_mod.create(3,n);

v_mod.delete();

and which works same way?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
vijay
  • 29
  • 2
  • 3
    Why not use a vector of vectors? – eerorika Feb 25 '16 at 14:32
  • 1
    @user2079303 [My thoughts exactly.](http://stackoverflow.com/a/35630222/2642059) – Jonathan Mee Feb 25 '16 at 14:35
  • Maybe he got a school assignment. – Hamza Anis Feb 25 '16 at 14:39
  • 2
    @HamzaAnis Sounds like a good C assignment. (Maybe that's too strong, I do expect C++ programmers to understand how dynamic memory works too.) – Jonathan Mee Feb 25 '16 at 14:41
  • if done then `v_mod.create()` should be `v_mod->create()` – Hamza Anis Feb 25 '16 at 14:43
  • hey @Jonathan, see if my answer meets the requirement. ;-) – Hamza Anis Feb 25 '16 at 15:08
  • @HamzaAnis Yikes please don't do this. This means that each *element* of your 2D-array will contain an uninitialized `variable**` and an uninitialized `int`. That's a memory waste of the number of elements (here 3 * 2 = 6) `* sizeof(variable**) * sizeof(int)`. `variable` is *not* a container, it's an element class, elements shouldn't have pointers to the whole container. If you want to try to reinvent an octagonal wheel you should at least make a separate class to contain the elements. For me, I'll take the standard implementation round wheel: `vector`. – Jonathan Mee Feb 25 '16 at 15:42
  • Sure, using vectors is the best option. but just wondering how to do it without it. – Hamza Anis Feb 25 '16 at 16:03
  • @HamzaAnis I've updated [my answer](http://stackoverflow.com/a/35630222/2642059) with a couple of options that I don't think you should consider. – Jonathan Mee Feb 25 '16 at 16:59
  • @HamzaAnis I've added [an example of how to work with these options](http://ideone.com/TD7Qcz) to [my answer]. The typing is a bit complex. I wanted to do it in a template, but I haven't been able to sort out just how to do that yet. – Jonathan Mee Feb 25 '16 at 17:33

1 Answers1

3

Don't use new use a vector:

vector<vector<variable>> v_mod(3, vector<variable>(n));

This will require that your variable object has a generated or your own default constructor.

But other than that you can use the vector version of v_mod as you used the dynamically allocated array version, except that a vector cleans itself up when it goes out of scope. So no need to delete.

EDIT:

@Hamza Anis has asked me how to do this without vector, so I'm updating this answer to reflect a couple ways to do that. Let me preface this by saying, vector is the right way to do this, anything short of vector is simply making life harder on everyone who handles the code.

Option 2 unique_ptr:

unique_ptr<variable[]> v_mod[3];

for(auto& i : v_mod) {
    i = make_unique<variable[]>(n);
}

Option 3 only do this if it's a homework assignment:

variable* v_mod[3];

for(auto& i : v_mod) {
    i = new variable[n];
}

for(auto& i : v_mod) {
    delete[] i;
}

[Complex Example using ints]

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288