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. compiler complains (Variable-sized object may not be initialized)
Forum Updated to NodeBB v4.3 + New Features

compiler complains (Variable-sized object may not be initialized)

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 24.9k 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.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 18 Aug 2020, 09:32 last edited by
    #1
    Variable-sized object may not be initializedclang(variable_object_no_init)
    
    void someFunction(int n){
        int Array[n] = {0};  // n is unknown until the fucntion recieves a value.
    }
    

    i tried with some "if else" statements to set the array 1 untill n is known, but the results are the same.

    what is the proper way?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 18 Aug 2020, 09:36 last edited by JohanSolo
      #2

      Static arrays must have a compile-time known size. What you wrote is actually invalid. If your function must allocate an array which size is known only at runtime, you should do

      void someFunction(int n){
      int* result = new int[n];
      }
      

      Then you must handle the object lifetime, i.e. properly call delete [] once you don't need it anymore, or use std::unique_ptr< int[] > for instance.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      N 1 Reply Last reply 18 Aug 2020, 09:53
      4
      • J JohanSolo
        18 Aug 2020, 09:36

        Static arrays must have a compile-time known size. What you wrote is actually invalid. If your function must allocate an array which size is known only at runtime, you should do

        void someFunction(int n){
        int* result = new int[n];
        }
        

        Then you must handle the object lifetime, i.e. properly call delete [] once you don't need it anymore, or use std::unique_ptr< int[] > for instance.

        N Offline
        N Offline
        Natural_Bugger
        wrote on 18 Aug 2020, 09:53 last edited by Natural_Bugger
        #3

        @JohanSolo

        thnx, i saw people declare an array like that, but sofar i didn't payed attention to it, because the "riddles" i'm doing went all well.

        i changed it to your input, but not the program crashes.
        i have to have the first value initiated to zero.

            int *Array = new int[n];
            for (int i = 0; i < n; i++)
            {
                Array[i] = 0;
            }
        
        // 0r
        
            int *Array = new int[n];
           /*
            for (int i = 0; i < n; i++)
            {
                Array[i] = 0;
            }
           */
        
        malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
        
        1 Reply Last reply
        0
        • J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 18 Aug 2020, 09:59 last edited by J.Hilk
          #4

          @JohanSolo is of course correct,
          but since you earlier posted, that you're on a journey to discover c++, let me introduce you to this solution:

          int main() {
          
             constexpr int Array [4] = {0};
             std::cout << Array[0] << Array[1]<< Array[2]<< Array[3] << '\n';
          }
          

          has of course, the downside, its constexpr and therefore cannot be overwritten during run time. But I'm not sure, if that is a requirement for you or not?


          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.

          1 Reply Last reply
          2
          • J Offline
            J Offline
            JohanSolo
            wrote on 18 Aug 2020, 10:26 last edited by
            #5

            @Natural_Bugger OK, I didn't know you're discovering C++.
            In C++, it's more common to use std::vector instead of static arrays in that case. Those can be resized at any time, have constructors which allows to set all values to an initial one. Your initial snippet would read:

            void someFunction(int n){
            std::vector< int > Array( n, 0 );
            }
            

            And each element would be accessed by Array[ i ] as with a static array. Please note that @J-Hilk's nice solution won't work if the array size is only known at runtime, i.e.

            void someFunction(int n){
            A< n > Array();
            }
            

            isn't valid and won't compile.

            About the crash you're reporting, I'm afraid I lack information on exactly what you're doing and how.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            N 1 Reply Last reply 18 Aug 2020, 11:19
            2
            • J JohanSolo
              18 Aug 2020, 10:26

              @Natural_Bugger OK, I didn't know you're discovering C++.
              In C++, it's more common to use std::vector instead of static arrays in that case. Those can be resized at any time, have constructors which allows to set all values to an initial one. Your initial snippet would read:

              void someFunction(int n){
              std::vector< int > Array( n, 0 );
              }
              

              And each element would be accessed by Array[ i ] as with a static array. Please note that @J-Hilk's nice solution won't work if the array size is only known at runtime, i.e.

              void someFunction(int n){
              A< n > Array();
              }
              

              isn't valid and won't compile.

              About the crash you're reporting, I'm afraid I lack information on exactly what you're doing and how.

              N Offline
              N Offline
              Natural_Bugger
              wrote on 18 Aug 2020, 11:19 last edited by
              #6

              @JohanSolo

              thnx, in the end i changed the program into vector and solved the riddle.

                  vector<int> Array;
                  int setter = 1;
                  for(int i = 0; i < n; i++){
                      Array.push_back(setter);
                      setter++;
                  }
              
              1 Reply Last reply
              0

              1/6

              18 Aug 2020, 09:32

              • Login

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