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