Freeing dynamically allocated memory

This is a discussion on Freeing dynamically allocated memory within the C Programming forums, part of the General Programming Boards category; I found this code on the internet. Its purpose is to allocate a contiguous memory block for a two-dimensional dynamic ...

  1. #1
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32

    Freeing dynamically allocated memory

    I found this code on the internet. Its purpose is to allocate a contiguous memory block for a two-dimensional dynamic array.

    Code:
    int** x;
    int* temp;
    
    x = (int**)malloc(dimension1_max * sizeof(int*));
    temp = (int*)malloc(dimension1_max * dimension2_max * sizeof(int));
    for (int i = 0; i < dimension1_max; i++) {
      x[i] = temp + (i * dimension2_max);
    }
    How should I free the memory allocated with such code?
    I'd go for:

    Code:
    free(x[0]);
    free(x);
    Is my assumption correct? I also think I can "forget" the temp pointer because it points to the same memory block as x[0].

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Yes.
    Thanks. Is the temp pointer actually needed at all? Can I replace it with x[0]?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    You can replace temp with x[0] if you wish, as long as you only use x[0] after x has been given an initial value.

    Incidentally, I suggest writing:
    Code:
    x = malloc(dimension1_max * sizeof(x[0]));
    There is no need to cast the return value of malloc, and then you might as well make sizeof use x in some way so that if the type of x changes, the code would remain correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Yes, you can remove temp and work with x[0] directly:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TEST
    #define N 10
    #define M 5
    
    int main()
    {
        int** p = (int**)malloc(N * sizeof(int*));
        int k = 0;
    
        if (p)
        {
            // Allocate one master block
            p[0] = (int*)malloc((N * M) * sizeof(int));
    
            // Split up the master block
            for (int x = 1; x < N; ++x)
            {
                p[x] = p[0] + (x * M);
            }
    
            // Initialize the array
            for (int x = 0; x < N; ++x)
            {
                for (int y = 0; y < M; ++y)
                {
                    p[x][y] = k++;
                }
            }
    
    #if defined(TEST)
            // Print for testing
            for (int x = 0; x < N; ++x)
            {
                for (int y = 0; y < M; ++y)
                {
                    printf("%3d", p[x][y]);
                }
    
                putchar('\n');
            }
    #endif
    
            // Clean up the mess
            free(p[0]);
            free(p);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing dynamic allocated memory of structure
    By darekg11 in forum C Programming
    Replies: 14
    Last Post: 01-10-2011, 08:17 AM
  2. Freeing Dynamic allocated memory
    By TiNkiN in forum C Programming
    Replies: 10
    Last Post: 10-26-2010, 04:39 AM
  3. Error when freeing memory (allocated with realloc)
    By svdrjeug in forum C Programming
    Replies: 18
    Last Post: 01-03-2008, 10:16 AM
  4. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  5. freeing allocated memory
    By Devil Panther in forum C Programming
    Replies: 5
    Last Post: 06-29-2003, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21