why does the memory increase step by step?

This is a discussion on why does the memory increase step by step? within the C Programming forums, part of the General Programming Boards category; Code: for ( int i = 0; i < 100; ++i ) { char **p = evaluation();//assign values to **p ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    why does the memory increase step by step?

    Code:
    for ( int i = 0; i < 100; ++i )
    {
         char **p = evaluation();//assign values to **p
    }
    why does memory increase?
    though defination of **p is local.( **p is local variable )

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You keep declaring a new **p. Stop that!

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    That's not the problem. It's deallocated when the scope ends.

    It's probably in the evaluation function.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    Quote Originally Posted by Adak View Post
    You keep declaring a new **p. Stop that!
    LOL!

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by cyberfish View Post
    That's not the problem. It's deallocated when the scope ends.

    It's probably in the evaluation function.
    Code:
    evalue function:
    usint** csMartrix(char **p,usint Punish,int *e)
    {
         usint equalPunish = 10;            
         usint gapPunish   = 5; 
         uint psNumber = caculateCombNum(e[1]);
         uint m = 0;
         usint *........ = (usint **) calloc (e[0],sizeof(usint*));
         usint preLineScore[psNumber]; 
         usint *st = (usint *) calloc (psNumber,sizeof(usint));
         for( uint pi = 0; pi < psNumber; ++pi) { preLineScore[pi] = 0; st[pi] = 0; }  
         ........ = st; 
         for( uint i = 0; i < e[0]; ++i )
         {	           
              m = 0;
              usint *s = (usint *) calloc (psNumber,sizeof(usint));//if ( i%1000 == 0 ) printf("%d\n",i);printf("%d %d\n",psNumber,psNumber*sizeof(usint));
              int n = i+1;
              if ( s == NULL ) { printf("malloc memory error.\n");exit(0); }
              //printf("%d\n",n);
              ss[n] = s ; //*(ss+n) = s; 
                                  
              for ( uint j = 0; j < e[1]-1; ++j)
              {
                  char a = p[i][j];//char a = *(*(p+i)+j);            
                  for ( uint k = j+1; k < e[1]; ++k)
                  {
                      char b = p[i][k];                                   
                      if ( a == '-' || a == '?' )
                      {
                           s[m] = preLineScore[m]+gapPunish;
                           preLineScore[m] = s[m];//printf("%d %d\n",i,m);//*(s+m) =  gapPunish;s[i][m] = 0;                       
                      }else if ( b == '-' || a == '?' )
                      {
                           s[m] = preLineScore[m]+gapPunish;
                           preLineScore[m] = s[m];
                      }else if ( a == b )
                      {
                           s[m] = preLineScore[m]+equalPunish;
                           preLineScore[m] = s[m];              
                      }else
                      {
                           s[m] = preLineScore[m]-Punish;
                           preLineScore[m] = s[m];       
                      }                  
                      ++m;
                  }
                  
              } 
                     
         }
         return ss;     
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    You are allocating memory and not freeing it?

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    he likes memory leaks

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by cyberfish View Post
    You are allocating memory and not freeing it?
    I do free it
    Code:
    /* free memory of usint** */
    void freeUS(usint** p,int n)
    {
    	if ( p[0][0] != '\0' )
    	{
    		for ( int i = 0; i < n; ++i )
    		{
    			if ( p[i][0] != '\0' ) free(p[i]);
    		}
    		//delete [] p;
    		free(p);
    	}  
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    But not all of it
    Code:
         usint * ss = (usint **) calloc (e[0],sizeof(usint*));
         usint preLineScore[psNumber]; 
         usint *st = (usint *) calloc (psNumber,sizeof(usint));
         for( uint pi = 0; pi < psNumber; ++pi) { preLineScore[pi] = 0; st[pi] = 0; }  
         * ss = st; 
         for( uint i = 0; i < e[0]; ++i )
         {	           
              m = 0;
              usint *s = (usint *) calloc (psNumber,sizeof(usint));//if ( i%1000 == 0 ) printf("%d\n",i);printf("%d %d\n",psNumber,psNumber*sizeof(usint));
              int n = i+1;
              if ( s == NULL ) { printf("malloc memory error.\n");exit(0); }
              //printf("%d\n",n);
              ss[n] = s ; //*(ss+n) = s;
    The red alloc is lost with the blue assignment.

    I'm assuming the profanity filter has been at your code.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    And that's not even called from your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 04:22 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21