Lan Nguyen
Lan Nguyen

Reputation: 329

Double buffer vs double array c++

I was asked to create a matrix with 5 rows and unknown column. And my boss want me to use a 1 dimensional buffer. concatenated by 5 rows buffer. I don't get what is that mean, can some one provide me a simple example please!

With array I can do

double[][] arr = new double[5][someNumber];

But he says then the size would be limited.

So I don't know what he means by using a DOUBLE buffer, I am not very good @C++

Thank you very much, an example would be nice!

Upvotes: 0

Views: 1168

Answers (2)

M4rc
M4rc

Reputation: 503

Sounds like you're saying

double *arr[5];
for(unsigned int x = 0; x < 5; ++x)
{
       arr[x] = new double[someNumber];
}

Since, you know that you have 5 for sure, and an unknown part my assumption is this is how you're referring to it.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476990

For R rows and C columns declare double arr[R * C], and arr[i * C + j] is the element at cell [i, j].

This generalizes to arbitrary dimensions.

Flattening out an array like that can be a very useful optimization, especially when you use dynamic arrays such as std::vector, where you can get a single dynamic array rather than one for each row.

Upvotes: 1

Related Questions