I am trying to create a generic function that will delete memory
I have created on the Free Store (using operator new). My idea
is to have a Switch statement inside the function that is
activated by an incoming parameter. My problem is that I am
using pointers within structures and I may be accessing that
information wrong. Here are the two structures seen in code
below:
struct SIM_RECORD_PACKAGE
{
struct RECORDS_PACKAGE* ptr_init_package;
struct SIM_PERIOD_RECORDS* ptr_per_data;
ushort NoPerData; //how many SIM_PERIOD_RECORDS there are
};
struct SIM_PERIOD_RECORDS
{
struct TREELIST_RECORD* ptr_liveR; //ptr to LIVE records
ulong LiveCount; //Count of LIVE records
ushort LiveSortedBy;
struct STAND_DATA* ptr_sd; //ptr to current stand data
};
**************************************
And here is a condensed version of code
I have tried.
***************************************
#1)***************************************************
Function1()
{
//I want this function to initiate a structure of type
//SIM_RECORD_PACKAGE and pass it around to many other
//pieces of the total code.
struct SIM_RECORD_PACKAGE *ptr_SRP;
//------------------------------
...
//Call up function to allocate memory on Free Store for
//an array of SIM_PERIOD_RECORDS structures inside of ptr_SRP
ptr_SRP = SetUpSRP(ptr_so); //go to #2below
//Now call up generic Free Store memory deleter
DeleteFreeStoreMemory((void*)ptr_SRP, INIT_SRP); //go to #3 below
}//end Function1()
#2)****************************************************
struct SIM_RECORD_PACKAGE* SetUpSRP(struct RECORDS_PACKAGE *ptr_so)
{
struct SIM_RECORD_PACKAGE SimPackage, *ptr_SRP;
//----------------------------------
...
//Make an array of SIM_PERIOD_RECORDS structures for each period
struct SIM_PERIOD_RECORDS (*SimRecords) = new struct SIM_PERIOD_RECORDS[PerToDo];
if(SimRecords == NULL)
Bailout(SendToBail);
memset( SimRecords, 0, sizeof(struct SIM_PERIOD_RECORDS)*PerToDo);
//Clear and set a pointer to the SIM_RECORD_PACKAGE that is going to get passed on
memset(&SimPackage, 0, sizeof(struct SIM_RECORD_PACKAGE));
ptr_SRP = &SimPackage;
//Set ptr_SRP->ptr_per_data at the SimRecords array made above
ptr_SRP->ptr_per_data = SimRecords;
...
//This returns a pointer back to call in Function1
return ptr_SRP;
}//end SetUpSRP()
#3) **************************************************
void DeleteFreeStoreMemory(void *ptr, ushort Type)
{
//struct SIM_RECORD_PACKAGE* ptr_SRP;
//----------------------------------------------------
//********************************************************
//Switch to an appropriate statement to handle all the
//deleting based on the incoming Type variable
//********************************************************
switch(Type)
{
//!!!!!! HELP - What should I do here !!!!!!!!!!
case INIT_SRP:
//ptr_SRP = (struct SIM_RECORD_PACKAGE*)ptr;
//delete [] ptr_SRP->ptr_per_data->ptr_sd;
//delete [] ptr_SRP->ptr_per_data;
//delete [] ( (struct SIM_RECORD_PACKAGE*)&ptr)->ptr_per_data;
break;
default:
break;
}//end of switch
}//end DeleteFreeStoreMemory()
//**********************************************************
I thank anyone in advance for any advice or comments
David H. Graetz
//***********************************************************
I have created on the Free Store (using operator new). My idea
is to have a Switch statement inside the function that is
activated by an incoming parameter. My problem is that I am
using pointers within structures and I may be accessing that
information wrong. Here are the two structures seen in code
below:
struct SIM_RECORD_PACKAGE
{
struct RECORDS_PACKAGE* ptr_init_package;
struct SIM_PERIOD_RECORDS* ptr_per_data;
ushort NoPerData; //how many SIM_PERIOD_RECORDS there are
};
struct SIM_PERIOD_RECORDS
{
struct TREELIST_RECORD* ptr_liveR; //ptr to LIVE records
ulong LiveCount; //Count of LIVE records
ushort LiveSortedBy;
struct STAND_DATA* ptr_sd; //ptr to current stand data
};
**************************************
And here is a condensed version of code
I have tried.
***************************************
#1)***************************************************
Function1()
{
//I want this function to initiate a structure of type
//SIM_RECORD_PACKAGE and pass it around to many other
//pieces of the total code.
struct SIM_RECORD_PACKAGE *ptr_SRP;
//------------------------------
...
//Call up function to allocate memory on Free Store for
//an array of SIM_PERIOD_RECORDS structures inside of ptr_SRP
ptr_SRP = SetUpSRP(ptr_so); //go to #2below
//Now call up generic Free Store memory deleter
DeleteFreeStoreMemory((void*)ptr_SRP, INIT_SRP); //go to #3 below
}//end Function1()
#2)****************************************************
struct SIM_RECORD_PACKAGE* SetUpSRP(struct RECORDS_PACKAGE *ptr_so)
{
struct SIM_RECORD_PACKAGE SimPackage, *ptr_SRP;
//----------------------------------
...
//Make an array of SIM_PERIOD_RECORDS structures for each period
struct SIM_PERIOD_RECORDS (*SimRecords) = new struct SIM_PERIOD_RECORDS[PerToDo];
if(SimRecords == NULL)
Bailout(SendToBail);
memset( SimRecords, 0, sizeof(struct SIM_PERIOD_RECORDS)*PerToDo);
//Clear and set a pointer to the SIM_RECORD_PACKAGE that is going to get passed on
memset(&SimPackage, 0, sizeof(struct SIM_RECORD_PACKAGE));
ptr_SRP = &SimPackage;
//Set ptr_SRP->ptr_per_data at the SimRecords array made above
ptr_SRP->ptr_per_data = SimRecords;
...
//This returns a pointer back to call in Function1
return ptr_SRP;
}//end SetUpSRP()
#3) **************************************************
void DeleteFreeStoreMemory(void *ptr, ushort Type)
{
//struct SIM_RECORD_PACKAGE* ptr_SRP;
//----------------------------------------------------
//********************************************************
//Switch to an appropriate statement to handle all the
//deleting based on the incoming Type variable
//********************************************************
switch(Type)
{
//!!!!!! HELP - What should I do here !!!!!!!!!!
case INIT_SRP:
//ptr_SRP = (struct SIM_RECORD_PACKAGE*)ptr;
//delete [] ptr_SRP->ptr_per_data->ptr_sd;
//delete [] ptr_SRP->ptr_per_data;
//delete [] ( (struct SIM_RECORD_PACKAGE*)&ptr)->ptr_per_data;
break;
default:
break;
}//end of switch
}//end DeleteFreeStoreMemory()
//**********************************************************
I thank anyone in advance for any advice or comments
David H. Graetz
//***********************************************************