-1
#include <stdio.h>    
#include <stdlib.h>

#define MAX_ROWS 5
#define MAX_COLS 5    

int globalvariable =  100;

void CreateMatrix(int ***Matrix)
      {
        int **ptr;
        char *cp;
        int i = 0;

        *Matrix = (int**)malloc((sizeof(int*) * MAX_ROWS) + ((MAX_ROWS * MAX_COLS)*sizeof(int)));
        ptr = *Matrix;
        cp = (char*)((char*)*Matrix + (sizeof(int*) * MAX_ROWS));

        for(i =0; i < MAX_ROWS; i++)
        {
            cp = (char*)(cp + ((sizeof(int) * MAX_COLS) * i));
            *ptr = (int*)cp;
            ptr++;      
        }

    }

    void FillMatrix(int **Matrix)
    {
        int i = 0, j = 0;

        for(i = 0; i < MAX_ROWS; i++)
        {
            for(j = 0; j < MAX_COLS; j++)
            {
                globalvariable++;           
                Matrix[i][j] = globalvariable;
            }
        }

    }

    void DisplayMatrix(int **Matrix)
    {
        int i = 0, j = 0;

        for(i = 0; i < MAX_ROWS; i++)
        {
            printf("\n");
            for(j = 0; j < MAX_COLS; j++)
            {
                printf("%d\t", Matrix[i][j]);                        
            }
        }
    }

    void FreeMatrix(int **Matrix)
    {
       free(Matrix);
    }


    int main()
    {
      int **Matrix1, **Matrix2;

      CreateMatrix(&Matrix1);

      FillMatrix(Matrix1);

      DisplayMatrix(Matrix1);

      FreeMatrix(Matrix1);

      getchar();

      return 0;
    }

If the code is executed, I get the following error messages in a dialogbox.

Windows has triggered a breakpoint in sam.exe.

This may be due to a corruption of the heap, which indicates a bug in sam.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while sam.exe has focus.

The output window may have more diagnostic information.

I tried to debug in Visual Studio, when printf("\n"); statement of DisplayMatrix() is executed, same error message is reproduced.

If I press continue, it prints 101 to 125 as expected. In Release Mode, there is no issue !!!.

please share your ideas.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63

3 Answers3

1

In C it is often simpler and more efficient to allocate a numerical matrix with calloc and use explicit index calculation ... so

int width = somewidth /* put some useful width computation */;
int height = someheight /* put some useful height computation */
int *mat = calloc(width*height, sizeof(int));
if (!mat) { perror ("calloc"); exit (EXIT_FAILURE); };

Then initialize and fill the matrix by computing the offset appropriately, e.g. something like

for (int i=0; i<width; i++)
  for (int j=0; j<height; j++)
    mat[i*height+j] = i+j;

if the matrix has (as you show) dimensions known at compile time, you could either stack allocate it with

   { int matrix [NUM_COLS][NUM_ROWS];
     /* do something with matrix */
   }

or heap allocate it. I find more readable to make it a struct like

   struct matrix_st { int matfield [NUM_COLS][NUM_ROWS]; };
   struct matrix_st *p = malloc(sizeof(struct matrix_st));
   if (!p) { perror("malloc"); exit(EXIT_FAILURE); };

then fill it appropriately:

   for (int i=0; i<NUM_COLS; i++)
     for (int j=0; j<NUM_ROWS, j++)
        p->matfield[i][j] = i+j;

Remember that malloc returns an uninitialized memory zone so you need to initialize all of it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • But why do this manually, if the compiler does it for you automagically? –  Aug 27 '13 at 08:56
0

A two-dimensional array is not the same as a pointer-to-pointer. Maybe you meant

int (*mat)[MAX_COLS] = malloc(MAX_ROWS * sizeof(*mat));

instead?

0

Read this tutorial.

A very good & complete tutorial for pointers, you can go directly to Chapter 9, if you have in depth basic knowledge.

0xF1
  • 6,046
  • 2
  • 27
  • 50