Thread: Freeing dynamically allocated memory

  1. #1
    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
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09: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, 11: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