Having problems running out of memory

This is a discussion on Having problems running out of memory within the C Programming forums, part of the General Programming Boards category; Hi all I'm a bit new to c programming, so go easy... I'm currently trying to implement a linked-list-like data ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Having problems running out of memory

    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!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I'm willing to bet that the main problem is in your InsertNode() function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    void * calloc ( size_t num, size_t size );

    it looks like you have your parameters switched around
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    The order could be better, but it wouldn't change the outcome.

    What would help would be to post a small and complete program that demonstrates the problem.
    By itself, the function seems fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Replies: 4
    Last Post: 03-29-2004, 06:56 PM
  4. Tile Engine Problems with Video Memory
    By Tommaso in forum Game Programming
    Replies: 8
    Last Post: 03-07-2003, 07:26 PM
  5. Replies: 15
    Last Post: 04-19-2002, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21