Hello Guys,
I've been experiencing a very odd problem on using the free() function in lite-C. It works on some other instances but it'll give me a problem when I combine it with memcpy(). I have an accessing problem on the last index of the array.
To give you the idea I have an initializations of structures in a .h file
typedef struct PLAYER_SCORE_STRUCT
{
var m_nScore;
var m_nlevel;
var m_nSkin;
double m_nDate;
STRING *m_timeStr;
STRING *playerName;
}PLAYER_SCORE_STRUCT;
And a pointer on it..
PLAYER_SCORE_STRUCT *playScoreArr;
And now when I read the file and store it on playScoreArr:
playScoreArr = malloc(MAX_PLAYER_LIST * MAX_LEVEL * sizeof(PLAYER_SCORE_STRUCT));
numRecords = fileRead(fileName, dirPath, playScoreArr, 0);
for(indexer=0; indexer < numRecords; indexer++)
{
if(str_cmpi(playScoreArr[indexer].playerName, strPlayerName) == 1 )
{
int arrIndx = indexer; //get the index of the element to be removed
if(indexer == numRecords - 1 && (&playScoreArr[indexer] != NULL)) // last record
{
free(&playScoreArr[indexer]);
}
else
{
for(arrIndx; arrIndx < numRecords-1; arrIndx++) //iterate through the array starting from the index
{
memcpy(&playScoreArr[arrIndx], &playScoreArr[arrIndx+1],sizeof(PLAYER_SCORE_STRUCT));
}
//free the last record
if((&playScoreArr[numRecords-1]) != NULL) free(&playScoreArr[numRecords-1]);
indexer--; // to recheck new value of deleted position
}
numRecords--;
}
}
I've got a crashed on this line
free(&playScoreArr[indexer]);
It is the last index of the array.
Any idea why??
Please advise!