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?

Edited 5 Years Ago by vineeshvs: one more q

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]

Edited 5 Years Ago by WaltP: n/a

but i am getting the error

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

where 41st line is

free y

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.

@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’

Edited 5 Years Ago by vineeshvs: n/a

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;
}

Edited 5 Years Ago by gerard4143: n/a

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;
}

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.

Edited 5 Years Ago by gerard4143: n/a

any help?

Any help? I told you what's wrong with your code. Correct the problems and your code will work.

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...

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;
}

Edited 5 Years Ago by gerard4143: n/a

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;
}

for(i = 0; i < DIM_1; i++)
{
free(a);
}

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?

Edited 5 Years Ago by vineeshvs: n/a

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's, the memory associated with each of the arrays of double is going to be leaked, as there's no way to free it.

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.

// You can try this code

#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]);
		}
  }
  return 0;
}

//FUNCTION-memalloc

double** memalloc(int M,int N)
{
  int i;
  double **y;

  y = (double**) malloc(M* sizeof(double *));

  if(y == NULL)
  {
		printf("out of memory\n");
		return 0;
  }

  for(i = 0; i < N; i++)
  {
		y[i] = (double*) malloc(N * sizeof(double));

		if(y[i] == NULL)
		{
			printf("out of memory\n");
			return 0;
		}
  }

  free (y);

  return y;
}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Comments
Did you read any of the posts above?

@sourabh17

Your freeing y and then returning the free'd pointer to be used which is a big no no, plus you haven't freed the addition memory allocated in your for loop... Please read the previous postings for a better explanation.

Edited 5 Years Ago by gerard4143: n/a

1. I am using a function 'free1' to free memory of 2D pointer 'a'. Is there any memory leak. ( I am still learning valgrind not yet ready to work on it)

#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);
void free1(double **a,int M);

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);
    free1(a,DIM_1);
    getchar();
    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;
}
void free1(double **a,int M)
{
     int i;
    for(i = 0; i < M; i++)
    {
        free(a[i]);
    }
free(a);
}

2. which is a good programming practice?
a. free the memory of a varible when that variable is not used in the lines below in main()?
b. free all the variables at the end of main() (I think this is useless, since by the end of main(), we no longer need any memory)

Edited 5 Years Ago by vineeshvs: n/a

I ran valgrind on your program and it produced this.

==10491== HEAP SUMMARY:
==10491==     in use at exit: 0 bytes in 0 blocks
==10491==   total heap usage: 4 allocs, 4 frees, 96 bytes allocated
==10491== 
==10491== All heap blocks were freed -- no leaks are possible

Which is good, no leaks possible.

Your second question, free allocated memory when its no longer needed.

This article has been dead for over six months. Start a new discussion instead.