Thread: a noob freeing memory...

  1. #16
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    this is a snaphot of the code.i hope it helps because i can't find a point where the pointer is dereferenced.
    Code:
    int **gene_pricing(int run_mode,int no_of_vehicles,int gene_dim,int *gene,
    		  int *edge_weight_section,int *demand,int capacity,int *fitness){
    	
            //various variables
    	register int i,j;
    	int int_fitness,sub_cost,ext_tour_size,ext_dim;
    	
    	int **prev_after,count;
    	
    	prev_after=malloc(no_of_vehicles*sizeof(*prev_after));
    	
    	if(prev_after==NULL){
    		 perror ("malloc");
    		 exit(EXIT_FAILURE);
    		  }
    	
    	for(i=0;i<no_of_vehicles;i++){
    		prev_after[i]=malloc(gene_dim*sizeof(**prev_after));
    		
                    if(prev_after[i]==NULL){ //Check for memory redudancy
    			 perror("malloc");
    			 exit(EXIT_FAILURE);
    		}
    	}
    		
    	for(i=0;i<no_of_vehicles;i++){
    		count=1;
    		for(j=0;j<gene_dim;j++){
    			if(gene[j]==i){
    				prev_after[i][j]=count;
    				count++;
    			}
    			else prev_after[i][j]=0;
    		}
    	}
    
      .
      . blah blah blah irrelevant stuff (nothing else allocated or used from the above)
      .
       
      *fitness=int_fitness; //some variable returned by reference externally
    
       return prev_after;
    }
    
    
    int main(){
    .
    .
    .
            int ext_fitness,i,j;
    	int **ext_matrix;
    	
    	ext_matrix=malloc(2*sizeof(*ext_matrix));
    	for(i=0;i<2;i++){
    		ext_matrix[i]=malloc(17*sizeof(**ext_matrix));
    	}
    			
    	ext_matrix=gene_pricing(0,2,17,gene17,edges17,demand17,10,&ext_fitness);
    	
            printf("\n\n%d\n\n",ext_fitness);
    		
    	printf("\n\n");	
    	for(i=0;i<2;i++){
    		for(j=0;j<17;j++) printf("%d ",ext_matrix[i][j]);
    		printf("\n");
    	}
    		
    	
    	for(i=0;i<2;i++) free(ext_matrix[i]);
    	free(ext_matrix);	
    	
    	return EXIT_SUCCESS;
    }
    any other suggestions???
    thanx for your help and patience.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >any other suggestions???
    Code:
    >	ext_matrix=malloc(2*sizeof(*ext_matrix));
    >	for(i=0;i<2;i++){
    >		ext_matrix[i]=malloc(17*sizeof(**ext_matrix));
    >	}
    These lines in main are completely unnecessary. This allocates memory for the matrix. But then you do the exact same thing in function gene_pricing. You don't need to allocate memory for the matrix twice. Now it's up to you where to do the allocation. You can either do it down in the function, or you can do it in main, then pass the matrix as an argument to the function.

    >if i don't free in main the code seems to leak exactly the double amount of bytes.
    Now we know why. Since you allocated the matrix twice, you would leak twice the amount.

  3. #18
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    These lines in main are completely unnecessary. This allocates memory for the matrix. But then you do the exact same thing in function gene_pricing. You don't need to allocate memory for the matrix twice. Now it's up to you where to do the allocation. You can either do it down in the function, or you can do it in main, then pass the matrix as an argument to the function.
    Now we know why. Since you allocated the matrix twice, you would leak twice the amount.
    thanks a lot i just learned that if i want to access an already allocated pointer with another i don't have to allocate it too
    pretty noob i think

    thank you very much... i appreciate your help and patience...

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >thank you very much... i appreciate your help and patience...
    Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 02:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 01:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM