1

I need to have a 2D array of double. Its width is around 900. Its height as well (the same width value).

Dealing with two loops (one for the width and one for the height), I really need to get access to all the pixels of the 900X900 image that I will process.

The size of the array is too big (error when specifying the number of raw and column).

I thought about establishing that with a dynamic array to optimize the time of calculation and to free the memory everytime I deal with one pixel on the two loops.

But I really cannot find the syntax I would like to have to declare a 2D dynamic array (malloc, setting array element values and freeing the memory).

5
  • 1
    Hint: in C++, std::vector is new malloc. Commented Mar 7, 2013 at 13:43
  • 900х900х4=3.2M It's not no much... dynamic arrays are very slow. Give us more info about how you get this image and how you process it. What error do you have? Commented Mar 7, 2013 at 13:50
  • it's written stack over flow ! Commented Mar 7, 2013 at 13:52
  • when it's about to process a window of the image (100X100 for instance), I have great calculations for it, but I want to process the whole image, thus, when I change the number of elements of my array to 900 :: ALERT with Stack over flow. Commented Mar 7, 2013 at 13:54
  • 1
    Do not create it on stack. Use malloc to allocate memory for array or increase stack size in your compiler options. Commented Mar 7, 2013 at 13:54

3 Answers 3

3

Wrap it in a class:

class Matrix2D {
    typedef std::vector<double> Column;
    std::vector<Column> columns;
public:
    Matrix2D(unsigned int width, unsigned int height) :
    columns(width, Column(height)) {
    }

    double& at(unsigned int i, unsigned int j) {
        return columns[i][j];
    }
};

Matrix2D matrix(900, 900);

matrix.at(45, 65) = 1234.5678;
Sign up to request clarification or add additional context in comments.

1 Comment

Waaw! The result is showed up in few seconds!!!!!! I was waiting 1- 2 minutes to deal with one small window (300X300) of the whole image Thank you
2

I need to have a 2D array of double

Since you are using C++ you should use STL classes that will take care of ugly memory management for you. So you are actually looking for std::vector< std::vector<double> >, or for the sake of the readability of your code:

#include <vector>
typedef std::vector<double> DVector;      // row represented by vector of doubles
typedef std::vector<DVector> MyVector;    // 2D array as a vector of these rows

And then avoid using dynamic allocation wherever it's possible to do so. Take advantage of RAII idiom:

{
    MyVectorarr;  // vector object with automatic storage duration
} // <-- vector is automatically destructed when execution goes out of scope

Questions that might help you:
Multi-dimensional vector
Initialization of a vector of vectors?
vector of vector

2 Comments

Thank you for that. But here, in the code example that you have showed, how to set the values of the created vector?
@ZeusM: Check my edit, I added links to questions that will help you.
1

I associate malloc with pure C, not C++ (as the prior answer points yout, you should use std::vector). However, if you really want to:

// allocate the memory in a block
double* block = (double *) malloc(sizeof(double) * xSize * ySize);
// allocate memory for the accessor array
double* accessor = (double*) malloc(sizeof(double*) * xSize);
// assign memory addresses
double* curPtr = block;
for (int i = 0; i < xSize; ++i) {
    accessor[i] = curPtr;
    curPtr += ySize;
}

// you can now access the array via accessor[x][y]

// now need to free malloced memory:
free(accessor);
free(block);

If you do it this way, I highly suggest tying it to the RAII pattern, otherwise you'll eventually get a memory leak. Using the STL's containers is a better approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.