Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED]Unexpected behaviour with int x[i] where 'i' is not known at compile time.

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

Scheduled Pinned Locked Moved C++ Gurus
dynamic allocatcompilersnon-qtarray
5 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ealione
    wrote on 29 Apr 2015, 07:35 last edited by ealione
    #1

    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();
                           i++;
                       }
                   }
                }
    ...
    

    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?

    K 1 Reply Last reply 29 Apr 2015, 08:32
    0
    • E ealione
      29 Apr 2015, 07:35

      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();
                             i++;
                         }
                     }
                  }
      ...
      

      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?

      K Offline
      K Offline
      koahnig
      wrote on 29 Apr 2015, 08:32 last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on 29 Apr 2015, 08:37 last edited by
        #3

        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.

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        1
        • E Offline
          E Offline
          ealione
          wrote on 29 Apr 2015, 08:52 last edited by
          #4

          @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.

          K 1 Reply Last reply 29 Apr 2015, 10:32
          1
          • E ealione
            29 Apr 2015, 08:52

            @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.

            K Offline
            K Offline
            koahnig
            wrote on 29 Apr 2015, 10:32 last edited by
            #5

            @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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            1

            1/5

            29 Apr 2015, 07:35

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved