[C++] How do I allocate a multidimensional array with new?

Started by
51 comments, last by rip-off 13 years, 7 months ago
@Bearhugger: I won't argue against the points you've made, but I just want to comment on this quickly:
Quote:It might be because I self-taught myself C++ instead of going to school, but I gotta say that I'm not too fond of teachers insisting on obscure technicalities that don't matter 99% of the time.
A lot of us here are self-taught coders with no formal training, but still feel that following (or at least being aware of) best practices in C++ can be beneficial and important. Also, things like the 'Rule of Three' aren't academic, ivory-tower curiosities, but rather basic fundamentals with practical implications for software quality (IMO).

Anyway, I'm certainly not questioning you in general, but I just wanted to point out that the 'self-taught vs. academic' thing isn't really relevant here.
Advertisement
Oh, I assure you, nothing against you guys either, I'm just a very opinionated and questioning programmer with the bad habit of getting dragged easily into debates. I should probably pull my reverence now.

In any case, I wanted to present a way to work with multi-dimensional arrays without having to use the STL or boost, which is obviously not the favorite way to achieve it here. I don't believe it is so dangerous, but whatever. I don't think anyone's gonna change his or her mind at this point anyway.

One last thing: about buffers, as I previously said, for variable or dynamic-sized arrays such as buffers, I have nothing against the STL. I just kinda hijacked the thread over to other topics by bringing other stuff. Anything that is exposed to the outside world needs to be strongly validated and checked, so, no, new and delete are really not a good idea here.
Quote:
...disabling = operators, and so on. This is great and all, it doesn't hurt to be careful I guess, but it looks like his teachers insisted more on those mostly academic issues then for things that actually happen in the industry, such as buffer overflows.

The thing is, its almost trivial. For any class where there is doubt, just inherit from boost::noncopyable. If necessary, later on you can go back and implement the correct deep copying semantics. You can even write this tiny helper class yourself, so you don't need to depend on boost:
class noncopyable {protected:    noncopyable(){}private:    // deliberately private and unimplemented, don't copy me!    noncopyable& operator =(const noncopyable &);    noncopyable(const noncopyable &);};class Foo : noncopyable {   // Now you only have to worry about getting the desctructor right};

Takes no time at all, and will catch errors. It is hardly some obscure acedemic principle here, all it takes is to accidentally pass Foo by value where a reference was intended - which is an easy mistake to make. The cost/benefit analysis clearly tips this into the realm of practical considerations.

I'd agree with you about non-explicit constructors, they've never been an issue for me and I consider worrying about them a little pedantic. As you said, maybe in a library API I might go the extra mile but not for internal classes.
Quote:
n any case, I wanted to present a way to work with multi-dimensional arrays without having to use the STL or boost, which is obviously not the favorite way to achieve it here.

I actually don't use boost::mutli_array for 2D arrays, I would find it inconvenient to use.

But I'd rather write the simple class I presented earlier (with the_edd's correction) than have to implement a destructor, etc. It works with copying if that is required, it has std::vector<>'s usual index checks too in debug mode. And later on, if profiling justifies it, I can re-implement using raw allocation if necessary.
Quote:
I don't believe it is so dangerous, but whatever.

I believe can see where you're coming from. In some code bases, by value copies are never done. I suspect that a copy by value of a non-trivial type would probably be as big a code smell to you as the lack of copying semantics is to us. You would just "see" it when you look at the code. I understand this feeling, I too have a number of subconscious checks that I make when running through code; one of them is uncommented deep copies of complex types.

This topic is closed to new replies.

Advertisement