Thread: A question related to dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    16

    A question related to dynamic memory allocation

    Hi,

    I have a really basic question about dynamic memory allocation. Is it necessary to free memory allocated to matrix temp (in function transpose) ?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    struct dMat {
    	int rows;
    	int cols;
    	double **t;
    };
    
    struct dMat allocate(int rows, int cols)
    {
    	struct dMat t;
    	int i;
    	t.rows=rows;
    	t.cols=cols;
    	t.t=(double **)malloc(t.rows * sizeof(double *));
    	for(i = 0; i < t.rows; i++)
    		t.t[i] = (double *) malloc(cols * sizeof(double));
    	return t;
    }
    
    struct dMat transpose(struct dMat t)
    {
    	struct dMat temp;
    	int i, j;
    	temp.cols = t.rows;
    	temp.rows = t.cols;
    	temp = allocate(temp.rows, temp.cols);
    
    	for(i=0; i<temp.rows; i++)
    		for(j=0; j<temp.cols; j++)
    			 *(*(temp.t+i)+j)=*(*(t.t+j)+i);
    
    	return temp;
    }
    
    void display(struct dMat t){
    	int i,j;
    	for(i=0;i<t.rows;i++) {
    		for(j=0;j<t.cols;j++)
    			printf("%.3f ",*(*(t.t+i)+j));
    		printf("\n");
    	}
    	printf("\n");
       printf("Dimension = [%d, %d]\n\n", t.rows, t.cols);
    }
    
    int main() {
    	struct dMat beta;
    	int i, j;
    
    	beta.rows = 5;
    	beta.cols = 6;
    	beta = allocate(beta.rows, beta.cols);
    
    	for(i = 0; i < beta.rows; i++)
    		for(j = 0; j < beta.cols; j++)
    			*(*(beta.t+i)+j) = 2.5*(i+j);
    
    	display(beta);
    	display(transpose(beta));
        free(beta.t);
        getch();
    	return 0;
    }

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Generally speaking, most OS'es will cleanup when the program finishes, but it is the programmers responsibility to explicitly clean up resources when he needs them no more.

    As you have written an allocate, write it's sister deallocate to perform the free operations, and care to code those operations so that the rows are freed before the master array pointer.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your transpose allocates memory - you are unable to free

    in general - structs in C do not pass by value - they are passed by pointer to avoid unneded copy (and also avoid creating copies of object that point to the same dynamically allocated memory)

    I think you should rethink you interface to make the memory management more clear and memory-leak free
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:34 AM
  3. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  4. Dynamic Memory Allocation
    By fr0ggs in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 03:34 AM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM