Click here to Skip to main content
15,904,416 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Accessing mainframe menu from a non-modal dialog box Pin
#realJSOP10-May-07 3:32
professional#realJSOP10-May-07 3:32 
GeneralRe: Accessing mainframe menu from a non-modal dialog box Pin
BadJerry10-May-07 4:02
BadJerry10-May-07 4:02 
GeneralRe: Accessing mainframe menu from a non-modal dialog box Pin
BadJerry10-May-07 5:35
BadJerry10-May-07 5:35 
AnswerThanks to everyone : solution! Pin
BadJerry10-May-07 5:34
BadJerry10-May-07 5:34 
QuestionFile operation problem Pin
sandeepkavade10-May-07 0:26
sandeepkavade10-May-07 0:26 
AnswerRe: File operation problem Pin
BadJerry10-May-07 1:07
BadJerry10-May-07 1:07 
QuestionRe: File operation problem Pin
David Crow10-May-07 1:46
David Crow10-May-07 1:46 
Questionmalloc and strings Pin
DarkWeaver545510-May-07 0:15
DarkWeaver545510-May-07 0:15 
Hi,

I'm currently trying to get an n-ary tree implemented in C (I know, C++ would be so much better, but I'm stuck with it). Each node is a struct, which holds a pointer to a string, which is assigned whilst the node is being setup. Basically, I'm confused over the use of malloc and free; I don't think everything is being correctly freed at the moment.
Struct:
typedef struct node {
  char *sData;                         // Data stored in node
  int iNumChildren;                    // Number of children of node
  struct node *nParent;                // Pointer to parent of node
  struct node *nChildren[MAX_NODES];   // MAX_NODES pointers to child nodes
} treeNode;

addNode:
struct node* addNode(struct node *nParent, char *sData)
{
	treeNode *temp;
	int iNodeLevel;

	if( nParent->iNumChildren < MAX_NODES )
	{
		// Allocate memory for new node
		temp = malloc( sizeof( treeNode ) );
		// Set up new node's data
		temp->sData = sData;
		
		...
		
		return temp;
	}
	else
	{
		return NULL;
	}
}

freeTree:
void freeTree()
{
	int iRep;
	int iFreed = 0;
	char *sData;
	treeNode *temp;
	
	while( getNumChildren( root ) > 0 )
	{
		// Set temp to the last child of root
		temp = root->nChildren[ root->iNumChildren - 1 ];

		for( iRep = 0; iRep < iLongestPath; iRep++ )
		{
			if( getNumChildren( temp ) > 0 )
				temp = temp->nChildren[ temp->iNumChildren - 1 ];
		}

		// We're now at the bottom of this particular branch, decrement parent's child count, then free
		temp->nParent->iNumChildren--;
		sData = temp->sData;
		// This didn't work
		//free( temp->sData );
		free( temp );
		iFreed++;
		printf( sData );
	}

	free(root);
	iFreed++;

#ifdef _DEBUG
	printf( "Freed %d nodes\n", iFreed );
#endif
}

As you can see, I've put in some code to check if the string was being freed, and it's not. However, I don't know how else this could be done; I'm not exactly a C guru.
Also, I did have a look around to see if there was anything similar, but all the code I found was in C++, which is no good for me. If anyone knows of something I could look at, I'd be supremely grateful.

Any ideas?
Sean.
AnswerRe: malloc and strings Pin
BadJerry10-May-07 0:50
BadJerry10-May-07 0:50 
GeneralRe: malloc and strings Pin
DarkWeaver545510-May-07 2:33
DarkWeaver545510-May-07 2:33 
QuestionRe: malloc and strings Pin
David Crow10-May-07 1:54
David Crow10-May-07 1:54 
QuestionRe: malloc and strings Pin
DarkWeaver545510-May-07 2:16
DarkWeaver545510-May-07 2:16 
AnswerRe: malloc and strings Pin
David Crow10-May-07 2:21
David Crow10-May-07 2:21 
GeneralRe: malloc and strings Pin
DarkWeaver545510-May-07 2:37
DarkWeaver545510-May-07 2:37 
QuestionGif image Pin
prathuraj10-May-07 0:14
prathuraj10-May-07 0:14 
AnswerRe: Gif image Pin
Hamid_RT10-May-07 2:14
Hamid_RT10-May-07 2:14 
GeneralRe: Gif image Pin
prathuraj10-May-07 5:15
prathuraj10-May-07 5:15 
GeneralRe: Gif image Pin
Hamid_RT10-May-07 6:52
Hamid_RT10-May-07 6:52 
AnswerRe: Gif image Pin
ThatsAlok16-May-07 21:16
ThatsAlok16-May-07 21:16 
Questiondata problem Pin
p_10-May-07 0:06
p_10-May-07 0:06 
Questionmaking windows service with wxwidget Pin
seneralex9-May-07 23:46
seneralex9-May-07 23:46 
AnswerRe: making windows service with wxwidget Pin
ThatsAlok16-May-07 21:15
ThatsAlok16-May-07 21:15 
AnswerRe: making windows service with wxwidget Pin
Priyank Bolia16-May-07 21:53
Priyank Bolia16-May-07 21:53 
QuestionReporting tool Pin
Piyush Thacker9-May-07 23:33
Piyush Thacker9-May-07 23:33 
JokeRe: Reporting tool Pin
Programm3r9-May-07 23:40
Programm3r9-May-07 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.