Thread: malloc and free for 2-dimensional array

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    malloc and free for 2-dimensional array

    Hi!

    I am trying to explore skills in C, however, I think I don't have a very good background in topics such as malloc.

    I am trying to convert multiple images (.bmp) into two-dimensional arrays for image processing. Importing the image and processing it one-by-one in a for-loop. I'm having trouble with managing the memory usage and the program crashes because i think it's taking so much memory. I wish to assign same memory over and over in the for-loop.

    I think the problem is on how I use the malloc and the free for my two-dimensional array.
    Please check if I'm using them correctly for my two-dimensional array.

    Code:
    int **image_matrix;
    
                 //Allocate memory and initialize image_matrix
                 image_matrix = (int **)malloc(rows * sizeof(int *));
                 if (NULL == image_matrix)
                 {
                          free(image_matrix);
                          printf("Memory allocation failed while allocating for the array[].\n");
                          exit(1);
                 }
                 image_matrix[0] = (int *)malloc(cols * rows * sizeof(int));
                 for(r=1; r<rows; r++)
                 {
                          image_matrix[r] = image_matrix[0] + r*cols;
                 }
                 for(r=0; r<rows; r++)
                 {
                          for(c=0; c<cols; c++)
                          {
                                   image_matrix[r][c] = 0;
                          }
                 }
    
                 //Free image_matrix
                 for(r=1; r<rows; r++)
                 {
                          free(image_matrix[r]);
                 }
                 free(image_matrix);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, remove the cast on the result of malloc.
    If you've included stdlib, then there is nothing more to do.

    Not including stdlib.h results in the cast suppressing a potentially very serious error.


    The alloc seems OK, but you have far too many free calls.
    All you need is
    free( a[0] );
    free( a );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic two dimensional array free problem
    By nickman in forum C Programming
    Replies: 14
    Last Post: 01-13-2011, 12:44 PM
  2. malloc() and free()
    By rlesko in forum C Programming
    Replies: 5
    Last Post: 12-05-2010, 01:49 PM
  3. Correct usage of malloc and free for 2D array
    By disruptivetech in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 05:20 AM
  4. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM