I have an array extern int vectorSync []

After some calculations, the size of the array is calculated. Writes to a variable. But for declaring the size of the array, I need a constant. How to move the value of a variable to a constant, and then substitute in the size of the array?

  • 2
    It may be worth thinking about choosing a library container, for example, std :: vector - Croessmah
  • @Croessmah std :: vector - self-expanding if the array index is assigned more than the array is initialized, or not? If not, do you need to finish your class, or is there an implementation of a “self-expanding array”? - nick_n_a
  • @nick_n_a no, will not expand. There are corresponding member functions for the extension. - Croessmah

2 answers 2

Sorry, but the question suggests that you see a purely external side, without seeing the essence ...

The point is not that the size is declared as const , but that it is determined at compile time . If you can perform these calculations at compile time - no problem, but if you can’t, then here’s your path:

 int * vectorSync; .... n = .... vectorSync = new int[n]; 
  • strangely, int ArrSize = 9; int * Arr = new int [ArrSize]; sizeof at any value shows size 4 - Victor Kurenkov
  • sizeof - shows the size of a variable or type in bytes. You have ArrSize - 4-byte int so sizeof (ArrSize) = sizeof (int) = 4 - nick_n_a
  • You need to understand that n is a specific number, and what it should be - you can only say looking at the task. There are implementations of the "self-expanding array", but they are rarely used because they slow down the work. You can malloc and then relloc to expand. But a self-expanding array is a separate issue. - nick_n_a
  • If you, for example, need to sort an array of 10 numbers, then n = 10. And so on, i.e. First you need to set a task. Why do you need an array? - nick_n_a
  • Naturally, the size of your array in the elements is given by the value n . sizeof() also computed at compile time , so it can not be applied to things like dynamic arrays. - Harry

If you don't really like the answer with new at all,

  1. You can set int vectorSync[n] with n = 1000 or 10000 or large enough so that your task will not exceed (there is a limit on the data segment, in a 32-bit program about 1 GB I got 10,000,000, in 64-bit much more). But more optimally [2], and [4].

  2. You can use new or malloc . malloc is done like this

     int * vectorSync; .... vectorSync = malloc(n * sizeof(int)); // или как как вам советовал Harry vectorSync = new int[n]; 
  3. You can specify a constant through the preprocessor #define n 1000 or through the constant const int n = 5*118*sizeof(int); and then use it. int vectorSync[n]; From the "functions" you can only use sizeof - because sizeof is also a constant. The function does not quite call it.

    Regarding sizeof - it is needed in two cases. a) Translate the size into bytes (for example, for malloc ) b) Measure the size of the array int nn =sizeof(array)/sizeof(array[0]); in elements (if we omit the denominator, we get the size in bytes, i.e. [a])

  4. If you really want a function with a variable array in the stack, and it is an array and not int * , you can use the template templates . But vseravno at substitution of a variable in <> - get the error of the constant expression that especially will not get rid of. But you can get out, like so

     template <int n> f(int nn){ int vectorSync[n]; } void Program(){ if (n < 10) f<10>(n); else if (n < 100) f<100>(n); else if (n < 1000) f<1000>(n); else { /*Не хватает памяти*/ } } 

    The compiler will create 3 copies of the function, each for its own volume. A similar design is used in video codecs or image decoders for acceleration. vectorSync will then be declared on the stack. I do not recommend transferring it to the data segment (as defined by the keyword), since the memory in the data segment may also end, and method 1 will be more economical if you need a variable in the data segment. A stack variable means that it is valid until the function that declares it does not return .

  5. The last case is an external variable. You need either through #pragma or through the compiler options to link the library that implements this variable - then its size will be exactly the same as in the connected library. And you can not change the size. The same word extern allows you to set the size later in the adjacent (or in the same) file. Those.

     extern int vectorSync[]; // это может быть файл 1 (предопределение) ... int vectorSync[100];// это может быть как файл 2, так и файл 1.