[SOLVED]Unexpected behaviour with int x[i] where 'i' is not known at compile time.

  • NOTICE: This is a fairly bad question and will probably be of no use to someone new as it is not even related with Qt. Do not waste your time reading further if the title means nothing to you.

    I have a pretty simple algorithm that goes like this:

    void Alg_DefaultEffects::applyErosion(QImage *image1, QImage *image2)
        QColor oldColor;
        int buff[9];
        int output[image1->width() * image1->height()];
        for (int y = 0; y < image1->height(); y++)
            for (int x = 0; x < image1->width(); x++)
                int i = 0;
                for (int ty = y - 1; ty <= y + 1; ty++){
                   for (int tx = x - 1; tx <= x + 1; tx++){
                       if(ty >= 0 && ty < image1->height() && tx >= 0 && tx < image1->width()){
                           //pixel under the mask
                           oldColor = QColor(image1->pixel(tx, ty));
                           buff[i] = oldColor.red();

    If you notice at the start you will see this line

    int output[image1->width() * image1->height()];

    That should produce an error since the compiler can't know at compile time how big output[] should be.

    Yet running this on my Mac with Qt 5.3.1 (Clang 5.0 (Apple), 64 bit) as well as on a virtual machine with Win7 and Qt 5.3.2 (MSVC 2013, 32 bit) produces no errors.

    If I try to run the same code on a second machine this time with Qt 5.3.2 (MSVC 2012, 32 bit) I get the usual error saying that we must know the size of output beforehand.

    Has anyone any idea on why this is allowed on those systems and I am not required to use the new operator as I normally (to my experience) would?

  • @ealione
    I have moved your thread to "C++ Gurus". As you probably have already tagged yourself, this is non-Qt related discussion. I would consider it of special interest though.

    My guess is that this might be a C++ standard dependent implementation issue. You should able to recreate fairly easy the same behaviour with those compilers involved but without using Qt.

    Check if the C++ standard definitions used are different. Probably those are the default settings in your case. If my assumptions are true, you shall be able to select an older standard definition and should see what you are expecting. The MS VC 2013 would be a good candidate for testing.

  • Hi,

    IIRC some C++ recommendation specify that you can use something like

    int output[image1->width() * image1->height()];

    Obviously not all compilers implements in the same way the C++ Recommendations, this is why the oldest compiler (MSVC 2012) returns an error and the the other ones no.

  • @koahnig thanks for moving the thread, I'll remember that this is the right location for such posts from now on.

    I suppose you are right, there are bound to be some discrepancies on how various compilers implement things. After testing this it seems that you can use this expression on newer compilers but older ones such as the one used in VS2012 will complain, in which case int *output = new int[x]; should do the trick.

  • @ealione
    The good thing is that typically users do not notice. The problem is popping up when trying to compile with older compilers or you remember that such stuff was not possible in the past.
    As @mcosta suggests there is probably a recommendation out. On the other compiler versions are changing too rapidly and also the versions of recommendations.