Thread: This may be due to a corruption of the heap

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    7

    This may be due to a corruption of the heap

    <<< split from This may be due to a corruption of the heap >>>
    READ THE RULES

    i have the same problem on visual studio 2010. Here is the code:

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char labeltype;
    
    typedef struct __celltype {
            labeltype label;
            struct __celltype *first_child;
            struct __celltype *next_sib;
            struct __celltype *last_child;
            int zadnji;
    } celltype;
    
       typedef celltype *node;
       typedef celltype *TREE;
    
       void MAKE_NULL( TREE* X ){
       
    	   celltype* novo_stablo = (TREE)malloc( sizeof( celltype ) );
    	   *X = novo_stablo;
    	   (*X)->label = '\0';
    	   (*X)->first_child = NULL;
    	   (*X)->next_sib = NULL;
    	   (*X)->last_child = NULL;
    	   (*X)->zadnji = 1;
       
       }
    
    
       void __FREE( TREE* stablo ){
    	    
    	   celltype* pom = (*stablo) -> next_sib;
    
    	   if( (*stablo) -> first_child != NULL ) __FREE( &( (*stablo)->first_child ) );
    
    	   if( pom == NULL ){ free( stablo ); return; }
    
    	   while( pom != NULL ){
    	   
    		   celltype* brisi = *stablo;
    		   *stablo = pom;
    		   pom = pom ->next_sib;
    		   free( brisi );
    	   
    	   }
    
    	   return;
    
       }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	TREE X;
    
    	MAKE_NULL( &X );
    
    	 __FREE( &X ); // here is the error by visual studio
    
    	return 0;
    }
    i compiled and runned program on devcpp and codeblocks and they didnt noticed any errors

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    you did not use malloc to allocate for X. but then you use free inside your _FREE function to free X. that may be the problem.
    Code:
    // this may be your problem
    if( pom == NULL ){ free( stablo ); return; }
    You may fix it with the code below, but there's no guarantee that will work c'zu i did not compile it.
    Code:
    if( pom == NULL ){ free( *stablo ); return; }
    btw, since you already use typedef to define TREE, why didn't you use it instead of "celltype *" for many instances in your code? That just causes hella lot of confusion.
    Last edited by nimitzhunter; 01-12-2011 at 06:53 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    *stablo solves it.( wondering why i didnt saw that mistake )
    ( I did the allocation in MAKE_NULL function ). Thanks

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Cobs View Post
    *stablo solves it.( wondering why i didnt saw that mistake )
    ( I did the allocation in MAKE_NULL function ). Thanks
    Well, you kind of abused typedef. Sometimes you use TREE, sometimes you use celltype*, it hard to keep track of what is a pointer and what is a pointer to pointer.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This may be due to a corruption of the heap
    By krishnampkkm in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2009, 03:19 AM
  2. Heap corruption detected. What does it mean?
    By franziss in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2008, 02:50 AM
  3. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  4. Heap corruption errors
    By VirtualAce in forum C++ Programming
    Replies: 0
    Last Post: 07-15-2006, 04:46 PM
  5. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM