Thread: can i reuse the dynamically allocated memory inside a function in main()

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    4

    Smile can i reuse the dynamically allocated memory inside a function in main()

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    
    int **transpose(int **x,int m,int n);
    
    
    
    main()
    {
      int nrows=2,ncolumns=2,i,j,k=0;
      
    
      //memory allocation for array x
      int **array;
      array = malloc(nrows * sizeof(int *));
      if(array == NULL)
        {
          printf("out of memory\n");
          return 0;
        }
    
      
      for(i = 0; i < nrows; i++)
        {
          array[i] = malloc(ncolumns * sizeof(int));
          if(array[i] == NULL)
    	{
    	  printf("out of memory\n");
    	  return 0;
    	}
        }
    
     
    
     
    
    
      //define x
      printf("x=\n");
      for(i=0;i<2;i++)
        {
    printf("\n");	  
      for(j=0;j<2;j++)	    
        {
          k=k+5;
          array[i][j]=i+j+k;
          printf("%d\t",array[i][j]);
        } 
        }
      printf("\n");
    
    
    
    
    
    
      //memory allocation for x_transpose, (storing the transpose returned by function)
    
      int **x_transpose;
      x_transpose = malloc(nrows * sizeof(int *));
      if(x_transpose == NULL)
        {
          printf("out of memory\n");
          return 0;
        }
    
      
      for(i = 0; i < nrows; i++)
        {
          x_transpose[i] = malloc(ncolumns * sizeof(int));
          if(x_transpose[i] == NULL)
    	{
    	  printf("out of memory\n");
    	  return 0;
    	}
        }
    
    
    
    
    
    
    
      //call function
      x_transpose= transpose(array,nrows,ncolumns);
    
    
    
    
    
    
      //display transpose
      printf("x_transpose=\n");
      for(i=0;i<2;i++)
        {
    printf("\n");	  
      for(j=0;j<2;j++)	    
        {
          printf("%d\t",x_transpose[i][j]);
        }
        }
    }
    
    
    
    
    
    
    
    
    
    //function_transpose
    
    int **transpose(int **x,int m,int n)
    {
      int nrows=n,ncolumns=m,i,j;
    
      //memory allocation for y,to store transpose
      int **y;
      y = malloc(nrows * sizeof(int *));
      if(y == NULL)
        {
          printf("out of memory\n");
          return 0;
        }
      for(i = 0; i < nrows; i++)
        {
          y[i] = malloc(ncolumns * sizeof(int));
          if(y[i] == NULL)
    	{
    	  printf("out of memory\n");
    	  return 0;
    	}	  
        }
    
    
    
    
     
      for(i=0;i<m;i++)
        for(j=0;j<n;j++)
          {
    	y[i][j]=x[j][i];
          }
      return y;
    }
    i allocated memory for storing y(transpose of matrix) inside the function. also i allocated memory(for x_transpose) for storing the same result in main program. can reuse the same memory in main also???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Huh? What gives? A cross-posting foul called by a cross-posting advocate?
    File archiving - Dev Shed

    Say it isn't so.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I don't ADVOCATE CROSS-POSTING!

    Anyone who does it a time-wasting idiot who doesn't deserve any help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. How are variables stored in memory.
    By ineedmunchies in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2010, 05:49 PM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM