Serious issue with malloc function

This is a discussion on Serious issue with malloc function within the C Programming forums, part of the General Programming Boards category; Hi, I'm facing a critical issue with the malloc function. Since my implementation is huge, I have copied only a ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    10

    Serious issue with malloc function

    Hi,

    I'm facing a critical issue with the malloc function.

    Since my implementation is huge, I have copied only a piece of code here which i feel erroneous

    Code:
    UINT32 nAllocMemSize = 0;
    
    void *utMemAlloc(UINT32 nMemSize)
    {
    	void *pAllocAddr = NULL;
    	
    	DEBUG_PRINT(("Start of utMemAlloc\n"));
    	
    	if(nMemSize != 0)
    	{
    		nAllocMemSize += nMemSize;
    		DEBUG_PRINT(("Allocating Memory of %d bytes. Total Allocated %d\n", nMemSize, nAllocMemSize));
    		pAllocAddr = malloc(nMemSize);
    		
    		if(pAllocAddr == NULL)
    		{
    			printf("Memory Allocation Failed\n");
    		}
    	}
    	
    	DEBUG_PRINT(("End of utMemAlloc\n"));
    	return pAllocAddr;
    }
    Console Output:
    ===============
    (check only the highlighted console output)

    Assigning the Address to the Return Variable
    End of utCreateInfoListNode

    Start of cpLexer_UpdateTokLoc for the token " " of size 1
    End of cpLexer_UpdateTokLoc

    Start of cpLexer_UpdateTokLoc for the token "*" of size 1
    End of cpLexer_UpdateTokLoc
    Start of cpLexer_TokenValStore
    Start of utMemAlloc
    Allocating Memory of 6 bytes. Total Allocated 13681
    End of utMemAlloc

    End of cpLexer_TokenValStore
    Start of cpProcessDecSpec
    Start of utCreateInfoListNode
    NULL Address. Hence Allocating Memory
    Start of utMemAlloc
    Allocating Memory of 12 bytes. Total Allocated 13693
    End of utMemAlloc


    [similiar prints sliced]

    Start of utMemAlloc
    Allocating Memory of 12 bytes. Total Allocated 13920
    End of utMemAlloc

    Memory Allocation Success!!!
    Assigning the Address to the Return Variable
    End of utCreateInfoListNode


    Start of cpLexer_UpdateTokLoc for the token " " of size 1
    End of cpLexer_UpdateTokLoc


    Start of cpLexer_UpdateTokLoc for the token "swblk_t" of size 7
    End of cpLexer_UpdateTokLoc
    Start of cpLexer_TokenStrValStore
    Start of utMemAlloc
    Allocating Memory of 8 bytes. Total Allocated 13928
    End of utMemAlloc
    Start of utMemAlloc
    Allocating Memory of 7 bytes. Total Allocated 13935


    D:\C_Yacc>

    Bug Description:
    ==================
    As you can see, the control exits inbetween without printing the "End of utMemAlloc" at the end of program execution. I was expecting the print statments "Memory Allocation Failed" incase of memalloc failure. But that is also not printed in the console screen. Some times I will get a message box saying "The instruction at "0x7c93426d" referenced memory at "0x000007e8". The memory could not be read". After this my program ends as said above. Could any one please advice me to overcome this issue?

    Thanks
    Anand V
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The bug may be somewhere else. probably you have heap overrun...

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    10

    Unhappy

    Quote Originally Posted by Bayint Naung View Post
    The bug may be somewhere else. probably you have heap overrun...

    I believe heap memory is not just 13935 (13.6kb)

    Also I'm getting the error message some times at 8000 bytes also. This is really confusing for me and i got struck at this point. Request any one to provide inputs on this.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I mean heap overflow?
    Code:
    char *p = malloc( 10 );
    p[12] = 'a';

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    But in my piece of code am not accessing the memory location i created. I'm sure the program exits when it tries to allocate memory (pAllocAddr = malloc(nMemSize); ) because i can able to see the print statement "Allocating Memory of 7 bytes. Total Allocated 13935" but after which the program ends, which means the last line executed was nothing but pAllocAddr = malloc(nMemSize);

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    But you use the memory returned from the function rite?
    There you may have overwritten the memory you don't belong.
    Just isolate the function utMemAlloc() and you'll see the bug is somewhere else.

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Using malloc without using free will cause stack overflow.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    Quote Originally Posted by Babkockdood
    Using malloc without using free will cause stack overflow.
    More likely, it will cause a memory leak.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    How is DEBUG_PRINT defined? If it's writing to a buffered stream you may not see its output when it crashes.

    As always, for more information one should run in the debugger, preferably with debug code.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I have defined DEBUG_PRINT as follows,

    Code:
    #define DEBUG_PRINT(PRINT_PARAM) printf PRINT_PARAM

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I'm not using free() anywhere in my code. Because i need all the information i created dynamically till my program ends. I'm badly in need of 3 to 5 mb of heap memory for storing all my datas which i parsed from a file. Is there any other way to store the datas in a structured format?

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Debugging tools are the best solution, if not try to find where you read that memory after the point that it fails.

    Alternatively change DEBUG_PRINT and make it write on a log file

    Or use perror() to print since stderr is unbuffered

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by anandvn View Post
    I'm not using free() anywhere in my code. Because i need all the information i created dynamically till my program ends. I'm badly in need of 3 to 5 mb of heap memory for storing all my datas which i parsed from a file. Is there any other way to store the datas in a structured format?
    You can just free them before the program ends then. For 3-5mb you should use dynamically allocated memory as you do. In C there is no better way

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    It says "Press cancel to debug"

    So do it!

    It will point you at the fault instruction / line of code.

    Examine the pointer variable which contains 0x7E8 (that's the fault address). Is 2024 (that's 0x7E8) significant for other reasons (maybe you just read it from a file).

    Hypothesise how your program got into this state, and work your way back through the program until you discover a cause.

    Set breakpoints along the way and re-test your code. Examine the program state at each breakpoint. Often, trouble starts way before an actual crash.
    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.

  15. #15
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by laserlight View Post
    More likely, it will cause a memory leak.
    Ah, that's it. Basically, don't use malloc without using free.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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