Results 1 to 4 of 4
  1. #1
    faid's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Location
    Somewhere over the rainbow
    Posts
    539
    Reputation
    40
    Thanks
    62
    My Mood
    In Love

    Am I doing this deallocate memory right?

    Hello, I am wondering whether I am freeing the the memory correctly or is there a better way to do so. Please enlighten me anyone. Much appreciated.


    Here is my data.
    Code:
    struct CSVRow
    {
    	char** colData;
    	int size;
    };
    
    struct CSVData
    {
    	struct CSVRow* rowData;
    	int size;
    };
    Here is the free memory function. I want to free everything. Am i doing this right?
    Code:
    /* Deallocates all entries of the given CSV Data */
    void FreeCSVData(struct CSVData* target)
    {
    	/* variable declarations */
    	int rowSize = 0, colSize = 0, i = 0, j = 0;
    	
    	/* checks for invalid input parameter */
    	if (target == NULL)
    		return;
    	
    	/* assign the row size of target to variable rowSize */
    	rowSize = target->size;
    	
    	/* loop through the rows of CSVData */
    	for (i = 0; i < rowSize; i++)
    	{
    		/* assign the row size of target to variable rowSize */
    		colSize = target->rowData[i].size;
    		/* loop through the column section of each specific rows */
    		for (j = 0; j < colSize; j++)
    		{
    			/* assigning each column data with null */
    			target->rowData[i].colData[j] = NULL;
    			/* free each column data */
    			free(target->rowData[i].colData[j]);
    		}
    		/* assigning each row of column data with null */
    		target->rowData[i].colData = NULL;
    		/* free each row of column data */
    		free(target->rowData[i].colData);
    	}
    	/* lastly free all rows */
    	free(target->rowData);
    	/* notify upon success function execution */
    	printf("Free CSVData success!\n");
    	
    	return;
    }

    Thank you.

  2. #2
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,979
    Reputation
    353
    Thanks
    8,755
    My Mood
    Twisted
    Assuming you're working on the heap, no! You're assigning NULL before you call free. You end up calling free with NULL as the memory pointer to free. You need to switch those two operations and free the memory before you invalidate the pointers. You should also invalidate your rowData pointer to possibly avoid dangling pointers.
    Last edited by Biesi; 1 Week Ago at 04:19 PM.

  3. The Following User Says Thank You to Biesi For This Useful Post:

    faid (1 Week Ago)

  4. #3
    faid's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Location
    Somewhere over the rainbow
    Posts
    539
    Reputation
    40
    Thanks
    62
    My Mood
    In Love
    Quote Originally Posted by Biesi View Post
    Assuming you're working on the heap, no! You're assigning NULL before you call free. You end up calling free with NULL as the memory pointer to free. You need to switch those two operations and free the memory before you invalidate the pointers. You should also invalidate your rowData pointer to possibly avoid dangling pointers.
    Oh no.. I never knew that, I always thought that it is the opposite. Now it makes sense. Thank you!
    Other than that, I hope I am not causing anymore leaks.

  5. #4
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,979
    Reputation
    353
    Thanks
    8,755
    My Mood
    Twisted
    Quote Originally Posted by faid View Post
    I hope I am not causing anymore leaks.
    Handling memory in C is a very complex topic that also depends on how you allocated the memory.. if you're working on the stack, you're mostly just fine, otherwise every allocate call should correspond to a free call.

  6. The Following User Says Thank You to Biesi For This Useful Post:

    faid (1 Week Ago)

Similar Threads

  1. Please someone tell me if im doing this right
    By Dab1996426 in forum General
    Replies: 3
    Last Post: 10-23-2015, 08:23 PM
  2. Am i doing this right?
    By Not an Asian in forum General
    Replies: 22
    Last Post: 02-20-2015, 09:28 AM
  3. Make sure i'm doing this right..
    By CainFool in forum Call of Duty Modern Warfare 2 Help
    Replies: 4
    Last Post: 08-29-2010, 07:53 AM
  4. This DLL is Right?
    By lordraven in forum Hack Requests
    Replies: 2
    Last Post: 03-25-2008, 06:43 PM
  5. am i doing this right?
    By ajdude121 in forum WarRock - International Hacks
    Replies: 3
    Last Post: 04-14-2007, 04:27 PM

Tags for this Thread