简体   繁体   English

C ++中vlas的编译时与运行时const变量分配和分配

[英]Compile- vs run-time const variable assignment and allocation of vlas in C++

I was working on a template function with non-type parameters (to avoid dynamic allocation of arrays) when a number of questions arose. 当出现许多问题时,我正在使用带有非类型参数的模板函数(以避免动态分配数组)。 My first question regards compile-time variable assignment. 我的第一个问题是关于编译时变量分配的。 This arose from the following attempts at calls to the template function: 这是由于以下尝试调用模板函数引起的:

template<int n>
int *getDegrees(int A[][n]) {
  //return degrees
}

int main(int argc, char **argv) {
  int n = 10;
  int A[n][n];
  int *degs = getDegrees<n>(A);
}

Here, we have two errors: first, the compiler is unable to resolve the call to getDegrees(A) : 这里,我们有两个错误:首先,编译器无法解决对getDegrees(A)的调用:

main.cc:27: error: no matching function for call to ‘getDegrees(int [(((long unsigned int)(((long int)n) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)n) + -0x00000000000000001)) + 1)])’

Second, we're unable to use n in the template call as it isn't a constant expression. 其次,我们不能在模板调用中使用n ,因为它不是常量表达式。 Simply making n constant does resolve the issues 简单地使n不变就可以解决问题

const int n = 10;

however, if I were to do 但是,如果我要做

int m = 10;
const int n = m; 

we get the same errors. 我们得到同样的错误。 While the second assignment may be allowed by the compiler, is it considered bad form to do so? 虽然编译器可能允许第二个分配,但这样做是否被认为是不好的形式? Additionally, why would making n constant make a difference in resolving the function call? 另外,为什么使n常数在解析函数调用中有所不同?

My other question regards vlas: is memory allocated for them on the stack or the heap (and is this compiler-dependent)? 我的另一个问题与vlas有关:是在堆栈还是堆上为它们分配了内存(并且此内存依赖于编译器)? There appears to have been some controversy in even allowing them in C++, should they be avoided in favor of vectors (or similar containers)? 甚至在C ++中允许使用它们似乎都存在争议,是否应避免使用矢量(或类似容器)?

Appreciate any insight! 感谢任何见识!

I will try to answer whatever I could get from your question. 我将尽力回答您的问题。
You can change the function prototype to receive the array by reference: 您可以更改函数原型以通过引用接收数组:

template<size_t n> // see the type
int *getDegrees(int (&A)[n][n]) {  // see the signature
  // ...
}

In above code, we are leveraging the fact that the array has same dimensions. 在上面的代码中,我们利用了数组具有相同尺寸的事实。
However, in general case it should be: 但是,一般情况下应该是:

template<size_t n1, size_t n2> // see the type
int *getDegrees(int (&A)[n1][n2]) {  // see the signature
  // ...
}

If the size is too big then an error is issued by the compiler to inform you. 如果大小太大,则编译器会发出错误通知您。 eg (from g++): 例如(来自g ++):

error: size of array 'A' is too large 错误:数组“ A”的大小太大

Now coming to the other question regarding the difference between assignment of the constant integer. 现在来看另一个有关常量整数赋值之间差异的问题。
In C++, the array size has to be compile time constant and 在C ++中,数组大小必须是编译时间常数,并且

const int n = 10;

fulfills that requirement. 满足该要求。 Because the compiler can make out that, 10 is a literal number being assigned to n . 因为编译器可以确定,所以10是分配给n的文字数字。

In case of, 的情况下,

int m = 10;
const int n = m;

Compiler finds out that the source of n is not a compile time constant itself. 编译器发现n的来源本身不是编译时间常数。 And thus it makes the code ill formed. 因此,这会使代码格式错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内存分配(C ++)编译时/运行时? - Allocation of memory ( C++ ) Compile-time/Run-time? eclipse c ++中的“控制到达非空函数的末尾”警告,但没有编译时或运行时错误 - “control reaches end of non-void function” warning in eclipse c++ but no compile- or run-time errors C ++向量内存分配和运行时? - C++ Vector memory allocation & run-time? C ++在运行时分配一个const值? - C++ Assign a const value at run-time? C++ 编译时条件运行时语句 - C++ Compile-Time Conditional Run-Time Statements 在运行时分配的函数中的 const 变量 - const variable in function assigned at run-time 编译时间字符串分配以进行运行时识别 - Compile time string assignment for run-time identification 使用辅助函数(C ++)定义运行时已知的全局const变量 - Define global const variables known at run-time with helper functions (c++) 带有 const 引用的 C++ Setter 成员函数在运行时导致“读取访问冲突”异常 - C++ Setter member function with const reference causes "read access violation" exception at run-time 我可以在C ++中的运行时初始化静态const成员吗? - Can I initialize a static const member at run-time in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM