954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

free memory

1. #include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
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]);
}
}
}
//FUNCTION-memalloc
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;
return y;
}

the line

free y


cause problems. should i use it after

return y

2. Is there any way to check whether the memory is freed?

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

but i am getting the error

test.c:41: error: expected ‘;’ before ‘y’

where 41st line is

free y
vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

free(y); ?

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

tried that also. still error persists

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

@gerard4143
thats rt. Since i am using the allocated memory of y for a, i should free a and not y. then the program is

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
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 a;
}
//FUNCTION-memalloc
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;
}

still same error comes.test.c:20: error: expected ‘;’ before ‘a’

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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
 
Your program creates a memory leak by free'ing a before free'ing the other allocated memory areas.


I hope, the above quote means I should free y before freeing a. then segmentation fault comes.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
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(a);
}
//FUNCTION-memalloc
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);
  return y;
}
vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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?

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
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
 

1.Any help? I told you what's wrong with your code. Correct the problems and your code will work.
sorry, I didnt see the second page

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
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(a);
}
//FUNCTION-memalloc
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]);	  
    } 
  //free(y);
  return y;
}

2.The above program works good. But why is the line 43 (now shown as comment)

free(y)


not workig, where

free(a)


works good? (Both are 2 dimensional pointers, and I dont see any difference.)
3.The above program has no memory leakage and no more freeing of memory is needed in the above program. right?
4. One fundamental doubt. I am freeing the allocated memory inside the function itself, then which memory location is being accessed by the main program for a?
Thanks...

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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
 
for(i = 0; i < DIM_1; i++) { free(a[i]); }

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?

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
 

ok. thats the answer i needed. thanks

vineeshvs
Light Poster
46 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: