Thread: heap corruption while releasing the memory - triple pointer involved

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    heap corruption while releasing the memory - triple pointer involved

    Hey folks,
    I was debugging a large code which I couldn't post here - so I wrote a prototype code just to handle the issue in my application. Please don't ask the logic or why I have written functions or that I could write it in other way. My issue is what is wrong with this piece of code and how to handle it? It is throwing me the error - "heap corruption" (in main function that i have jotted in red color line i.e. in free(ptr)). I am running it in Visual C++

    the code is -

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    void test_function(int ***);
    int * fill();
    int count();
    
    int main()
    {
    	int **p=NULL;
    	test_function(&p);
    	free(*p);
    	free(p); //THIS LINE IS THROWING THE ERROR
    	system("pause");
    	return 0;
    }
    
    
    void test_function(int ***p)
    {
        int n=count();
        *p=(int **)malloc(n);
        int *temp=NULL;
        temp=fill();
        **p=temp;
     }
    
    int * fill()
    {
       int *temp=NULL;
       temp=(int *)malloc(sizeof(int));
       *temp=2;
       return temp;
    }
    
    int count()
    {
    	int cnt = 1;
    	return cnt;
    }
    thanks and appreciate for any help
    Last edited by vivek_coer; 11-10-2009 at 05:58 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    To what chunk of memory is your triple indirect pointer pointing to?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Hello itCbitC,
    As you see in the code, I am using tripe pointer just to allocate the memory for the double pointer (pointer to array of pointers to char) in main(). In short triple pointer in test_function is nothing but my way of manipulating the double pointer of main function.

    I am releasing the memory pointed to by double pointer in main() - why is it unable to do so? I debugged and noted the address in all the functions and they all match as they should.

    What is wrong with this code and what line I should edit or add to make it working?

    Thank you

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void test_function(int ***);
    int *fill();
    
    int main()
    {
    	int **p=NULL;
    	test_function(&p);
    	free(*p);
    	free(p);
    	return 0;
    }
    
    
    void test_function(int ***p)
    {
        *p = malloc(sizeof(**p));
        int *temp=NULL;
        temp=fill();
        **p=temp;
     }
    
    int *fill()
    {
       int *temp=NULL;
       temp=(int *)malloc(sizeof(int));
       *temp=2;
       return temp;
    }
    I took off the other function just to simplify the code, that wasn't the problem.
    Can you see what was the problem?
    Last edited by MisterIO; 11-10-2009 at 06:24 PM.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Fantastic - I got the point. Thank you MisterIO - much appreciated .

    I was assuming that the pointer will just take one byte - bad habit from using old C compilers. For others to refer I would add my code again with the changes which makes it executable.

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    void test_function(int ***);
    int * fill();
    int count();
    
    int main()
    {
    	int **p=NULL;
    	test_function(&p);
    	free(*p);
    	free(p); //THIS LINE IS NO MORE THROWING THE ERROR
    	system("pause");
    	return 0;
    }
    
    
    void test_function(int ***p)
    {
        int n=count();
        *p=(int **)malloc(n*sizeof(**p)); //THIS CHANGE SOLVES THE PROBLEM
        int *temp=NULL;
        temp=fill();
        **p=temp;
     }
    
    int * fill()
    {
       int *temp=NULL;
       temp=(int *)malloc(sizeof(int));
       *temp=2;
       return temp;
    }
    
    int count()
    {
    	int cnt = 1;
    	return cnt;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By xixpsychoxix in forum C Programming
    Replies: 4
    Last Post: 10-02-2008, 03:31 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. heap memory management
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2002, 10:23 PM