473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I have a problem with the "free()" function in a program written in C.

1 New Member
This is the full code of my program,which I need to write for a homework assignment. It worked without the "free()" function, but I wanted to optimize it. Adding the "free()" function led to crashes while it calculates the result, and I couldn't find the reason why.The problematic fraction is on lines 91 to 94.
Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define MAXDIM 200
  5.  
  6. /*This program lets the user do some mathematical operations with matrixes.
  7. It is in early stage, so only one operation is available - calculating a determinant*/
  8.  
  9. /*This program is made just for a project I have been assigned to make.
  10. I'm in early stage of studying Computer Science, so some of my code may seem unusual.
  11. I'm open for advices of how to improve my code writting.*/
  12.  
  13. /*Each function is described before it's implementation*/
  14. void read(int**,int,int);
  15. void print(int**,int,int);
  16. int** allocateMatrix(int,int);
  17. void applyOperation(int**,int,int,void (*)(int**,int,int));
  18. void enterDim(int*,int*);
  19. int adjugateMatrix(int**,int,int,int);
  20. int** subMatrix(int**,int,int,int,int);
  21. int det(int**,int,int);
  22.  
  23. int main()
  24. {
  25.     int **matrix,n,m;
  26.     enterDim(&n,&m);
  27.     matrix = allocateMatrix(n,m);
  28.     read(matrix,n,m);
  29.     printf("The determinant is: %d ",det(matrix,n,m));
  30.     return 0;
  31. }
  32.  
  33. /*This function lets the user to enter the matrix*/
  34. void read(int **matrix,int dim1,int dim2){
  35.     printf("Enter the matrix with %d rows and %d columns:\n",dim1,dim2);
  36.     printf("(Enter %d elements separated by space on %d lines)\n",dim2,dim1);
  37.     int **i,*j;
  38.     for(i = matrix; i < matrix + dim1; i++)
  39.         for(j = *i; j < *i + dim2; j++){
  40.             scanf("%d",j);
  41.         }
  42. }
  43.  
  44. /*This function prints a matrix on the screen*/
  45. void print(int **matrix,int dim1,int dim2){
  46.     int **i,*j;
  47.     for(i = matrix; i < matrix + dim1; i++){
  48.         for(j = *i; j < *i + dim2; j++){
  49.             printf("%-5d",*j);
  50.         }
  51.         putchar('\n');
  52.     }
  53. }
  54.  
  55. /*This function allocates space for a matrix with dim1 rows and dim2 columns and returns a pointer-to-pointer-to-int*/
  56. int** allocateMatrix(int dim1,int dim2){
  57.     int i,**matrix = (int **)malloc(dim1*sizeof(int *));
  58.     for(i = 0; i < dim2;i++){
  59.         *(matrix + i) = (int *)malloc(dim2*sizeof(int));
  60.         if((*matrix + i) == NULL){
  61.             puts("Unable to allocate memory!!!");
  62.             exit(EXIT_FAILURE);
  63.         }
  64.     }
  65.     return matrix;
  66. }
  67.  
  68. /*This function will be used later for easier operation selection*/
  69. void applyOperation(int** matrix,int dim1,int dim2,void (*oper)(int**,int,int)){
  70.     (*oper)(matrix,dim1,dim2);
  71. }
  72.  
  73. /*This function lets the user define the dimensions of the matrix*/
  74. void enterDim(int *n,int *m){
  75.     puts("Enter the two dimensions:");
  76.     do{
  77.         scanf("%d",n);
  78.         if((*n<1)||(*n>MAXDIM))puts("Invalid dimension.Try again!!!");
  79.     }while((*n<1)||(*n>MAXDIM));
  80.     do{
  81.         scanf("%d",m);
  82.         if((*m<1)||(*m>MAXDIM))puts("Invalid dimension.Try again!!!");
  83.     }while((*m<1)||(*m>MAXDIM));
  84. }
  85.  
  86. /*Calculates the adjoint matrix of the element on the n-th row and m-th column*/
  87. int adjugateMatrix(int **matrix,int dim1,int n,int m){
  88.     int i,d,**newMatrix = subMatrix(matrix,dim1,dim1,n,m);
  89.     d = ((int)pow(-1.0,(double)(n+m)))*det(newMatrix,dim1-1,dim1-1);
  90.  
  91.     /*for(i = 0; i < dim1-1; i++){
  92.         free(*(newMatrix + i));
  93.     }
  94.     free(newMatrix);*/
  95.     /*The above comment is the problematic fraction.
  96.     I've tested the program for up to six rowed matrix, and it worked without the above section.
  97.     But I want to make the program work for big "n"s.
  98.     So I tried to free the allocated memory, when unneeded.
  99.     But that led to a crash for bigger than 3x3 matrix.*/
  100.  
  101.     return d;
  102. }
  103.  
  104. /*This function makes a new matrix with the n-th row and m-th column removed*/
  105. int** subMatrix(int **matrix,int dim1,int dim2,int n,int m){
  106.     int i,j,k,l,**newMatrix = allocateMatrix(dim1 - 1,dim2 -1);
  107.     for(i = 0,k =0; i < dim1; i++){
  108.         if(i == n)continue;
  109.         for(j = 0, l = 0; j < dim2; j++){
  110.             *(*(newMatrix + k) + l) = *(*(matrix + i) + j);
  111.             if(j == m)continue;
  112.             l++;
  113.         }
  114.         k++;
  115.     }
  116.     return newMatrix;
  117. }
  118.  
  119. /*This function calculates the determinant using recursion*/
  120. int det(int **matrix,int dim1,int dim2){
  121.     int d = 0,i;
  122.     if(dim1 != dim2){
  123.         puts("Determinant is defined only for square matrix!!!");
  124.         exit(EXIT_FAILURE);
  125.     }
  126.     else{
  127.         if(dim1 <= 2){
  128.             return (**matrix)*(*(*(matrix + 1) + 1)) - (**(matrix + 1))*(*(*matrix + 1));
  129.         }
  130.         else{
  131.             for(i = 0; i < dim1; i++){
  132.                 d += (*(*matrix + i))*adjugateMatrix(matrix,dim1,0,i);
  133.             }
  134.         }
  135.     }
  136.     return d;
  137. }
  138.  
