Shape ***newData = new Shape[_frames][_capacity]; //figure out this
これは、あなたがそう考えるものではありません。 フリーストアの配列へのポインタの配列のみを割り当てます。_capacity
が定数であると仮定すると、配列内の各ポインタは、正確に_capacity
のインスタンスがShape
である別の固定配列へのポインタです。ここで絵です:
index
+-----------+
newData --> | 0 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| 1 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| 2 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| |
.......
| |
+-----------+
| _frames-1 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
(This is the only thing that is actually allocated)
表現new Shape[_frames][_capacity]
は、実際にShape
の_frame*_capacity
のインスタンスを保持するのに十分なメモリを割り当てません。また、配列内の各ポインタはの固定配列の配列を指しているため、コードスニペット内の非定数_capacity
についてコンパイラが不平を言います。代わりに、このように、行と列をエミュレートするためのアドレス指定方式を使用し、その後、Shape
秒のリニアアレイを保持するためにstd::vector
を使用し、痛々しいほど混乱Shape***
とnew[_frames][_capacity]
ビジネスを使用しての
:
// Exposition only
std::vector<Shape> data;
void ResizeMatrix(std::vector<Shape>& data, int frames, int capacity)
{
data.resize(frames * capacity);
}
// Populate data with Shapes
void IndexMatrix(int capacity_index, int frame_index, int capacity)
{
Shape& aShape = data[capacity_index + (capacity * frame_index)];
// Do something with it
}
なぜ」上がりませんあなたは 'std :: vector'を使っていますか? –
サイズ '(num_rows * num_cols)'で 'std :: vector'を使い、' data [col_index +(row_index * num_cols)] 'のようなアドレス体系を使うのはなぜでしょうか? 'Shape *** 'は見るだけで痛いからです。 –
C++の '***'は非常に間違っています。 –