E
Eitan Michaelson
Hi,
Can any one tell me what's wrong with this code?
It leaks (acceding to bound checker), when it attempts to reallocate memory.
The code is not pure C, but with minor adjustments any C compiler would
compile this.
Thank you
Eitan Michaelson.
////////////////////////////////////////////////// Code Starts Here
///////////////////////////////////////////////////////////////////////
// This code attempts to create a pointer to a structure,and then allocate a
buffer for that structure, so it would hold an array of structures.
// in each loop I’m trying to reallocate the structure array so it would
grow by 1, and then I'm trying to add data into it.
// one of the structure elements is a void*, which in turn is allocated by
malloc.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_STRUCT_SIZE (50)
struct st_Test
{
char szChar[32];
int iInt;
void *pVoid;
};
void main(void)
{
// Pointer to the structure
st_Test *pst_Test;
pst_Test = NULL; // pointer not valid yet
for(int i = 0;i <=MAX_STRUCT_SIZE;i++)
{
if(!pst_Test) // not yet allocated
pst_Test = (st_Test*)malloc(1 * sizeof(st_Test));
else // Resize by 1
pst_Test = (st_Test*)realloc(pst_Test,(i+1)
*sizeof(st_Test)); /// This is the Leak /////////
// Reset the buffer we got
memset(&pst_Test,'\0',sizeof(st_Test));
// Add some data to the allocated struct
pst_Test.iInt = 64738;
strcpy(pst_Test.szChar,"poke 53280,1");
// allocate buffer for the void* element ////// This is
where bound checker found the problem ///
if(!pst_Test.pVoid)
pst_Test.pVoid = (char*)malloc(32);
strcpy((char*)pst_Test.pVoid,"commodre 64");
}
////////////////////////////////////////////////// End
///////////////////////////////////////////////////////////////////////
Can any one tell me what's wrong with this code?
It leaks (acceding to bound checker), when it attempts to reallocate memory.
The code is not pure C, but with minor adjustments any C compiler would
compile this.
Thank you
Eitan Michaelson.
////////////////////////////////////////////////// Code Starts Here
///////////////////////////////////////////////////////////////////////
// This code attempts to create a pointer to a structure,and then allocate a
buffer for that structure, so it would hold an array of structures.
// in each loop I’m trying to reallocate the structure array so it would
grow by 1, and then I'm trying to add data into it.
// one of the structure elements is a void*, which in turn is allocated by
malloc.
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_STRUCT_SIZE (50)
struct st_Test
{
char szChar[32];
int iInt;
void *pVoid;
};
void main(void)
{
// Pointer to the structure
st_Test *pst_Test;
pst_Test = NULL; // pointer not valid yet
for(int i = 0;i <=MAX_STRUCT_SIZE;i++)
{
if(!pst_Test) // not yet allocated
pst_Test = (st_Test*)malloc(1 * sizeof(st_Test));
else // Resize by 1
pst_Test = (st_Test*)realloc(pst_Test,(i+1)
*sizeof(st_Test)); /// This is the Leak /////////
// Reset the buffer we got
memset(&pst_Test,'\0',sizeof(st_Test));
// Add some data to the allocated struct
pst_Test.iInt = 64738;
strcpy(pst_Test.szChar,"poke 53280,1");
// allocate buffer for the void* element ////// This is
where bound checker found the problem ///
if(!pst_Test.pVoid)
pst_Test.pVoid = (char*)malloc(32);
strcpy((char*)pst_Test.pVoid,"commodre 64");
}
////////////////////////////////////////////////// End
///////////////////////////////////////////////////////////////////////