When I am freeing my memory in this part of code...i am getting an error showing as :free(): invalid next size (fast)
int insertRecord(char *record,int recordSize,long dataPageNumber)
{
datapage *dataPage=(datapage *)malloc(sizeof(datapage));
readPage(dataPage,dataPageNumber);
slotentry slot;
//for checking and freeslotnumber storage
int freeSlotNumber=-1;
int negativeFlag=0;
int freeFlag=0;
if(recordSize+sizeof(slotentry)<=dataPage->cfs)
{
slot.slotsize = recordSize;
slot.slotaddress = dataPage->cfsptr;
dataPage->cfs -= (recordSize+sizeof(slotentry));
dataPage->cfsptr += recordSize;
dataPage->slotcount++;
memcpy(&dataPage->data[slot.slotaddress],record,recordSize);
free(dataPage);
return 1;
}
After executing free(dataPage) i am getting the above error...
typedef struct
{
int pagenumber;
int priority;
long dirPageNo;
long cfs;
int cfsptr;
int slotcount;
char data[1];
} datapage;
typedef struct
{
int slotaddress;
int slotsize;
} slotentry;
I had kept the free(dataPage) before the memcpy it's working fine but after memcpy it's not working.. and showing the error....Can any one help me from this issue...
You are probably getting this due to writing outside the boundaries if the dataPage->data
entry. This struct entry is just a single byte long, so unless slot.slotaddress==0
and recordSize==1
, you will be writing to whatever memory lies after the end of the datapage
struct
. This memory corruption is probably what is causing your free
error.
To track down this type of error, I recommend running your program through valgrind
:
valgrind progname args
This in this case, you will probably get messages about "invalid writes", which tell you that you are writing outside the boundaries of your arrays.