Hi all
I'm a bit new to c programming, so go easy...
I'm currently trying to implement a linked-list-like data structure, which is actually a giant "word tree", so I have a struct like so:
Code:
/* Structure to hold a linked list node */
typedef struct tagTreenode
{
/* Create an array of 26 pointers to nodes (one for each letter) */
struct tagTreenode *apNodes[26];
char cValue;
} TREENODE;
I then have a recursive function (InsertNode()) that attempts to store new nodes in the correct place in the array of the current node... the problem I'm having is that on the 2nd call to InsertNode(), when it calls this function...
Code:
TREENODE * CreateNewBlankNode( )
{
int i = 0;
TREENODE *pNewNode = NULL;
/* Assign some memory for the new node */
if ((pNewNode = calloc(sizeof(TREENODE), 1)) == NULL)
{
printf("ran out of dynamic memory allocating a node ");
}
if (pNewNode)
{
/* Set the value of the first node to "Start of Text" */
pNewNode->cValue = 3;
/* Set all of the pointers to nodes in the array of the first node to NULL */
for (i; i < 26; i++)
{
pNewNode->apNodes[i] = NULL;
}
}
return pNewNode;
}
... calloc fails to allocate any memory. It works perfectly the first time, but not the second. Is this just a case of very n00b-like programming in that I'm not being efficient enough with memory? Or do you think I'm doing something else majorly wrong? The sizeof a TREENODE is only 108...
I realise it might be difficult to work out what's going on just from the snippets of code above.. so I can post more if it will help... any advice would be much appreciated, thanks!