Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You allow <code>target_searcher</code> to be equal to <code>last_int</code> while searching for the minimum. So, sometimes you get a random small value injected into your array (and mess with memory that doesn't belong to you). Of course, I am assuming <code>last_int</code> is the length of the array.</p> <p>When you are dealing with "valid indexes only", the range is from <code>0</code> to <code>len-1</code>. You can see that with an array of length 1 (in case you are ever in doubt again). Since there is only 1 element, it is at <code>array[0]</code>, or <code>array[len-1]</code>.</p> <p>Having said that, it is usually customary to pass parameters as array and length instead of array and index of last valid element. It is more natural. Say, you have a large array with two blocks of <code>len1</code> and <code>len2</code>, and a function that does something with these partitions. If you use length as parameter, you use:</p> <pre><code>processBlock(arr, len1); processBlock(arr + len1, len2); </code></pre> <p>If you were to use last valid index, there would be all these <code>+/-1</code> terms you'd have to deal with. So it is either:</p> <pre><code>processBlockIdx(arr, len1 - 1); processBlockIdx(arr + len1, len2 - 1); </code></pre> <p>or:</p> <pre><code>processBlockIdx(arr, lastIdx1); processBlockIdx(arr + lastIdx1 +1, lastIdx2 - lastIdx1 - 1); </code></pre> <p>As for the answer of your second question: yes, the problem was caused by accessing an element that is outside the bounds of your array. Since C doesn't have the safety net of checking array bounds, an error like this usually manifests itself as an unexplained value appearing in your results, or worse, an application crash. In some cases you are not as lucky, and it manifests the problem in a completely unrelated part of your program. So, it is best to be <em>very</em> sure about array element accesses.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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