Thread: Memory leaks and bad indeces: use of malloc

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Memory leaks and bad indeces: use of malloc

    Hi all,

    this is a follow-up on my questions about segmentation fault that was generated when I tried to write data in an output file.

    Thanks everybody that responded to that, I think that I have now located the error.

    In a completely unrelated function, some matrix was filled "outside" of its indeces. The funny thing was that nothing happened here, but rather the program crashed later when I was trying to write data into a file.

    Anyway, to prevent these memory issues in the future, I have some questions on malloc.


    8<-------------------
    float (* fun_matrix)[5];
    .
    .
    .
    *fun_matrix= malloc(10* sizeof(order_class);
    fun_matrix[10][5]=3.14;
    8<------------------

    Is this correct use of malloc? Did I get the indeces right?

    I read about using the "free" command to clear up memory when I am done. Is this necessary? If I don't use free, is the used memory still locked up after the program is finished? Can subsequent executions be affected?

    Thanks again for all help.

    Tor

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    *fun_matrix= malloc(10* sizeof(order_class);
    Malloc returns a pointer, you do NOT need to dereference fun_matrix. You want to put the pointer that malloc returns into fun_matrix so you may dereference it later using *fun_matrix. You also created an array of pinters here:
    float (* fun_matrix)[5];
    I hope you understand that if order_class is a float, you have just malloc()ed 10 times that much, mostlikely 80 bytes, why is this needed? What is order_class? A float? It will have to be type float in order to create an address for memory and put that into the pointer fun_matrix, or change the data type of fun_matrix to the same data type as order_class. A bit more code would be nice.

    Also, always free() memory that has been allocated! and next time, please use CODE TAGS.
    Last edited by stumon; 03-25-2003 at 03:10 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Sorry, missed something

    I should have cleaned up my exxample better. This is what I meant. The goal is to produce a 10x5 matrix of floats.

    8<-------------------
    float (* fun_matrix)[5];
    .
    .
    .
    *fun_matrix= malloc(10* sizeof(fun_matrix);
    fun_matrix[10][5]=3.14;
    8<-----------------

    Thanks.

    Tor

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I should have cleaned up my exxample better. This is what I meant. The goal is to produce a 10x5 matrix of floats.
    Thats fine, but this is just creating an 1-dimensional array of floats. you want this as a declaration:
    float fun_matrix[10][5];
    then you want to point malloc to one entry using:
    fun_matrix[0][0] = malloc(sizeof(fun_matrix));
    then:
    fun_matrix[0][1] = malloc(sizeof(fun_matrix));

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    or create a nested for loop to fill the array with pointers.

    Code:
    int i , ii;
    
    for (i = 0; i < 10; i++)
    	for(ii = 0; ii < 5; ii++)
    		fun_matrix[i][ii] = malloc(sizeof(fun_matrix));
    Last edited by stumon; 03-25-2003 at 03:45 PM.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    lol, im getting confused.
    The goal is to produce a 10x5 matrix of floats.
    So you just want floats? Then your declaration is wrong and you dont need to malloc anything. Use
    Code:
    float fun_matrix[10][5];
    
    //and add floats to this array however you want using this.
    
    fun_matrix[0][0] = 3.14;
    fun_matrix[0][1] = 0.00; 
    
    //and so on to fill it. No pointers needed and no malloc needed.
    This is for a multi-dem array of floats and the first was for a multi-dem array of pointers to floats. Which ever you need, there you go.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Smile Clarification

    Yes, if I know the size from the start, then I don't need malloc.

    So let's clarify one final (?) time
    Code:
    float (* fun_matrix)[5];
    .
    .
    .
    size_of_matrix = complex_calculation();
    *fun_matrix= malloc(size_of_matrix* sizeof(fun_matrix);
    Is this the correct way to dynamically generate a matrix of size size_of_matrix*5? Or is it 5*size_of_matrix? Or is it completely wrong?

    Thanks again guys. Sorry for the confusion.

    Tor

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Not completely, but still wrong.

    Here, this should help.

    Code:
    //To create a 5 element array of pointers to allocated memory, type float
    float *matrixPtr[5];
    
    //to create a 5 element array of floats. 
    float matrices[5];
    
    //to create a multidimensional array of pointers.
    float *matrixPtr[5][10];   //I dont see why this would be needed ever.
    
    //to create a multidimensional array of floats.
    float matrices[5][10];
    
    //to allocate memory to the array of pointers.
    matrixPtr[0] = malloc(sizeof(float));
    
    //to allocate memory to a multidimensional array of pointers.
    matrices[0][0] = malloc(sizeof(float));
    You dont want to do this,
    *fun_matrix= malloc(size_of_matrix* sizeof(fun_matrix);
    This will dereference the pointer fun_matrix and store the address of the memory allocated by malloc() in the float pointed to by fun_matix. You want to store the address in fun_matrix, so you get rid of the '*'. You also dont really want to change the "sizeof(fun_matrix)" by multiplying it by the size_of_matrix. The keyword "sizeof" gets the size of that float in bytes and allocated that much room. Just use something = malloc(sizeof(float));. That should explain everything! If not, let me know.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Smile Thanks Stumon

    That was comprenhensive and very useful.

    2 follow up questions

    Code:
    //to allocate memory to a multidimensional array of pointers.
    matrices[0][0] = malloc(sizeof(float));
    What is the significance of the zeros here? Are they just dummies to be replaced with the real size of the matrix?

    Also, my end objective is this: to construct a 2D matrix of floats (not pointers to floats) with one side of 5, and the other dynamically determined by input to the program. Is this the right way to do it?

    Code:
    matrices[input_variable][5] = malloc(sizeof(float));
    Also, I would like matrices to be accessible in all functions, not just in main where I presume this would be declared.

    Thanks again.

    Best,

    Tor

  10. #10
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    What is the significance of the zeros here? Are they just dummies to be replaced with the real size of the matrix?
    No, these are the elements of the array. 10 rows, 5 columns. This puts the first malloc()ed memory into the 1st row of the 1st column. If you wanted the malloc()ed memory to be put in the 2nd row of the 3rd column, you use
    Code:
    matrices[1][2] = malloc(sizeof(float));
    
    //same way for the 7th row of the 5th column use:
    
    matrices[6][4] = malloc(sizeof(float));
    Also, my end objective is this: to construct a 2D matrix of floats (not pointers to floats) with one side of 5, and the other dynamically determined by input to the program. Is this the right way to do it?
    If this is the case, you do not need to allocate any memory. Just declare a multi-dimensional array, and put the entries in that array then they are received by the user. Wait one second, I will create a small program showing you how to do this and post the code in the next post ;D. BRB

  11. #11
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Here is a small program to show you how to input entries from a user into a multi-dimensional array.
    Code:
    #include <stdio.h>
    
    #define ROWS 4
    #define COLUMNS 2
    
    int main()
    {
    	float matrices[ROWS][COLUMNS] = {0.0};
    	int r, c;
    
    	printf("Enter the first 4 rows, then the next 4 rows to fill 2 columns");
    
    	for (c = 0; c < COLUMNS; c++){
    		for (r = 0; r < ROWS; r++){
    			scanf("%f", &matrices[c][r]);
    		}
    	}
    	
    	for (c = 0; c < COLUMNS; c++){
    		for (r = 0; r < ROWS; r++){
    			printf("%.1f\n", matrices[c][r]);
    		}
    	}
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Talking Thanks a lot

    I really appreciate the time and effort. I think I am finally getting this thing to work...

    Best,

    Tor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Problem with _beginthread()
    By nord in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 11:44 AM
  2. confused about arrays
    By sal817 in forum C Programming
    Replies: 17
    Last Post: 09-20-2004, 03:45 PM