Results 1 to 2 of 2

Thread: How to free the strucure memory in C++

  1. #1
    Senior Member
    Join Date
    Oct 2012
    Posts
    297
    Blog Entries
    5

    How to free the strucure memory in C++

    Hello all,

    I am working on structures,I have the below structure

    Code:
    typedef struct stTemp {
    
    char pchchar;
    
    int iVal;
    char* list;
    
    }stTempObj;
    I have appended the structure values by allocating the memory for that.

    And in the destructor memory is freed for the structure.
    Code:
    if (stTempObj)
    { free (stTempObj);  stTempObj = NULL; }
    But memory leaks are found when I run the application using valgrind.
    Please help me what is the wrong in my code. and how to free the memory.

  2. #2
    Senior Member
    Join Date
    Oct 2012
    Posts
    72,727
    First free the memory for the structure char* variable
    Code:
    if (stTempObj)
    {
    
    
    for (int i = 0; i < Count; i++)
    {
       
      if (stTempObj[i].list) {
      free (stTempObj[i].list); stTempObj[i].list = NULL; }  // free the structure variable
    
    }
     free (stTempObj);  stTempObj = NULL; 
    
    }

Similar Threads

  1. problem with free()function
    By sarvani in forum Programming languages
    Replies: 0
    Last Post: 09-08-2012, 10:55 AM
  2. How to free the allocated memory for an array of pointers in c++?
    By Santhi Katuri in forum Programming languages
    Replies: 1
    Last Post: 06-13-2012, 09:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •