Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Defined int parameter[NUM] returned segment fault
Forum Updated to NodeBB v4.3 + New Features

Defined int parameter[NUM] returned segment fault

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 989 Views 1 Watching
  • 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.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by victor wang
    #1

    Hi all,
    I'm having an issue with defining "int parameter[num]".
    It success when compiling the code.
    But If I run my application, it will show segment fault error to me when defining int parameter[num].

    if(DBG)
        {
            qDebug("DIDO_Light_on function in!");
        }
        int result[DI_num];       
        if(DBG)
        {
            qDebug("result function defined!");
        }
        int err_flag = false;
        QString get;
        //QStringList result;
        QStringList DI_get_value;
        QStringList DO_set_value_low;
        QStringList DO_set_value_high;
    

    It crashes at int result[DI_num];

    What did I miss?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please paste the code: your variable definition and exactly the line where it crashes.

      (Z(:^

      V 1 Reply Last reply
      1
      • sierdzioS sierdzio

        Please paste the code: your variable definition and exactly the line where it crashes.

        V Offline
        V Offline
        victor wang
        wrote on last edited by
        #3

        @sierdzio
        Thanks for reminding.
        I have modified the question just now.

        J.HilkJ 1 Reply Last reply
        0
        • V victor wang

          @sierdzio
          Thanks for reminding.
          I have modified the question just now.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @victor-wang
          are you sure DI_num is a positive value? If not than that's undefined behavior.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          V 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @victor-wang
            are you sure DI_num is a positive value? If not than that's undefined behavior.

            V Offline
            V Offline
            victor wang
            wrote on last edited by
            #5

            @J.Hilk
            Yes, I'm sure it is a positive value.
            I defined it in mainwindow.h like below.

            private:
            
                int Comport_Count,LED_Light,PingIP_1,PingIP_2,a,b,height,width,can1_rec_prt,can0_send_prt,burn_chk,Burn_count,a_cpu,b_cpu,LanTabClick,DidoTabClick,dido_count;
                int count,active_test_item,test_result_count;
                int DI_num,DO_num,showconnectcount;
            
            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              And it compiles? In C++ arrays cannot have dynamic sizes, what's between [ and ] should be a constant expression.

              (Z(:^

              V D 2 Replies Last reply
              2
              • sierdzioS sierdzio

                And it compiles? In C++ arrays cannot have dynamic sizes, what's between [ and ] should be a constant expression.

                V Offline
                V Offline
                victor wang
                wrote on last edited by
                #7

                @sierdzio
                I'm using arm-poky-linux-gnueabi-g++

                So I can only use static parameter between [ and ]?

                J.HilkJ 1 Reply Last reply
                0
                • V victor wang

                  @sierdzio
                  I'm using arm-poky-linux-gnueabi-g++

                  So I can only use static parameter between [ and ]?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @victor-wang
                  yes, since C99 I believe.

                  if you want a dynamically sized array, you should use QVector<int>


                  yes, since C99 I believe. nope, had that wrong, variable length arrays are a C99 feature only.
                  But C++ standard (till C++11) doesn’t support variable sized arrays. The C++11 standard mentions array size as a constant-expression.
                  However the GCC compiler seems to provide an extension to support them.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  V 1 Reply Last reply
                  4
                  • sierdzioS sierdzio

                    And it compiles? In C++ arrays cannot have dynamic sizes, what's between [ and ] should be a constant expression.

                    D Offline
                    D Offline
                    Devopia53
                    wrote on last edited by
                    #9

                    Not standard but latest gcc/g++ available. But first initialized. like this:

                    int DI_num{1024}, ...;

                    or

                    DI_num = 1024;
                    int result[DI_num];

                    V 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @victor-wang
                      yes, since C99 I believe.

                      if you want a dynamically sized array, you should use QVector<int>


                      yes, since C99 I believe. nope, had that wrong, variable length arrays are a C99 feature only.
                      But C++ standard (till C++11) doesn’t support variable sized arrays. The C++11 standard mentions array size as a constant-expression.
                      However the GCC compiler seems to provide an extension to support them.

                      V Offline
                      V Offline
                      victor wang
                      wrote on last edited by
                      #10

                      @J.Hilk
                      I would try to use QVector later

                      1 Reply Last reply
                      0
                      • D Devopia53

                        Not standard but latest gcc/g++ available. But first initialized. like this:

                        int DI_num{1024}, ...;

                        or

                        DI_num = 1024;
                        int result[DI_num];

                        V Offline
                        V Offline
                        victor wang
                        wrote on last edited by
                        #11

                        @Devopia53
                        But DI_num would get a dynamic parameter form ini file.
                        I will convert String to int and give it to DI_num.

                        So I should defined it like int DI_num{1024}; first then give the new parameter to DI_num?

                        D 1 Reply Last reply
                        0
                        • V victor wang

                          @Devopia53
                          But DI_num would get a dynamic parameter form ini file.
                          I will convert String to int and give it to DI_num.

                          So I should defined it like int DI_num{1024}; first then give the new parameter to DI_num?

                          D Offline
                          D Offline
                          Devopia53
                          wrote on last edited by
                          #12

                          @victor-wang

                          This is possible if the value of the variable is set to an appropriate value before the array declaration is executed. But, I recommend using a vector.

                          1 Reply Last reply
                          2

                          • Login

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