Thread: Memory problem

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Memory problem

    I'm having a problem with part of a program I'm writing.

    I tried to create a new variable to store a value and I get a segmentation error (I think that's what it's called...the SIGSEGV one).

    My program isn't all that long, but it has about 30 variables. Is it possible that I ran out of memory for another variable?

    I tried to copy "problem section" of the code and run it in a smaller program, and it works fine.

    What can be wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Provide the smallest and simplest compilable code that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could post the code?

    You can be sure that your computer has enough memory to hold more than 30 variables
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    I think I ran out of memory...I took the longer code and removed about 4 variables. The program works fine now.

    How can I free some memory? After I use a variable, and no longer need it, is there any way to sort of "get back" the memory it used?

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Quote Originally Posted by anon View Post
    You could post the code?

    You can be sure that your computer has enough memory to hold more than 30 variables
    ...even if my variables include arrays with dimensions as big as [100][3] ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How can I free some memory? After I use a variable, and no longer need it, is there any way to sort of "get back" the memory it used?
    If the variable is a pointer that points to dynamically allocated memory, then you should use free() appropriately. If not, then perhaps you have one monolithic function that needs to be broken up. The side effect would be that the various local variables would then have scopes limited to the functions that they belong to, and thus their lifetimes would be correspondingly limited.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Now when I repasted it, the smaller code is not working either. Let me post everything...



    Sorry for the complex variables...
    main program:
    Code:
    #include<stdio.h>
    #include<math.h>
    
    #include"vect_pinv2.c"
    
    #define Pi 3.14159265
    
    int main()
    
    {
    
    
    int M=3, cluster_number=2;
    double AS_deg[1][2]={20, 35};
    double phi_deg[1][2]={25,20};
    double delta_phi_deg[1][2]={90, 90};
    double amplitude_cluster[1][2]={20, 30};
    double sigma_deg[1][2]={20, 31};
    
    /*int PAS_type=2;*/
    /*double d_norm[1][3]={0, 0.5, 1};*/
    /*double Q[1][2]={0.096, 0.1891};*/
    
    int m;
    int i,j,k;
    double C;
    double result_xx[100][M];
    double result_xy[100][M];
    double tmp_xx_gaussian[100][M];
    double tmp_xy_gaussian[100][M];
    double phi_rad[1][cluster_number], sigma_rad[1][cluster_number];
    double delta_phi_rad[1][cluster_number];
    double Ar[100][cluster_number];
    double Ai[100][cluster_number];
    double pinv[1][cluster_number], n, x, y;
    double erfr1, erfi1, erfr2, erfi2;
    char z[15];
    
    	
    	/*-----------------------Rxx and Rxy uniform-----------------*/
    		
    			/*Conversion degree -> radian*/
    
    			for(j=0;j<cluster_number;j++)
    			{
    				phi_rad[0][j]=phi_deg[0][j]*(Pi/180);
    				sigma_rad[0][j]=sigma_deg[0][j]*(Pi/180)*sqrt(2);
    				delta_phi_rad[0][j]=delta_phi_deg[0][j]*(Pi/180);
    			}
    			
    			vect_pinv(cluster_number, sigma_rad, pinv);
    	
    
    			for(j=0; j<cluster_number; j++)
    			{
    				C+=(delta_phi_rad[0][j]*pinv[0][j]);
    				printf("&#37;.4lf\t", pinv[0][j]);
    			}printf("%.4lf\t", C);
    
    
    	return 0;
    
    }

    vect_inv.c (function code)

    Code:
    #include<stdio.h>
    
    double vect_pinv(int cluster_number, double sigma_rad[1][cluster_number], double pinv[1][cluster_number])
    {
    	int i;
    	double mag;
    
    	for(i=0;i<cluster_number;i++)
    	mag+= (sigma_rad[0][i]*sigma_rad[0][i]);
    
    	for(i=0;i<cluster_number;i++)
    	{
    		pinv[0][i]= sigma_rad[0][i]/mag;
    	}
    
    	return 3;
    }

    Something's wrong with my variable C, I think...but what?
    Last edited by magda3227; 06-30-2008 at 09:59 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you have a whole bunch of unused variables. Also, do you really need variable length arrays? It seems like cluster_number should be const instead. I am also puzzled as to why you write:
    Code:
    double AS_deg[1][2]={20, 35};
    instead of:
    Code:
    double AS_deg[2]={20, 35};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Some of the variables are used later on, in the part of code I didn't post.

    This is part of a larger program I am converting from MatLab to C. It will be linked later. The cluster number is user dependent, so it will change, I just happened to use 2 in order to compare my output values to that of MatLab's.

    I can't remember why my arrays are [1][2], but I think there was a good reason for it. Would it really make a huge difference if I changed that?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The cluster number is user dependent
    In that case you might want to use dynamic memory allocation with malloc() and free() instead of variable length arrays. This would allow you to control precisely when you create and destroy these arrays.

    I can't remember why my arrays are [1][2], but I think there was a good reason for it. Would it really make a huge difference if I changed that?
    Probably not, but if you cannot remember the reason and did not bother to document it, then perhaps it was not such a good reason after all, so you might as well simplify your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with atl memory cleaning
    By Mariam1989 in forum Windows Programming
    Replies: 1
    Last Post: 11-11-2008, 12:35 PM
  2. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM