0

I'm trying to create a multidimentional int array with the following function code:

int ** createIntMatrix(unsigned int rows, unsigned int cols) 
    {

  int ** matrix;
  unsigned int i,j;

  matrix = (int **) calloc(cols, sizeof(int *));

  for(i = 0; i < cols; i++)
    matrix[i] = (int *) calloc(rows, sizeof(int));


  for(i = 0; i < cols; i++)
    for(j = 0; j < rows; j++)
      matrix[i][j] = 0;

  return matrix;
}

I create three instances using this function in the following code,

cout<<"allocating temporary data holders..."<<endl;
  int ** temp_meanR;
  int ** temp_meanG;
  int ** temp_meanB;
  temp_meanR = createIntMatrix(img->height,img->width);
  temp_meanG = createIntMatrix(img->height,img->width);
  temp_meanB = createIntMatrix(img->height,img->width);
cout<<"....done!"<<endl;

I'm accessing these elements like temp_meanB[4][5].

But unfortunately, I get the following error during runtime:

allocating temporary data holders...
....done!
tp6(1868) malloc: *** error for object 0x122852e08: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

Where am I going wrong here?

3
  • 3
    why are you using calloc instead of new? Commented May 18, 2011 at 13:17
  • 1
    no need to initialize elements to zero manually if you allocate memory with calloc. According to cplusplus.com/reference/clibrary/cstdlib/calloc all bits are set 0 automatically. Commented May 18, 2011 at 13:18
  • 1
    malloc is the C way, use new in C++. Commented May 18, 2011 at 13:23

1 Answer 1

5
  for(i = 0; i < cols; i++)
    for(j = 0; i < rows; i++)
       matrix[i][j] = 0;

note the inside for loop, it says j=0; i<rows; i++ (before Aarohi Johal's edit)

Next you do not have to set the memory manually to 0, as calloc does it for you.

In C++, you should use new and delete .

In the code segment

matrix = (int **) calloc(cols, sizeof(int *));

for(i = 0; i < cols; i++)
  matrix[i] = (int *) calloc(rows, sizeof(int));

I think first the rows should be allocated and then for each row link the int arrays.

Visulize like this:

        +--------+
        | matrix |
        +--------+
          |            c  o  l  s
          |     +----------------------------+
          V     |                            |
   +--  +---+   +---+---+---+       +---+
   |    |   |-->|   |   |   | . . . |   |
   |    +---+   +---+---+---+       +---+
   |    |   |--+
 r |    +---+  |   +---+---+---+       +---+
 o |    |   |  +-->|   |   |   | . . . |   |
 w |    +---+      +---+---+---+       +---+
 s .      .
   .      .
   .      .
   |    |   |
   |    +---+   +---+---+---+       +---+
   |    |   |-->|   |   |   | . . . |   |
   +--  +---+   +---+---+---+       +---+

First do the rows and then the cols, in the above visualization, then the arr[i][j] interpretation would be like normal array.

Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant! Thanks. And I fixed the typo for the i,j loops in the code.
also note the order you are allocating the rows and cols, am i clear with the diagram what i am saying, about the row-col order of the allocation? In your code, actually the rows is acting as columns and the cols is acting as rows.
That was exactly what was wrong. I compiled successfully as a result of your post :)

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.