Two questions:
- How to quickly clear an array of a structure?
- How to free memory allocated by the structure’s member?
Code:
struct sComputerNames
{
TCHAR *sName; // Using a pointer here to minimize stack memory.
};
TCHAR *sComputer = (TCHAR *) calloc(2048+1, sizeof(TCHAR));
struct sComputerNames sCN[4096] = {0};
_tcscpy(sComputer,L"PC1");
sCN[0].sName = (TCHAR *) calloc(128,sizeof(TCHAR));
_tcscpy_s(sCN[0].sName,128,sComputer);
// What is a better way to clear out the structure array?
for (DWORD i=0;i<4096;i++)
{
free(sCN[i].sName);
sCN[i].sName=NULL;
}
// Assign a new value
_tcscpy(sComputer,L"PC2");
sCN[0].sName = (TCHAR *) calloc(128,sizeof(TCHAR));
_tcscpy_s(sCN[0].sName,128,sComputer);
free(sCN);sCN=NULL; // Erroring here - how to free memory allocated by sCN's members?
free(sComputer);sComputer=NULL;
Thank you!
Clearing the array is easy:
As for the rest, you have some confusion about trying to free sCN which you didn’t malloc(), and trying to free lots of names when you only calloc()’d one of them.