Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behavior with std::nth_element
    primarykey
    data
    text
    <p>I'm trying to find the median of vectors of (x,y) points using nth_element</p> <pre><code> cv::Point2f medOffset; vector&lt;float&gt; tempOffsetsX = offsetsX; int medLoc = tempOffsetsX.size()/2; nth_element(tempOffsetsX.begin(), tempOffsetsX.begin()+medLoc, tempOffsetsX.end()); // sort(tempOffsetsX.begin(), tempOffsetsX.end()); medOffset.x = tempOffsetsX[medLoc]; vector&lt;float&gt; tempOffsetsY = offsetsY; ****** debug out line 1 ********* nth_element(tempOffsetsY.begin(), tempOffsetsY.begin()+medLoc, tempOffsetsY.end()); // sort(tempOffsetsY.begin(), tempOffsetsY.end()); medOffset.y = tempOffsetsY[medLoc]; ****** debug out line 2 ********* </code></pre> <p>tempOffsetsX is working just fine but, occasionally, tempOffsetsY is giving very strange results after nth_element. Here is sample output at the marked debug lines</p> <pre><code>tempOffsetsY1: 5.184135 -1.564125 3.751759 0.221855 -0.742348 1.737648 tempOffsetsY2: -0.742348 -1.564125 -8885092352.000000 -8850636800.000000 0.000000 0.000000 </code></pre> <p>The results are pretty repeatable until I recompile, at which point the specifics change the the general problem remains. Clearly the vector is getting corrupted somehow but I can't think of how. </p> <p>Also, if I use sort instead of nth_element it works without problem. For debugging, I tried doing a sort and then nth_element which worked just fine. So somehow the reordering that happens inside nth_element is getting messed up but I can't think of how.</p> <p>Any ideas how this is happening?</p> <p><em>edit</em> - More info about my environment. I'm running Arch Linux. I just did a system update. I should note that this same code did work without problem before the update, and this is the first time I've ran it after the update. But that's a gap of several days and I'm always hesitant to point at system libraries for what is usually my own problem.</p> <pre><code>[]$ uname -r 3.11.6-1-ARCH []$ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /build/gcc/src/gcc-4.8.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-install-libiberty --disable-multilib --disable-libssp --disable-werror --enable-checking=release Thread model: posix gcc version 4.8.2 (GCC) []$ pacman -Qi glibc Name : glibc Version : 2.18-8 .... </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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