Thread: usage of free in an array setting

  1. #1
    Registered User
    Join Date
    Apr 2019

    usage of free in an array setting

    im working my way through c unleashed and got the section on data organization using arrays. it gives an example program of dynamically allocating memory for the columns as pointers then dynamically allocates memory for each row. so in effect the pointers in each column point to the memory allocated for that row. ie
    myarray = malloc(sizeof(*myarray) * num_columns)
    for (i = 0; i < num_columns; i++)
           myarray[i] = malloc(sizeof(row) * num_elements)
    however he then goes on to say that if the memory allocation fails clean up loops through the number of columns freeing the pointer for the row with free(myarray[i]) but to free the initial call of maloc he just uses free no brackets.

    further down he has a function to clean up after the array is finished with he clears the rows as before but this time he calls free(myarray)

    which is correct or do both work. i would try with a simple program (no need to fill the array) but im worried about leaving a memory leak and causing issues.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by cooper1200
    but to free the initial call of maloc he just uses free no brackets
    free is a function, so you're either looking at prose (as in not code) or a simple typo error.

    For this kind of question, you should write a program (or two) to test before asking on this forum. It is easily answered by your compiler.

    Look, you're almost certainly going to run your program on a modern OS. Even if your compiler didn't give you an error and you ran a program and it had a huge memory leak, your computer will be perfectly fine. I promise. At most the process from your program might crash, or you'll have to kill it, that's all. Memory will be returned to your OS, life goes on as per normal. So compile your program and run it. Don't worry.
    Last edited by laserlight; 06-07-2019 at 03:58 AM.
    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    I'm guessing that you are looking at the "FreeStrArray" function on page 360/361.

    Remember that Array is actually a char** that had memory dynamically allocated to it
    Array = malloc(NumRows * sizeof *Array);
    You then went through each row and allocated the size of each row
    for(Row = 0; Row < NumRows; Row++) 
      Array[Row] = malloc(Width * sizeof *Array[Row]) ;
    So you will need to free each row, and then the initial char** when disposing the memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up a usage message cmd
    By kingtin00 in forum C Programming
    Replies: 2
    Last Post: 10-09-2017, 02:18 PM
  2. How to detect CPU usage and free RAM on Windows x86 in C?
    By barracuda in forum C Programming
    Replies: 1
    Last Post: 02-20-2015, 12:27 PM
  3. malloc and free usage
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 09-26-2011, 08:06 AM
  4. 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
  5. free() usage
    By pdstatha in forum C Programming
    Replies: 5
    Last Post: 03-13-2002, 09:28 AM

Tags for this Thread