Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with generic free store memory deleter 1

Status
Not open for further replies.

graetzd

Technical User
Jun 29, 2001
17
US
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
//***********************************************************


 
why not put a constructor and destructor in your structs to handle it for you?

Matt
 
Zyrenthian,

Thank you. I was trying to avoid learning about constructors and destructors for awhile but I forged ahead and have modified my structure to be a class and now I have been able to successfully use a destructor to free up huge free store memory that I allocate to store arrays of structures. Thanks again.

David G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top