C++ 禁止可变大小数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11379433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ forbids variable-size array
提问by user1509364
I am using some existing code that someone else has written, and I cannot get it to compile (limited C experience here but I am trying to learn!).
我正在使用其他人编写的一些现有代码,但我无法编译它(这里的 C 经验有限,但我正在努力学习!)。
utilities.cc
公用事业.cc
#include "utilities.h"
FILE *open_file(char *filename, const char*extension, const char *access)
{
char string[MAX_STR_LEN];
FILE *strm = NULL;
if(filename[0]=='Compiling utilities.cc ...
src/utilities.cc: In function ‘FILE* open_file(char*, const char*, const char*)':
src/utilities.cc:251: error: ISO C++ forbids variable-size array
gmake: *** [/home/landon/geant4/work/tmp/Linux-g++/exampleN01/utilities.o] Error 1
')
{
printf("\n INPUT FILENAME (%s) > ",access);
fgets(string,MAX_STR_LEN,stdin);
sscanf(string,"%s",filename);
printf(" FILE %s opened \n", filename);
}
int len=strlen(filename);
if( len + strlen(extension) >= MAX_STR_LEN)
{
printf("\n ERROR: String Length of %s.%s Exceeds Maximum",
filename, extension);
return(NULL);
}
// char *filename1 = new(char[len+strlen(extension)+1]);
const int filenameLength = len+strlen(extension)+1;
char *filename1 = new(char[filenameLength]);
strcpy(filename1,filename); // temp filename for appending extension
/* check if file name has .extension */
/* if it does not, add .extension to it */
int i=len-1;
while(i > 0 && filename[i--] != '.');
// printf("\n Comparing %s to %s", extension, filename+i+1);
if(strcmp(extension, filename+i+1) )
strcat(filename1,extension);
if( (strm = fopen(filename1, access) ) == NULL )
{
printf("\n ERROR OPENING FILE %s (mode %s)", filename1,access);
}
delete(filename1);
return(strm);
}
Here is the error.
这是错误。
char *filename1 = new(char[filenameLength]);
The error on line 251 refers to
第 251 行的错误是指
char filename1char[filenameLength];
If you need any additional information let me know please.
如果您需要任何其他信息,请告诉我。
回答by mfontanini
The error is correct. VLA(variable size arrays) are forbidden in C++. This is a VLA:
错误是正确的。VLA(可变大小数组)在 C++ 中是被禁止的。这是一个 VLA:
char *filename1 = new char[filenameLength];
What you probably meant is this:
你的意思可能是这样的:
delete[] filename1;
Which is not a VLA, but an array of char
s allocated on the heap. Note that you should delete this pointer using operator delete[]
:
这不是 VLA,而是char
在堆上分配的s数组。请注意,您应该使用 operator 删除此指针delete[]
:
char *filename1 = new char[filenameLength];
回答by mathematician1975
Try this instead
试试这个
char filename1[filenamelength];
you cannot create an array as a local variable length array on the stack like this
你不能像这样在堆栈上创建一个数组作为局部可变长度数组
delete [] filename1;
unless filenamelength
is declared as const
.
除非filenamelength
被声明为const
.
Also as you have allocated memory for an array, you should free the memory using
此外,由于您已为数组分配了内存,因此您应该使用
##代码##otherwise you will have a memory leak. Also it is not essential to have parentheses around your return
values;
否则你会有内存泄漏。此外,在您的return
值周围加上括号也不是必需的;
回答by user1095108
They are forbidden, but a workaround is to use a stack allocator, for example:
它们是被禁止的,但一种解决方法是使用堆栈分配器,例如:
http://howardhinnant.github.io/stack_alloc.html
http://howardhinnant.github.io/stack_alloc.html
You can use the stack allocator with a ::std::vector
(or with some other container, or just directly) and you've got yourself a VLA
.
您可以将堆栈分配器与 a ::std::vector
(或其他容器,或直接使用)一起使用,并且您已经获得了一个VLA
.