By TobyB


2019-11-08 14:05:00 8 Comments

I am trying to work with a matrix of pointers to some pixels and everything seem to work fine, until I extracted my code into a function and now my matrix is suddenly unusable.

Here is my problematic function:

rgb32 ***PixelMatrix(bitmap bmp)
{
    int rows = bmp.getHeight();
    int columns = bmp.getWidth();
    int i, j;

    rgb32*** pixelMatrix = new rgb32**[rows];
    for(i=0; i<rows; ++i)
    {
        pixelMatrix[i] = new rgb32 * [columns];
    }

    for(i=0; i<rows; ++i)
        for(j=0; j<columns; ++j)
        {
            pixelMatrix[i][j] = bmp.getPixel(i, j);
        }

    return  pixelMatrix;
}

//in main()

rgb*** pixelMatrix = PixelMatrix(bmp)

While debuging this code everything seemed fine, but as soon as the function returns, the pointer will now point to some unreadable memory. I am very curious as why this happens?

0 comments

Related Questions

Sponsored Content

25 Answered Questions

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 296424 View
  • 1525 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

25 Answered Questions

[SOLVED] Why do we need virtual functions in C++?

6 Answered Questions

[SOLVED] Typedef function pointer?

  • 2010-11-28 04:50:25
  • Jack Harvin
  • 377423 View
  • 425 Score
  • 6 Answer
  • Tags:   c++ c pointers typedef

20 Answered Questions

4 Answered Questions

[SOLVED] Explanation of function pointers

4 Answered Questions

2 Answered Questions

[SOLVED] pointers to template functions

  • 2012-07-18 14:21:46
  • FatalCatharsis
  • 135 View
  • 3 Score
  • 2 Answer
  • Tags:   c++

Sponsored Content