I don't know when to use free() here, I want to make sure that I remove all allocted memory. I allocate everytime I call my function, and that is a lot of times. Where and how can I use free so that the values of Store, Suppliers,Plants well be deleted, but I still want to be able to use my selectedStores ... etc.
Any ideas?
Code:
void createSelectedArrays(int* binaryDecisionArray,int** selectedStores,int** selectedPlants,int** selectedSuppliers,Environment* e){
// i have removed parts of the code here
Stores = mallocVector(numberOfStores+1);
Plants = mallocVector(numberOfPlants+1);
Suppliers = mallocVector(numberOfSuppliers+1);
// here I fill the arrays
*selectedSuppliers = Suppliers;
*selectedPlants = Plants;
*selectedStores = Stores;
// free(Suppliers);
// free(Plants);
// free(Stores);
}
Code:
do{
createSelectedArrays(binaryDecisionArray,&selectedStores,&selectedPlants,&selectedSuppliers,e);
error = get_opt_Standortkonfiguration(&optZFW,flowSuppliersToPlantsForAllProducts,flowPlantsToStoresForAllProducts,flowStoresToCustomerregionsForAllProducts,selectedSuppliers,selectedPlants,selectedStores);
for(i = 1; i<binaryDecisionArray[0]; i++){
if(binaryDecisionArray[i]==0){
binaryDecisionArray[i]=1;
createSelectedArrays(binaryDecisionArray,&selectedStores,&selectedPlants,&selectedSuppliers,e);
error = get_opt_Standortkonfiguration(&ZFW,flowSuppliersToPlantsForAllProducts,flowPlantsToStoresForAllProducts,flowStoresToCustomerregionsForAllProducts,selectedSuppliers,selectedPlants,selectedStores);
costReduction[i]=optZFW-ZFW;
binaryDecisionArray[i]=0;
if(costReduction[i]<=0)
binaryDecisionArray[i]=10; //
}
}
countNull = countZero(binaryDecisionArray);
}while(countNull!=0);//do-while