Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with a C program. It was working before I made some changes (from define do var declarations). Now:

  • it compiles without errors using: gcc m.c -lm -Wall -march=native
  • has a run-time error: Segmentation fault

So I tried to find a problem using gdb. Now I know more:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400bbb in GivePeriod (Cx=-0,75, Cy=-0, Iteration_Max=650000, 
    precision=0,00033329999999999997) at m.c:137
137    orbit[0][0]=0.0;

The problem is in function (which code was not changed), code below.

How can I find the problem?

gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)

/*-------------------------------*/
// this function is based on program:
// Program MANCHAOS.BAS  
// http://sprott.physics.wisc.edu/chaos/manchaos.bas
// (c) 1997 by J. C. Sprott 
//
int GivePeriod(double Cx,double Cy, int Iteration_Max, double precision)
{  
 double Zx2, Zy2, /* Zx2=Zx*Zx;  Zy2=Zy*Zy  */
     ZPrevieousX,ZPrevieousY,
     ZNextX,ZNextY;
   int Iteration,
  I;  
double orbit[Iteration_Max+1][2]; /* array elements are numbered from 0 to   length-1 */    
 /* starting point is critical point  */
ZPrevieousX=0.0;
ZPrevieousY=0.0;
orbit[0][0]=0.0;
orbit[0][1]=0.0;  
Zx2=ZPrevieousX*ZPrevieousX;
Zy2=ZPrevieousY*ZPrevieousY;
/* iterate and save points for analysis */
for (Iteration=1;Iteration<Iteration_Max+1 ;Iteration++)
    {
        ZNextY=2*ZPrevieousX*ZPrevieousY + Cy;
        ZNextX=Zx2-Zy2 +Cx;
        Zx2=ZNextX*ZNextX;
        Zy2=ZNextY*ZNextY;
        if ((Zx2+Zy2)>ER2) return 0; /* basin of atraction to infinity */
        //if   (SameComplexValue(ZPrevieousX,ZPrevieousY,ZNextX,ZNextY,precision))
        //   return 1; /* fixed point , period =1 */
        ZPrevieousX=ZNextX;
        ZPrevieousY=ZNextY;
        /* */
        orbit[Iteration][0]=ZNextX;
        orbit[Iteration][1]=ZNextY;   

    }; 
 /* here iteration=IterationMax+1 but last element of orbit has number  IterationMax */    
 for(I=Iteration_Max-1;I>0;I--) 
  if (SameComplexValue(orbit[Iteration_Max][0],orbit[Iteration_Max] [1],orbit[I][0],orbit[I][1],precision))
    return(Iteration_Max-I);
 return 0;
}
share|improve this question

migrated from unix.stackexchange.com Jan 5 at 8:23

This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems..

add comment

2 Answers

up vote 7 down vote accepted

Program received signal SIGSEGV, Segmentation fault. 0x0000000000400bbb in GivePeriod (Cx=-0,75, Cy=-0, Iteration_Max=650000, precision=0,00033329999999999997) at m.c:137 137 orbit[0][0]=0.0;

double orbit[Iteration_Max+1][2];

650001 * 2 * 8 (bytes/double) = 10400016

That's probably bigger than your maximum stack size;1 on linux you can check that with ulimit -s and by default it is 8192 kB.

If you need storage that big, allocate it on the heap with malloc() and free() it when done.


1. Memory in a C program is broken into two main areas: the heap, which contains globals and dynamically allocated things (and grows with them), and the small fixed size stack, which is a LIFO structure onto which local data is pushed. Since array orbit is declared in a function and not allocated dynamically, it is local data and pushed onto the stack. When a function exits, its local data is popped off the stack and discarded.

share|improve this answer
    
You are right. My stack is 8192. So orbit array is too big. I thought that the reason is different, but second check of older ( working version ) of the program showed that I have also changed ( increased ) size of the orbit array. Thx. –  Adam Jan 4 at 12:35
add comment

You're probably blowing your stack. That orbit array weighs something like 10 megabytes, too much for a stack allocation.

Allocate it on the heap with malloc or calloc, and don't forget to free it on every path that exits your function.

share|improve this answer
    
It seems that it is possible to make array bigger then ulimit -s = 8192 kB. I can make and use array 8376 kB, but bigger causes segmentation fault. –  Adam Jan 7 at 10:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.