Note that there are some explanatory texts on larger screens.

plurals
  1. POsegmentation fault on a for loop in c
    primarykey
    data
    text
    <p>I'm writing a magnetometer sampling function. </p> <p>It goes through the cycle as expected, where samplesPerAxis = 33 and MAX_AXES is 3 and MAX is 999</p> <p>As you can see the segmentation fault occurs after the for loops complete, but the final "end" doesn't print. </p> <pre><code>void createSamplingData(){ int i, indexOfValue, numaxis=0, sampling_value ; printf("%i %i %i\n", magmin[0], magmin[1], magmin[2]); printf("%i %i %i\n", magmax[0], magmax[1], magmax[2]); for (numaxis = 0; numaxis &lt; MAX_AXES; numaxis++){ printf("beginAxis: %i\n", numaxis); sampling_value = magmin[numaxis]; for (i = samplesPerAxis*numaxis ; i &lt; samplesPerAxis*(numaxis+1) ; i++){ indexOfValue = findIndexOfClosestValue(sampling_value, numaxis); printf("%i: %i =&gt; %i\t", i, sampling_value, indexOfValue); MagSamples[i][0] = MagInput[indexOfValue][0]; MagSamples[i][1] = MagInput[indexOfValue][1]; MagSamples[i][2] = MagInput[indexOfValue][2]; printf("%i %i %i\n", MagSamples[i][0], MagSamples[i][1], MagSamples[i][2]); sampling_value = sampling_value + (magmax[numaxis]-magmin[numaxis])/samplesPerAxis; // creates and even range between mag in min in each axis } printf("end axis\n"); } printf("\nend"); } </code></pre> <p>output i get:...</p> <pre><code>beginAxis: 0 0: 32648 =&gt; 263 32648 32760 32916 1: 32656 =&gt; 258 32656 32724 32888 2: 32664 =&gt; 130 32664 32754 32898 ... 29: 32880 =&gt; 488 32880 32774 32804 30: 32888 =&gt; 469 32888 32706 32822 31: 32896 =&gt; 990 32896 32752 32812 32: 32904 =&gt; 973 32904 32808 32844 end axis beginAxis: 1 33: 32624 =&gt; 463 32790 32624 32906 34: 32631 =&gt; 685 32784 32632 32884 35: 32638 =&gt; 652 32756 32638 32926 36: 32645 =&gt; 465 32833 32645 32867 ... 63: 32834 =&gt; 601 32690 32834 32930 64: 32841 =&gt; 597 32689 32841 32923 65: 32848 =&gt; 627 32690 32848 32914 end axis beginAxis: 2 66: 32769 =&gt; 511 32793 32749 32769 67: 32777 =&gt; 512 32785 32755 32777 68: 32785 =&gt; 520 32769 32731 32785 69: 32793 =&gt; 504 32853 32707 32793 ... 98: 33025 =&gt; 86 32805 32775 33025 end axis Segmentation fault: 11 </code></pre> <p>What's going on?</p> <p>EDIT: samplesPerAxis is defined as</p> <pre><code>#define samplesPerAxis 33 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload