-5

These codes are parts of project:

//.h file

#ifndef IMAGEFILTER_H
#define IMAGEFILTER_H

#include "filter.h"

class ImageFilter : public Filter {
public:
    ImageFilter(int _dimension);
    virtual ~ImageFilter();
protected:
    int* values;
};
#endif // IMAGEFILTER_H

//.cpp file

#include "imagefilter.h"

ImageFilter::ImageFilter(int _d) : Filter(_d) {
    values = new int[_d * _d];
}

ImageFilter::~ImageFilter() {
    delete [] values;
}

How should I understand the line :"values = new int[_d * _d];" ? Could you help me ?

Little Tooth
  • 99
  • 3
  • 10
  • Probably as a pointer to a heap-allocated buffer of `_d * _d *sizeof(int)` bytes. Or yes, undefined behavior if this is not an excerpt but the full code. – Marco A. Nov 18 '16 at 08:55
  • Sorry, my friends, I should post all relative codes just now. I have edited my question.@songyuanyao@Marco A@songyuanyao – Little Tooth Nov 18 '16 at 09:09
  • I'd say that your filter stores a two-dimensional image of quadratic shape with a side length of _d that is stored in an array over int, accessed by something like "coordinates x,y are stored at position x + _d*y" (or the other way around - in any case, it's somewhat strange that _d is not stored itself). The line in question initializes the array. Everything clear now? Btw, in C++, one would prefer std::vector over an array whenever possible. – Aziuth Nov 18 '16 at 09:13
  • I think you need a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 18 '16 at 09:19
  • I took `*` as the symbol of the pointer just now, in fact, it means multiplying. Many thanks for your answer.@Aziuth – Little Tooth Nov 18 '16 at 09:29
  • Gratitude also for you! @molbdnilo – Little Tooth Nov 18 '16 at 09:33

1 Answers1

1

What does values = new int[_d * _d]; mean?

Reserve _d * _d of sequenced integers (probably 4 byte each) dynamically in the free store. In other words, you have reserved an array of integers with _d * _d items that can be used later in many ways.

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • 1
    You are trying to do image processing in C++ while you do not even know what dynamic arrays are.. probably you need to start from a lower level and find some good C++ book (with absolutely no offense). and I have answered your question already. – Humam Helfawi Nov 18 '16 at 09:13
  • 1
    Glad for you timely reply! I absolutely agree with your words, also I am a green bird. I took `*` as the symbol of the pointer just now, in fact, it means multiplying. Thank for your advice very much! – Little Tooth Nov 18 '16 at 09:27