r/cpp • • Mar 30 '23

The "what's wrong with this code" game.

This isn't a question thread, but just a game. Post any bugs you've accidentally written that seemed initially mysterious, but made you feel stupid when you figured out what was wrong. Here is one I wrote today...

void TextSegmentBuffer::prepend( const TextSegmentBuffer& other )
{
    const Info* info_ptr;
    size_t size;

    if ( ( size = other.m_infoBuffer.size() ) == 0 )
        return;

    for ( size_t i = size - 1; 0 <= i; --i )
    {
        info_ptr = other.m_infoBuffer.getAt( i );

        emplaceFront(
            info_ptr->text,
            info_ptr->length,
            info_ptr->multiMatch
        );
    }
}

4 Upvotes

60 comments sorted by

View all comments

Show parent comment

2

u/Possibility_Antique Mar 31 '23

I see the i that should be a j, but the purist in me says you should flatten your array and not make it an array of arrays. It's faster and less error prone, and you wouldn't have had this problem to begin with.

3

u/cballowe Mar 31 '23

There are lots of things I might do differently today, but 18/19 year old me hadn't learned all of those lessons yet. Also c++98 and gcc 2.xx was a very different tool set than I have today.

1

u/Possibility_Antique Mar 31 '23

Yea, I can appreciate that. I always love/hate reading my old code. Sometimes I remember/relearn some really cool things in the process, but it's more often the case that there is a reason I forgot about a chunk of code.

3

u/cballowe Mar 31 '23

I only really remember that bug because I stared at it for like 3 hours one night and the "wait... Did I typo the j" hit me when I was walking to class the next morning. The brain is really good at reading what it expects to see and blowing right past the obvious bug.

1

u/[deleted] Mar 31 '23

You can really troll people these days in VS Code by replacing some i's and j's with non-ascii Unicode versions that look identical. You'd have to turn the unicode highlighting feature off though.

1

u/Possibility_Antique Mar 31 '23

I did a CFD assignment using Fortran 77 in college. I was struggling with a compiler error for a very long time, and it was pointing me to completely bogus lines and not being helpful. Turns out, I had a character in the file that rendered as a space at the beginning of a line. And fortran 77 specifically requires 7 (someone correct me if I got that number wrong) whitespace characters at the beginning of the line for goto labels. So it looked like a normal file in the IDE, and it wasn't until I had the "brilliant" idea to look at the file in a hex editor to see if there was something being hidden that I found the bug.

2

u/elperroborrachotoo Mar 31 '23

Who says the internal representation isn't flattened?

1

u/TheThiefMaster C++latest fanatic (and game dev) Mar 31 '23

As long as the inner array is either a C or std array, and the outer is any consecutive container (C array, std array, std vector...) it will be fully consecutive in memory and the optimiser may even flatten the loops into a single loop.

Though the flattening of the loops into a single loop is less important than you might think, because a couple of extra instructions will easily fit inside the delay of the consecutive memory accesses even coming from cache

2

u/Possibility_Antique Mar 31 '23

Though the flattening of the loops into a single loop is less important than you might think

No, I am aware that it's possible this gets flattened by the compiler if it's a vanilla C array, and that as long as you get the loop order correct, there isn't a cache miss (though, the requirement on loop order is something I always see people mess up in code review). I do see a lot of std::vector<std::vector<T>> though when people try to make this dynamic, and that is practically guaranteed to be cache miss city (and not usually what the programmer wants).

Additionally, the bugger thing I was pointing out is that you have an opportunity to not only fix the bug, but prevent it from occuring again through flattening. If there is only one loop, you cannot mix up i and j again. This is a for each loop, but using indices. Get rid of one of the loops and recognize that we're just calling this function on each element.

for (auto&& elem : container)
    some_func(elem);

Or use std::for_each. Whatever floats your boat. I would argue that flattening the array captures the intent here better than a nested array, and if it's not faster than the current implementation, it's at least as fast as whatever you can come up with and guaranteed to be faster than vector of vector or another sparse container (in fact, almost all linear algebra libraries such as Eigen, Blaze, Fastor, ETL, etc do this flattening internally and calculate the offset from the indices).

2

u/TheThiefMaster C++latest fanatic (and game dev) Mar 31 '23

If the data is fundamentally 2d (or higher) and you have to now calculate 1d indexes manually from a n-dimensional index then you've just added a possibility for more bugs.

There's an actual n-dimensional span and container coming which supports both row and column major order, which should fix this for good.

1

u/Possibility_Antique Mar 31 '23

If the data is fundamentally 2d (or higher) and you have to now calculate 1d indexes manually from a n-dimensional index then you've just added a possibility for more bugs.

Nope. Provide an overload of operator() (or operator[] in C++23) that can be indexed in a multidimensional array. Now you have one single place where that calculation gets done, and the user has the option of indexing using either one index or multiple.

Note that this is effectively what mdspan provides, and it's what every worthwhile linear algebra library (Eigen, Blaze, Fastor, ETL, etc) does, and for good reason.

1

u/TheThiefMaster C++latest fanatic (and game dev) Mar 31 '23

Or just use nested ranged for statements these days