the line
free y
cause problems.
After 30+ posts you should know better by now.
Ground it for a month with no TV.should i use it after
return y
If you use it after return y will it be executed? What does return y do?
[EDIT]
I see this question has already been answered here . You didn't believe him?
[/EDIT]
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
tried that also. still error persists
Why are you free'ing y and then returning it?
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Why are you free'ing y and then returning it?
Good point. I missed that it was the same variable, "the forest for the trees," sorry.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Here's a general rule about malloc - The number of times that you call malloc, is the the number of times that you have to call free. Your program creates a memory leak by free'ing a before free'ing the other allocated memory areas.
And your error
free(a);
This was pointed out previously.
When you write programs that allocate and free memory, you should run them with a program like valgrind to ensure your not creating memory leaks.
Also, in C the main function is defined as
int main()
{
/*...*/
return 0;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Every time you call malloc, you create an independent area of memory that's identified by the pointer returned by malloc. So free'ing a or y only frees the memory allocated here on line 27.
y = malloc(M* sizeof(double *));
Now if you'll check lines 33 - 41 you'll note your calling malloc again in this loop.
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
You have to free the memory allocated here as well.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
any help?
Any help? I told you what's wrong with your code. Correct the problems and your code will work.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Yeah it works, but your using memory that you freed which is a no no.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
int main()
{
int i,j;
double **a;
a=memalloc(2,2);
printf("a\n");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
/*free the additional memory here*/
free(a);
return 0;
}
double **memalloc(int M,int N)
{
int i;
double **y;
y = malloc(M* sizeof(double *));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
free(y[i]);/*don't free the memory here*/
}
return y;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
A few pointers, the main function returns an integer and you shouldn't use magic numbers you should use a descriptive variable or macro..See below.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define DIM_1 3/*don't use magic numbers, instead use a descriptive name*/
#define DIM_2 3/*don't use magic numbers, instead use a descriptive name*/
double **memalloc(int M,int N);
int main()/*main returns an int*/
{
int i,j;
double **a;
a = memalloc(DIM_1, DIM_2);
for(i = 0; i < DIM_1; i++)
{
printf("\n");
for(j = 0; j < DIM_2; j++)
{
a[i][j]=i+j;
printf("%f\t",a[i][j]);
}
}
fputc('\n', stdout);
for(i = 0; i < DIM_1; i++)
{
free(a[i]);
}
free(a);
return 0;
}
double **memalloc(int M,int N)
{
int i;
double **y;
y = malloc(M* sizeof(double *));
if(y == NULL)
{
printf("out of memory\n");
return 0;
}
for(i = 0; i < M; i++)
{
y[i] = malloc(N * sizeof(double));
if(y[i] == NULL)
{
printf("out of memory\n");
return 0;
}
}
return y;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
In your function, you first allocate an array of M pointers to double (line 45), then line 55 allocates an array of N doubles for each of those pointers to double. In freeing you need to first free each of the arrays of N doubles (lines 30-33) and then finally free the memory associated with the M pointers to double on line 35.
If you free(a) before freeing all of the a[i]'s, the memory associated with each of the arrays of double is going to be leaked, as there's no way to free it.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
1.Is this step must? (since you havnt shown this in first post)
2.After freeing a in that way, why is
free(a)
needed?
You have to understand that each call to malloc returns an individual area of memory that has no association to other calls to malloc, so for each call to malloc you have to call free.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387