Thread: warnings- dynamic memory allocation - malloc

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Exclamation warnings- dynamic memory allocation - malloc

    I have huge problem with pointers from few days, i have written the transpose function in two ways, the one just below the main with malloc never works and the one without malloc in quotes works perfect, i have to write a program using malloc, please can any one help me.

    Code:
    //the .h file is as follows
    int size_b_row1=4;
    int size_b_col1= 4;
    void transpose();
    //void transpose(double matrix[size_b_row1][size_b_col1], double matrix_t[size_b_col1][size_b_row1]);
    
    
    //the .c file is as follows 
    int main() 
    {
        double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
        double TC[4][4];
    
    
        transpose(C, TC);
    
        printf("%f", TC[3][3]);
        puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
        return EXIT_SUCCESS;
    }
    
    
    
    void transpose()
    {
        int i,j,k;
        double **matrix;
        double **matrix_t;
    
    
            matrix=(double **) malloc(sizeof(double *)*size_b_row1);
            for( k=0;k<size_b_row1;k++)
            {
                matrix[k]=(double *) malloc(sizeof(double*)*size_b_col1);
            }
            matrix_t=(double **) malloc(sizeof(double *)*size_b_row1);
            for( k=0;k<size_b_row1;k++)
            {
                matrix_t[k]=(double *) malloc(sizeof(double*)*size_b_col1);
            }
    
    
        for(i=0;i<size_b_row1;i++)
        {
            for(j=0; j<size_b_col1;j++)
                matrix_t[j][i] = matrix[i][j];
        }
      
    /*
    
    void transpose(double matrix[size_b_row1][size_b_col1], double matrix_t[size_b_col1][size_b_row1])
    {
        int i,j;
        for(i=0;i<size_b_row1;i++)
        {
            for(j=0; j<size_b_col1;j++)
                matrix_t[j][i] = matrix[i][j];
        }
    }
    */
    Last edited by raviraj123; 06-05-2013 at 07:11 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Actually, neither of your functions will work if size_b_col1 is not equal to size_b_row1. You also need to make up your mind whether you want to pass data to and from your functions using function arguments, or using global variables. Global variables are generally not considered a good idea.

    But, to answer your question ....

    At no point in the (uncommented) function are any values assigned to elements of matrix. Transposing an uninitialised array gives an uninitialised array. You have have no code that copies C (from main()) to matrix (in transpose()), nor do you have any code that copies matrix_t (in transpose()) back to TC (in main()). In contrast, your commented function is modifying the arrays in main() directly (since they are passed as arguments).

    The type conversions (double *) or (double **) are not required on a malloc() call and, in fact, it is a bad idea to use them. Make sure you have done #include <stdlib.h> if you are using malloc().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    the whole program works better with the second function with it , i have the out put also,but when i use malloc ,it compiles with out errors and gives me just zero. if you know where am i doing the mistake please point out me. the code which runs perfect is below
    Code:
    //h file 
    int size_b_row1=4;
    int size_b_col1=4;
    void transpose();
    
    
    // .c file 
    int main() {
    
    
    	double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
    	double TC[4][4];
    
    
    	transpose(C, TC);
    	printf("%f", TC[3][3]);
    	puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    	return EXIT_SUCCESS;
    }
    
    
    void transpose(double matrix[size_b_row1][size_b_col1], double matrix_t[size_b_col1][size_b_row1])
    {
    	int i,j;
    	for(i=0;i<size_b_row1;i++)
    	{
    		for(j=0; j<size_b_col1;j++)
    			matrix_t[j][i] = matrix[i][j];
    	}
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why would you think it makes sense to allocate "matrix" inside the transpose function?
    Where the heck is the data supposed to come from if the array to hold it doesn't even exist until transpose is called?
    How do you think it is going to be useful to pass arguments to a function that specifies that it does not specify the need for any arguments?

    Hint: lines 11 and 12 are wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    this is just a part of my code , i dont think so you really understood the problem, i have to write it like this because i was told to write like this. i found an another way by using malloc in it ,the code is in my first thread-post , have a look at it it just returns zero, if you think the lines 11 and 12 are wrong copy paste the code in your compiler and run it you will get the out put as 3.00000 , and now coming to the point i was asking the question about malloc not the one which runs perfectly.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It's not clear how you're supposed to use double ** and malloc() for this program. Are you supposed to convert C and TC in main to be of type double **, allocate them and then fill then with values, and then call a transpose that takes two double** parameters? Otherwise, if transpose is taking two double[][] as parameters, then I don't see the point in transpose using double** and malloc internally.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by raviraj123 View Post
    this is just a part of my code , i dont think so you really understood the problem, i have to write it like this because i was told to write like this. i found an another way by using malloc in it ,the code is in my first thread-post , have a look at it it just returns zero, if you think the lines 11 and 12 are wrong copy paste the code in your compiler and run it you will get the out put as 3.00000 , and now coming to the point i was asking the question about malloc not the one which runs perfectly.
    No, I have not misunderstood anything.
    No I did not say lines 11 and 12 would not compile. Of course they will compile. The problem is that you can't do it that way if you are meant to use malloc to allocate these using the array of arrays technique.

    If my questions didn't give you a serious wake-up call as to what kinds of things you have very wrong, go back and ponder on them again until you understand your mistakes. Then post some updated code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation with malloc
    By duif in forum C Programming
    Replies: 6
    Last Post: 11-28-2012, 09:05 AM
  2. Help! Using dynamic allocation and malloc
    By jh294 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 01:13 PM
  3. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  4. Memory allocation without malloc
    By messi89 in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 07:27 AM
  5. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 07:54 AM

Tags for this Thread