views:

198

answers:

2

The following code crashes for me using GCC to build for ARM:

#include <vector>

using namespace std;

void foo(vector<bool>& bools) {
    bools.push_back(true);
}

int main(int argc, char** argv) {

    vector<bool> bools;
    bool b = false;
    bools.push_back(b);
}

My compiler is: arm_v5t_le-gcc (GCC) 3.4.3 (MontaVista 3.4.3-25.0.30.0501131 2005-07-23). The crash doesn't occur when building for debug, but occurs with optimizations set to -O2.

Yes, the foo function is necessary to reproduce the issue. This was very confusing at first, but I've discovered that the crash only happens when the push_back call isn't inlined. If GCC notices that the push_back method is called more than once, it won't inline it in each location. For example, I can also reproduce the crash by calling push_back twice inside of main. If you make foo static, then gcc can tell it is never called and will optimize it out, resulting in push_back getting inlined into main, resulting in the crash not occurring.

I've tried this on x86 with gcc 4.3.3, and it appears the issue is fixed for that version.

So, my questions are:

Has anyone else run into this? Perhaps there are some compiler flags I can pass in to prevent it.

Is this a bug with gcc's code generation, or is it a bug in the stl implementation (bits/stl_bvector.h)? (I plan on testing this out myself when I get the time)

If it is a problem with the compiler, is upgrading to 4.3.3 what fixes it, or is it switching to x86 from arm?

Incidentally, most other vector<bool> methods seem to work. And yes, I know that using vector<bool> isn't the best option in the world.

+1  A: 

Can you build your own toolchain with gcc 3.4.6 and Montavista's patches? 3.4.6 is the last release of the 3.x line.

I can append some instructions for how to build an ARM cross-compiler from GCC sources if you want. I have to do it all the time, since nobody does prebuilt toolchains for Mac OS X.

I'd be really surprised if this is broken for ARM in gcc 4.x. But the only way to test is if you or someone else can try this out on an ARM-targeting gcc 4.x.

Mike D.
Yeah, I was kind of hoping for the "someone else" option :) I can get our toolchain updated but I was hoping to know first if it would actually help.
JimSteele1222
+1  A: 

Upgrading to GCC 4 is a safe bet. Its code generation backend replaces the old RTL (Register Transfer Language) representation with SSA (Static Single Assignment). This change allowed a significant rewrite of the optimizer.

MSalters