Apr 10 '11 #1
1 1728
weaknessforcats
9,208 Recognized Expert Moderator Expert
A 3x3 matrix with:

123
456
789

is laid out in memory as:

123456789

With this in mind, if the elements of your array are int* and these int* point to the row elements, you will not be able have rows of different sizes nor can you have new arrays with different dimesions since there is no data for either the number of rows nor the number of elements in each row.

You might research how to use a chain link table. I know that Robert Sedgewick has examples in his books.

Other than that, you will need to be sure that your vision of the array is the same as malloc's.

you might also read http://bytes.com/topic/c/insights/77...rrays-revealed
Apr 11 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: TCMA | last post by:
I am looking for some tools to help me understand source code of a program written in C++ by someone else. Are there any non-commercial, open source C or C++ tools to reverse engineer C or C++...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: Bikini Browser | last post by:
Folks: I am not a programmer so I need some help here... I need a small program written that tells me how many days old my daughter is since she was born. She was born September 26th, 2000. ...
2
by: Manikandan | last post by:
Hi, I have a program written in .Net Framework 1.1 using Visual studio enterprise edition 2003. I tried compiling the same program in visual c# express edition 2005. I'm getting following...
2
by: Alenik1989 | last post by:
I wrote this code, but I'm not able to test it because im getting 3 errors. So I'm not sure that this code will work properly or not!!! the code in #include <stdio.h> #include <stdlib.h> int...
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
2
by: lacole | last post by:
I have an .exe program written in C# that will not run on a specific PC that has Windows XP on it. That same .exe program will run on other Windows XP PCs though. So it's something specific to that...
1
kirubagari
by: kirubagari | last post by:
Dear expert, i had written the code in my form Public Function HoldType(ByRef sType As String) As String If bOkClicked Then sType = "Rehold " End If
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.