在过去的几年里,我并没有非常使用过C语言.当我今天读到这个问题时,我遇到了一些我不熟悉的C语法.
显然在C99中,以下语法有效:
void foo(int n) {
int values[n]; //Declare a variable length array
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个非常有用的功能.有没有关于将它添加到C++标准的讨论,如果是这样,为什么它被省略?
一些潜在的原因:
C++标准规定数组大小必须是常量表达式(8.3.4.1).
是的,当然我意识到在玩具示例中可以使用std::vector<int> values(m);
,但这会从堆中分配内存而不是堆栈.如果我想要一个多维数组,如:
void foo(int x, int y, int z) {
int values[x][y][z]; // Declare a variable length array
}
Run Code Online (Sandbox Code Playgroud)
该vector
版本变得很笨拙:
void foo(int x, int y, int z) {
vector< vector< vector<int> > > values( /* Really painful expression here. */);
}
Run Code Online (Sandbox Code Playgroud)
切片,行和列也可能遍布整个内存.
看一下comp.std.c++
这个问题的讨论很明显,这个问题在争论的两个方面都有一些非常重要的名字引起争议.毫无疑问,a std::vector
总是更好的解决方案.
当我尝试从我声明的变量创建数组时出错.
int row = 8;
int col= 8;
int [row][col];
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误:
表达式必须具有常量值.
我一直认为在C++中声明一个数组时,大小必须是一个常量整数值.
例如 :
int MyArray[5]; // correct
Run Code Online (Sandbox Code Playgroud)
要么
const int ARRAY_SIZE = 6;
int MyArray[ARRAY_SIZE]; // correct
Run Code Online (Sandbox Code Playgroud)
但
int ArraySize = 5;
int MyArray[ArraySize]; // incorrect
Run Code Online (Sandbox Code Playgroud)
以下是Bjarne Stroustrup在C++编程语言中的解释:
数组的元素数量(数组绑定)必须是常量表达式(§C.5).如果需要变量边界,请使用向量(§3.7.1,§16.3).例如:
void f(int i) {
int v1[i]; // error : array size not a constant expression
vector<int> v2(i); // ok
}
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,上面的代码在我的系统上编译得很好!
这是我试图编译的内容:
void f(int i) {
int v2[i];
}
int main()
{
int i = 3;
int v1[i];
f(5);
}
Run Code Online (Sandbox Code Playgroud)
我没有错!我正在使用GCC v4.4.0.
有什么我想念的吗?
在下面的代码中,我希望在调用Class构造函数时将数组定义为大小为x的数组.我怎样才能做到这一点?
class Class
{
public:
int array[];
Class(int x) : ??? { }
}
Run Code Online (Sandbox Code Playgroud) 你能解释一下sizeof()
随机长度数组的工作原理吗?我认为sizeof()
在编译期间计算数组,但是,具有随机长度的数组的大小似乎是正确计算的.
例:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
srand ( (unsigned)time ( NULL ) );
int r = rand()%10;
int arr[r]; //array with random length
printf("r = %d size = %d\n",r, sizeof(arr)); //print the random number, array size
return 0;
}
Run Code Online (Sandbox Code Playgroud)
多次运行的输出:
r = 8 size = 32
r = 6 size = 24
r = 1 size = 4
Run Code Online (Sandbox Code Playgroud)
编译器:gcc 4.4.3
我的问题很简单(这并不意味着答案会很简单..:D)
为什么C++中的数组包含大小作为类型的一部分而Java不包含?
我知道Java数组引用变量只是指向堆上数组的指针,但C++指向数组的指针也是如此,但我需要提供一个大小.让我们先分析一下C++:
// in C++ :
// an array on the stack:
int array[*constexpr*];
// a bidimensional array on the stack:
int m_array[*constexpr1*][*constexpr2*];
// a multidimensional array on the stack:
int mm_array[*constexpr1*][*constexpr2*][*constexpr3*];
// a dynamic "array" on the heap:
int *array = new int[n];
// a dynamic bidimensional "array" on the heap:
int (*m_array)[*constexpr*] = new int[n][*constexpr*];
// a dynamic multidimensional "array" on the heap:
int (*mm_array)[*constexpr*][*constexpr*] = new int [n][*constexpr1*][*constexpr2*];
Run Code Online (Sandbox Code Playgroud)
n不必是编译时常量表达式,所有元素都是默认初始化的.动态分配的"数组"不是类型数组,但新表达式产生指向第一个元素的指针.
因此,当我创建一个动态数组时,除了第一个维度之外的所有维度都必须是常量表达式(否则我无法声明指针来保存它们的元素).这样对吗??
现在到Java.I只能在堆上分配数组,因为这是Java的工作方式:
// a dynamic array on …
Run Code Online (Sandbox Code Playgroud) 我已经教自己编程了几年,我确信如果你需要变量编号的数组声明,你需要使用malloc
或new
.
今天我发现这在g ++版本4.4.4下编译,没有警告或错误:
#include <iostream>
using namespace std;
int main()
{
int size_array;
cin >> size_array;
int iTable[size_array];
for(int i=0;i < size_array;i++)
iTable[i]=i*i;
for(int i=0;i < size_array;i++)
cout << iTable[i] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你使用gcc(更改后cout
和cin
使用printf
和scanf
),它也可以完全编译
在Visual Studio下,此代码无法编译,因为size_array
它不是常量.
什么时候改变了?这是一种安全的方法吗?
我最近尝试过这个实验,其中我没有为未知大小的内存需求进行动态内存分配,而是进行了静态分配.当a[i]
我声明一个数组时,我保持i
(数组的大小)变量并且依赖于用户给出的输入.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void function(int );
int main(void)
{
int i;
printf("Enter:");
scanf("%d",&i);
function(i);
printf("i = %d\n",i);
getch();
return 0;
}
void function(int i)
{
char a[i];
char b[4];
strncpy(a,"hello",i);
strcpy(b,"world");
int j = 0;
char *c = a;
for( j = 0; j< 20; j++ )
printf("%c",*c++);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
当我在本教程中阅读有关数组初始化的内容时。我发现了这个注释。
type name [elements];
Run Code Online (Sandbox Code Playgroud)
注意:方括号内的 elements 字段
[]
表示数组中元素的数量,必须是常量表达式,因为数组是静态内存块,其大小必须在程序运行之前的编译时确定。*
据我所知,数组在运行时分配内存。这应该是假条吧?或者这意味着什么?
必须在编译时知道数组维度,这意味着维度必须是常量表达式
另外一点是这样的
unsigned count = 42; // not a constant expression
constexpr unsigned size = 42; // a constant expression
Run Code Online (Sandbox Code Playgroud)
我会,然后期望以下声明失败
a[count]; // Is an error according to Primer
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.编译并运行良好.
还有一点奇怪的是,++count;
在数组声明之后也没有引起任何问题.
用-std=c++11
flag打开的程序g++4.71
这是为什么?