Thread: when use free here

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    when use free here

    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

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    In the calling function, free your memory just before you either exit, or call the function again so that you don't lose a reference to the memory before releasing it.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    I thougt I would only free the last ones allocated , not everyone then ? Or? Because I call the function who allocates several times.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    You call the function that allocates memory multiple times with the same pointers. Each time you call the function, the memory that the pointers were referring to before is lost when you point them to new memory. Because of that, you need to be careful to release the memory before the next call.
    Code:
    do {
      createSelectedArrays(a, b, c);
      /* Use a, b and c */
      free(a);
      free(b);
      free(c);
      for (i = 1; i < n; i++) {
        if (case one) {
          createSelectedArrays(a, b, c);
          /* Use a, b and c */
          free(a);
          free(b);
          free(c);
        }
        else {
          /* Something else */
        }
      }
    } while (condition);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM