<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Segfault in if-Anweisung]]></title><description><![CDATA[<p>Hallo,</p>
<p>erstmal möchte ich mich für den folgenden Quelltext entschuldigen, da er eine Mischung aus C und C++ ist und nur wenig strukturiert.</p>
<p>Mein Problem ist in folgendem Programm zu finden (im wesentlich die fette if-Anweisung):</p>
<pre><code>#include &lt;cstdlib&gt;
#include &lt;cmath&gt;
#include &lt;cstdio&gt;
#include &lt;ctime&gt;
#include &lt;iostream&gt;

using namespace std; 

#define lmax 200
#define rs (rmax+2.)
#define rd (rmax+5.)
#define rkill (100.*rmax)

double pi = acos(-1), rmax = 1.0;

double max(double a, double b)
{
 double maxi;
 if(a&gt;b) maxi = a;
 else maxi = b;
 return(maxi);
}

double dim(int counter)
{
 double D;
 D=log((double)counter)/log((double)lmax);
 return(D);      
}

/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
/*===============================Entering=====================================*/
/*============================================================================*/
/*=============================Main-Function==================================*/
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/

int main(int* argc, char* argv[])
{
 FILE* f=fopen(argv[1],&quot;w&quot;);
 int i,j,counter;
 int k=0;
 int rx=0,ry=0;
 double D, phi, x, y, r;

 int **xf=new int*[lmax];
 for(i=0;i&lt;lmax;i++)
 {
  xf[i]=new int[lmax];
 }

 for(i=0;i&lt;lmax;i++)
 for(j=0;j&lt;lmax;j++)
 {
  xf[i][j]=0;                   
 }

 xf[lmax/2][lmax/2]=1;

 phi=(double)(rand()/RAND_MAX*2.*pi);
 rx=(int)(rs*cos(phi));
 ry=(int)(rs*sin(phi));

 srand(time(NULL));
 i=rand()%5;
 {
  if(i==0) rx+=1;
  if(i==1) rx+=-1;
  if(i==2) ry+=1;
  if(i==3) ry+=-1;
 }

 while(1)
 {
  k++;
  printf(&quot;%d  \n&quot;, k);
  if(sqrt(rx*rx+ry*ry)&gt;rkill)
  { 
    phi=(double)(rand()/RAND_MAX*2.*pi);
    rx=(int)(rs*cos(phi));
    ry=(int)(rs*sin(phi)); 
    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    }
  }
  [b]if(rx&lt;lmax/2 &amp;&amp; ry&lt;lmax/2 &amp;&amp; rx &gt; -lmax/2 &amp;&amp; ry &gt;-lmax/2 &amp;&amp; xf[rx+1+lmax/2][ry+lmax/2]+xf[rx-1+lmax/2][ry+lmax/2]+xf[rx+lmax/2][ry+1+lmax/2]+xf[rx+lmax/2][ry-1+lmax/2]&gt;0 )
  { 
    xf[rx+lmax/2][ry+lmax/2]+=1;
    x=(double)rx;y=(double)ry;
    rmax = max(rmax,sqrt(x*x+y*y));
    if(rmax&gt;lmax/2.-5.) {printf(&quot;\7&quot;);getchar();exit(1);};
    phi=(double)(rand()/RAND_MAX*2.*pi);
    rx=(int)(rs*cos(phi));
    ry=(int)(rs*sin(phi));

    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    } 
  }[/b]
  if(sqrt(rx*rx+ry*ry)&gt;=rd)
  { 
    phi=(double)(rand()/RAND_MAX*2.*pi);
    x=(double)rx; y=(double)ry; r=sqrt(x*x+y*y);
    rx+=(int)((r-rs)*cos(phi));
    ry+=(int)((r-rs)*sin(phi));
  }
  else
  { 
    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    }
  }
 }

 for(rx=0;rx&lt;lmax;rx++)
 for(ry=0;ry&lt;lmax;ry++)
 {
  if(xf[rx+lmax/2][ry+lmax/2]!=0)
  {
   counter+=1;
   fprintf(f,&quot;%d  %d \n&quot;, rx+lmax/2,ry+lmax/2);
  }
 }
 D=dim(counter);
 cout &lt;&lt; &quot;Simulation beendet&quot; &lt;&lt;endl;
 cout &lt;&lt; &quot;Die fraktale Dimension (Boxdimension) lautet:   &quot; &lt;&lt; D &lt;&lt; endl;
 cout &lt;&lt; &quot;Bitte drücken Sie eine Taste, um das Programm zu verlassen. Sie finden Íhre Ergebnisse in:  &quot;&lt;&lt; argv[0] &lt;&lt;endl;
 getchar();
 for(i=0;i&lt;lmax;i++)
 {
  delete[] xf[i];                  
 }

 delete[] xf;   
 fclose(f);  
}
</code></pre>
<p>Ich sitze jetzt wirklich einige Tage davor und weiß nich, woher der segfault kommt, weil ich eigentlich der Meinung bin, genügend Speicher alloziert zu haben.</p>
<p>Wäre nett, wenn mir jemand helfen könnte (und entschuldigt bitte nochmals die grauenhafte Form).</p>
<p>MfG</p>
<p>Borninger</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/202084/segfault-in-if-anweisung</link><generator>RSS for Node</generator><lastBuildDate>Tue, 06 Oct 2026 12:12:28 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/202084.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 07 Jan 2008 13:49:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Segfault in if-Anweisung on Mon, 07 Jan 2008 13:49:25 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>erstmal möchte ich mich für den folgenden Quelltext entschuldigen, da er eine Mischung aus C und C++ ist und nur wenig strukturiert.</p>
<p>Mein Problem ist in folgendem Programm zu finden (im wesentlich die fette if-Anweisung):</p>
<pre><code>#include &lt;cstdlib&gt;
#include &lt;cmath&gt;
#include &lt;cstdio&gt;
#include &lt;ctime&gt;
#include &lt;iostream&gt;

using namespace std; 

#define lmax 200
#define rs (rmax+2.)
#define rd (rmax+5.)
#define rkill (100.*rmax)

double pi = acos(-1), rmax = 1.0;

double max(double a, double b)
{
 double maxi;
 if(a&gt;b) maxi = a;
 else maxi = b;
 return(maxi);
}

double dim(int counter)
{
 double D;
 D=log((double)counter)/log((double)lmax);
 return(D);      
}

/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
/*===============================Entering=====================================*/
/*============================================================================*/
/*=============================Main-Function==================================*/
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/

int main(int* argc, char* argv[])
{
 FILE* f=fopen(argv[1],&quot;w&quot;);
 int i,j,counter;
 int k=0;
 int rx=0,ry=0;
 double D, phi, x, y, r;

 int **xf=new int*[lmax];
 for(i=0;i&lt;lmax;i++)
 {
  xf[i]=new int[lmax];
 }

 for(i=0;i&lt;lmax;i++)
 for(j=0;j&lt;lmax;j++)
 {
  xf[i][j]=0;                   
 }

 xf[lmax/2][lmax/2]=1;

 phi=(double)(rand()/RAND_MAX*2.*pi);
 rx=(int)(rs*cos(phi));
 ry=(int)(rs*sin(phi));

 srand(time(NULL));
 i=rand()%5;
 {
  if(i==0) rx+=1;
  if(i==1) rx+=-1;
  if(i==2) ry+=1;
  if(i==3) ry+=-1;
 }

 while(1)
 {
  k++;
  printf(&quot;%d  \n&quot;, k);
  if(sqrt(rx*rx+ry*ry)&gt;rkill)
  { 
    phi=(double)(rand()/RAND_MAX*2.*pi);
    rx=(int)(rs*cos(phi));
    ry=(int)(rs*sin(phi)); 
    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    }
  }
  [b]if(rx&lt;lmax/2 &amp;&amp; ry&lt;lmax/2 &amp;&amp; rx &gt; -lmax/2 &amp;&amp; ry &gt;-lmax/2 &amp;&amp; xf[rx+1+lmax/2][ry+lmax/2]+xf[rx-1+lmax/2][ry+lmax/2]+xf[rx+lmax/2][ry+1+lmax/2]+xf[rx+lmax/2][ry-1+lmax/2]&gt;0 )
  { 
    xf[rx+lmax/2][ry+lmax/2]+=1;
    x=(double)rx;y=(double)ry;
    rmax = max(rmax,sqrt(x*x+y*y));
    if(rmax&gt;lmax/2.-5.) {printf(&quot;\7&quot;);getchar();exit(1);};
    phi=(double)(rand()/RAND_MAX*2.*pi);
    rx=(int)(rs*cos(phi));
    ry=(int)(rs*sin(phi));

    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    } 
  }[/b]
  if(sqrt(rx*rx+ry*ry)&gt;=rd)
  { 
    phi=(double)(rand()/RAND_MAX*2.*pi);
    x=(double)rx; y=(double)ry; r=sqrt(x*x+y*y);
    rx+=(int)((r-rs)*cos(phi));
    ry+=(int)((r-rs)*sin(phi));
  }
  else
  { 
    //srand(time(NULL));
    i=rand()%5;
    {
     if(i==0) rx+=1;
     if(i==1) rx+=-1;
     if(i==2) ry+=1;
     if(i==3) ry+=-1;
    }
  }
 }

 for(rx=0;rx&lt;lmax;rx++)
 for(ry=0;ry&lt;lmax;ry++)
 {
  if(xf[rx+lmax/2][ry+lmax/2]!=0)
  {
   counter+=1;
   fprintf(f,&quot;%d  %d \n&quot;, rx+lmax/2,ry+lmax/2);
  }
 }
 D=dim(counter);
 cout &lt;&lt; &quot;Simulation beendet&quot; &lt;&lt;endl;
 cout &lt;&lt; &quot;Die fraktale Dimension (Boxdimension) lautet:   &quot; &lt;&lt; D &lt;&lt; endl;
 cout &lt;&lt; &quot;Bitte drücken Sie eine Taste, um das Programm zu verlassen. Sie finden Íhre Ergebnisse in:  &quot;&lt;&lt; argv[0] &lt;&lt;endl;
 getchar();
 for(i=0;i&lt;lmax;i++)
 {
  delete[] xf[i];                  
 }

 delete[] xf;   
 fclose(f);  
}
</code></pre>
<p>Ich sitze jetzt wirklich einige Tage davor und weiß nich, woher der segfault kommt, weil ich eigentlich der Meinung bin, genügend Speicher alloziert zu haben.</p>
<p>Wäre nett, wenn mir jemand helfen könnte (und entschuldigt bitte nochmals die grauenhafte Form).</p>
<p>MfG</p>
<p>Borninger</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1432415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1432415</guid><dc:creator><![CDATA[Borninger]]></dc:creator><pubDate>Mon, 07 Jan 2008 13:49:25 GMT</pubDate></item><item><title><![CDATA[Reply to Segfault in if-Anweisung on Mon, 07 Jan 2008 14:12:19 GMT]]></title><description><![CDATA[<p>*mal kurz überfliegt* lmax ist 200, also lmax/2==100 - damit liegen die Werte für rx und ry im Bereich -99..99 und du greifst auf die Indizes von 2..200 bzw. 1..199 (für &quot;xf[rx+1+lmax/2][ry+lmax/2]&quot;) zu - ersterer liegt bereits außerhalb des verfügbaren Speicherplatzes.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1432434</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1432434</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Mon, 07 Jan 2008 14:12:19 GMT</pubDate></item><item><title><![CDATA[Reply to Segfault in if-Anweisung on Mon, 07 Jan 2008 15:21:04 GMT]]></title><description><![CDATA[<p>Hey Danke,</p>
<p>das war das Problem. Naja is nich gut, wenn man nich rechnen kann.</p>
<p>Ich versuche jetzt mal das alles zu simulieren.</p>
<p>Ciao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1432484</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1432484</guid><dc:creator><![CDATA[Borninger]]></dc:creator><pubDate>Mon, 07 Jan 2008 15:21:04 GMT</pubDate></item><item><title><![CDATA[Reply to Segfault in if-Anweisung on Mon, 07 Jan 2008 16:59:24 GMT]]></title><description><![CDATA[<p>Noch ein Nachtrag:<br />
Wenn bei dir 'lmax' sowieso eine Konstante (bzw. define) ist, warum reservierst du dann dynamisch Speicher?</p>
<pre><code class="language-cpp">const int LMAX = 200; // besser als define
int xf[LMAX][LMAX];
</code></pre>
<p>Und sehr leserlich ist der Code nicht (Einrückungen, alles in der main-Funktion, ...).<br />
Zumindestens für</p>
<pre><code class="language-cpp">i=rand()%5;
if(i==0) rx+=1;
if(i==1) rx+=-1;
if(i==2) ry+=1;
if(i==3) ry+=-1;
</code></pre>
<p>solltest du dir eine (Inline-)Funktion definieren (hast du 4x in deinem kurzen Code), z.B. so</p>
<pre><code class="language-cpp">inline
void DoRandom(int &amp;x, int &amp;y)
{
  switch(rand()%5)
  {
    case 0: x+=1; break;
    case 1: x-=1; break;
    case 2: y+=1; break;
    case 3: y-=1; break;
    case 4: break; // do nothing
  }
}

//Aufruf dann einfach mit
DoRandom(rx, ry);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1432565</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1432565</guid><dc:creator><![CDATA[Th]]></dc:creator><pubDate>Mon, 07 Jan 2008 16:59:24 GMT</pubDate></item></channel></rss>