2

I'm new to c++ (java programmer) and I am working on a homework assignment for an intro course. The purpose is "Constructors, Dynamic Memory Allocation and Overloading Operators" That being said, I'm really stuck on one of the specifics.

I'm creating 2 classes Color, and ColorBox. It was specified in the instruction that the member variables in ColorBox are int width, int height and Color** data. My understanding is that data holds reference to a 2D array of Color objects...

My question is: How do I set some type of empty or basic value for data in the DVC? And does anybody have a link for a decent write up on this kind of pointer? I've found generic write ups on arrays and pointers but I'm still having trouble wrapping my head around this.

Thanks in advance!

Edit: I think I made it work with the code below, but I'll admit I still don't think I know what I'm doing.

ColorBlock::ColorBlock()
{
    this->width = 0;
    this->height = 0;
    Color temp_data[1][1];
    this->data = (Color**)temp_data;
}
SMI
  • 21
  • 2
  • What is "DVC" in this context? – Dai Oct 29 '16 at 05:03
  • Default value constructor – SMI Oct 29 '16 at 05:18
  • Ecxellent. I was wondering how you lost the J in Damme Van Claude-Jean. – user4581301 Oct 29 '16 at 05:30
  • Sorry, I didn't understand either. Your code in Edit will provoke a Segfault temp_data is destroyed at the end of the scope so if you attempt to use this->data after it will explode. maybe you want use [nullptr](http://en.cppreference.com/w/cpp/language/nullptr) ? @user4581301 :p – Stargateur Oct 29 '16 at 05:49
  • You didn't made it work. Firstly, you store a pointer to a local object. As soon as ColorBlock constructor finishes, that pointer becomes invalid and then you have a nasty bug waiting to struck. Secondly, you probably should use `width` and `height` when allocating memory (which you should do using `new`). Thirdly, given `Color**` type, you need to allocate array of arrays and not single 2D array. – Andrey Turkin Oct 29 '16 at 05:51

1 Answers1

2

When you say empty or basic value, I assume you mean to initialize the variables to a safe default state. If that is the case, the code you present is somewhat correct, but could use some work.

To start, you could rewrite your constructor like the following

ColorBlock::ColorBlock()
{
    this->width = 0;
    this->height = 0;
    this->data = nullptr;
}

The most significant change here is that data no longer points towards the junk data you created. This is important because when the constructor terminates, the memory pointed to will no longer be valid memory but it will be difficult to check if it is junk memory or not. nullptr is the C++11 way of initializing pointers to a safe value as it cannot point to a valid memory location (it is used to denote the pointer points to nothing) and it is also type safe.

[In C and C++ standards before C++11 we would initialize pointers to NULL, for more information on why you should use nullptr check here: What are the advantages of using nullptr? ]

Another thing to note, as noted below by stargateur, is that you used a C-type cast, this cast is powerful because it combines reinterpret_cast, const_cast, and static_cast, but is unsafe for the very same reason. Here you probably would have wanted reiniterpret_cast.

Beyond that, however, there is another 'more c++' way of initializing this data.

ColorBlock::ColorBlock() : //Note the colon here
    width(0), height(0), data(nullptr)
{ }

For information on why you would/should/could use this style see the following:
Why should I prefer to use member initialization list?

P.S. This is my first attempt at helping someone through this site, if I could improve my format or explanation in order to be more concise please point it out to me. If I made an error, please point that out as well.

EDIT: attempted to add changes as suggested.

Community
  • 1
  • 1
Siech0
  • 471
  • 6
  • 12
  • add why it's not safe to do `this->data = (Color**)temp_data;` in the case. One the cast is in c-like and two why temp_data will be deytroy at the end of the function. – Stargateur Oct 29 '16 at 06:10