当我尝试像这样声明一个global二维数组时C++:
int maxX = 10;
int maxZ = 10;
SDL_Rect mapX[maxX][maxZ];
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说 error: variable-size type declared outside of any function
为某些数据分配内存有什么好处.相反,我们可以使用它们的数组.
喜欢
int *lis;
lis = (int*) malloc ( sizeof( int ) * n );
/* Initialize LIS values for all indexes */
for ( i = 0; i < n; i++ )
lis[i] = 1;
Run Code Online (Sandbox Code Playgroud)
我们可以使用普通的数组.
好吧,我不明白malloc是如何工作的,实际上是做什么的.所以解释它们对我来说会更有益.
假设我们sizeof(int) * n只n用上面的代码替换然后尝试存储整数值,我可能面临哪些问题?有没有办法直接从内存分配空间打印存储在变量中的值,例如这里是lis?
是否可以定义char可变长度的 a ?
我有一个长度为 25的char“名称”(struct命名“人”的成员),但我希望它是值 1 和 25 之间的可变长度,因为我想生成char具有不同大小的随机字符串而不是始终具有相同的长度 (25)。该方法的参数之一是sizeof(n.name)。
注:n是一个struct(struct person n)。
在struct“人”的定义是这样的:
struct person{
int c;
char name[25];
};
Run Code Online (Sandbox Code Playgroud)
任何人?
c struct char dynamic-memory-allocation variable-length-array
我试着编写一个返回像素颜色的随机数组的函数,所以当我调用时randomPalette(i),该函数将创建一个随机i颜色数组.以下是我的代码.它表示random[colors]该表达式的错误必须具有恒定值.我不知道为什么.怎么解决?
pixel* randomPalette(int colors){
pixel random[colors] ;
int i, x;
srand(time(NULL)); //generate a random seed
for (i = 0; i < colors; i++){
x = rand() % 256;
random[i].r = x; random[i].g = x; random[i].b = x;
}
return random;
}
Run Code Online (Sandbox Code Playgroud) 我想用C语言制作一个二维数组。
我知道一种方法可以做到这样。
#include <stdlib.h>
void my_func(int **arr)
{
printf("test2: %d\n", arr[0][1]);
}
int main(void)
{
const int row = 3;
const int col = 4;
int **arr = (int **)malloc(sizeof(int *) * 3);
arr[0] = (int *)malloc(sizeof(int) * 4);
arr[1] = (int *)malloc(sizeof(int) * 4);
arr[2] = (int *)malloc(sizeof(int) * 4);
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[0][3] = 4;
arr[1][0] = 3;
arr[1][1] = 4;
arr[1][2] = 5;
arr[1][3] = 6;
arr[2][0] = 5;
arr[2][1] = …Run Code Online (Sandbox Code Playgroud) c declaration definition multidimensional-array variable-length-array
这是我写的:
const int MAX=100;
int main (){
int notas [MAX]={0};
Run Code Online (Sandbox Code Playgroud)
编译器说如下:
[错误] 可能无法初始化可变大小的对象
[警告] 数组初始值设定项中的元素过多
当我用 写作MAX时#define MAX 100,它起作用了。但我不明白这样做有什么问题?
zo@laptop:~/Desktop$ g++ stack.cpp
stack.cpp: In function ‘int main(int, char**)’:
stack.cpp:50:19: error: no matching function for call to ‘boyInitial(Boy [boyNumber])’
boyInitial(boy);
^
stack.cpp:17:6: note: candidate: template<class T, int N> void boyInitial(T (&)[N])
void boyInitial(T (&boy)[N]){
^~~~~~~~~~
stack.cpp:17:6: note: template argument deduction/substitution failed:
stack.cpp:50:19: note: variable-sized array type ‘long int’ is not a valid template argument
boyInitial(boy);
^
stack.cpp:51:19: error: no matching function for call to ‘getBoyAges(Boy [boyNumber])’
getBoyAges(boy);
^
stack.cpp:34:6: note: candidate: template<class T, int N> void getBoyAges(T …Run Code Online (Sandbox Code Playgroud) 当我尝试找到 sizeof(A) 其中 A 的类型为 int 且大小为 'n' 时,n 是一个未定义的 int。我得到 496 的输出,当我给 n 一个值然后检查它时,sizeof(A) 给我的值与 496 相同。我知道 Array 是一种静态数据类型,因此无论“n”如何,它都会有内存谁能解释一下 496 的值是从哪里来的?
int main()
{
int n;
int A[n];
cout<<sizeof(A)<<"\n";
cin>>n;
cout<<sizeof(A);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在得知 VLA(可变长度数组)与 C++ 不兼容后,我第一次开始研究这个兔子洞。这是因为可变长度的数组在编译时不会有一个常量的大小,而 C++ 编译器需要它来计算程序将要分配的堆栈帧的大小运行。我知道这种在编译时知道这样的事情的愿望就是禁止 VLA 数组的原因(以及其他原因)。
但是,为什么需要在编译时知道这样的事情(要分配的堆栈帧的大小)?为什么不能确定堆栈所需的所有内存,然后在运行时分配?我听说它解释说,在编译时确定堆栈帧的大小对于避免堆栈溢出异常很有用,因为您提前确切地知道给定函数调用需要多少内存。如果我们允许堆栈上有一个 VLA,其大小由运行时用户输入确定,他们可以输入一个巨大的数字,例如 100000000000,然后我们会得到堆栈溢出异常。禁止使用 VLA 可以让我们避免做类似的事情(或者至少我是这么理解的)。
这就是向我解释的,但我仍然不明白为什么这意味着一切都必须在编译时确定。例如,alloca可以通过简单地移动堆栈指针来在运行时在堆栈上进行分配。为什么不能在运行时为函数中的每个新局部变量在内部完成此操作,以便我们可以拥有 VLA 而不是立即分配整个堆栈帧?我觉得答案应该是“很明显,所以你最终不会尝试分配比你拥有的更多的内存并导致堆栈溢出异常!”,但无论你采用什么技术,这不是一个风险吗?我可以尝试在运行时在堆上声明我的可变长度数组,但是如果我尝试分配比可用内存更多的内存怎么办?在这种情况下,我遇到了同样的问题,由于用户输入而占用了超出我所能承受的内存,并且在编译时计算程序的内存使用量无法拯救我。
那么,在编译时这样做仅仅是为了提高效率吗?如果是的话,是什么让它更有效率?难道我们只是提前计算需要为函数调用分配多少内存,而不是在运行时计算,从而在每次调用函数时节省几微秒的时间?
c++ stack-overflow alloca compile-time variable-length-array
在任何适用于C或C ++的常规编译器中,可变长度数组均可正常运行,但在Visual Studio Community 2019中,VLA无法正常运行。我如何以任何方式将Visual Studio用作IDE(因为我喜欢它的功能)并且仍然在C和C ++中具有VLA
我试图更改它使用的编译器。我试图找到migwin编译器,但无法做到。所有在线教程都与我在最新版本的Visual Studio 2019中看到的有所不同。
int n;
cin>>n;
int arr[n]; // This line gives an error
Run Code Online (Sandbox Code Playgroud)
int arr [n]; //此行应在Visual Studio 2019中工作。使用什么编译器都没关系。只是我需要让这件事在VS Community 2019中工作,因为我想将其用作IDE。
c ×6
c++ ×6
arrays ×4
declaration ×2
alloca ×1
char ×1
compile-time ×1
definition ×1
function ×1
malloc ×1
sizeof ×1
struct ×1
templates ×1