我对C++比较陌生,从一开始就钻进了我不能做的事情
int x;
cin >> x;
int array[x];
Run Code Online (Sandbox Code Playgroud)
相反,您必须使用动态内存.但是,我最近发现上面会编译(虽然我得到了一个警告说它被ISO C++禁止).我知道,如果标准不允许这样做,这显然是一个坏主意,但我以前甚至都不知道这是可能的.
我的问题是,为什么g ++允许在标准不允许的情况下不动态分配的可变长度数组?另外,如果编译器可以这样做,为什么不在标准中呢?
我有一段时间没有使用过c ++,但我刚刚用它开始了一个项目.这可能是不可能的,但我试图用一个数组创建一个模板类,该数组将其大小设置为我试图用构造函数设置的常量的值.
这是构造函数的代码:
Tarray(int s): start_size(s){
}
Run Code Online (Sandbox Code Playgroud)
这是设置数组大小的代码:
const int start_size;
T this_array[start_size];
Run Code Online (Sandbox Code Playgroud)
这是整个文件:
#ifndef TARRAY_H_
#define TARRAY_H_
template<typename T>
class Tarray {
private:
const int start_size;
T this_array[start_size];
int array_size;
public:
Tarray(int s): start_size(s){
}
~Tarray(){
delete[] this_array;
}
T & operator[](int i){
return this_array[i];
}
};
#endif /* TARRAY_H_ */
Run Code Online (Sandbox Code Playgroud)
这些是我得到的错误:
..\/template_array/Tarray.h:16:24: error: 'Tarray<T>::start_size' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: 'new' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: ISO C++ forbids initialization of member 'this_array' [-fpermissive]
..\/template_array/Tarray.h:16:34: …Run Code Online (Sandbox Code Playgroud) 这在C语言中有效吗?
#include <stdio.h>
int main()
{
int i = 5;
int a[i]; // Compiler doesn't give error here. Why?
printf("%d",sizeof(a)); //prints 5 * 4 =20. 4 is the size of integer datatype.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器不会在声明中给出错误int a[i];.我不是一个常数,那么它怎么能成功编译?是因为我使用的是gcc编译器吗?是否允许在C++中使用?
我目前正在学习和练习本文档中的试错方法,我偶然发现了这个问题:
\n\n给定一个包含m行和n的整数网格。找出边与格子边缘平行(每条边的长度都大于1)且位于其边界上的数字之和最大的矩形。
\n\n
\n- 输入:
\n
\n第一行包含 2 个整数m和n。
\n下一个m行:每行包含 n 个描述网格行的整数。- 输出:
\n
\n我们找到的最大总和。- 约束:
\n
\n2 <= m,n <= 500
\n运行时 < 3\xe2\x80\xafs例如:
\n输入:
\nRun Code Online (Sandbox Code Playgroud)\n5 4\n\xe2\x80\x87 9\xe2\x80\x87-2\xe2\x80\x87-1\xe2\x80\x87 3\n-10\xe2\x80\x87-5\xe2\x80\x87 1\xe2\x80\x87-4\n\xe2\x80\x87 1\xe2\x80\x87-1\xe2\x80\x87 2\xe2\x80\x87-2\n\xe2\x80\x87 3 \xe2\x80\x870\xe2\x80\x87 0\xe2\x80\x87-1\n\xe2\x80\x87 2\xe2\x80\x87 2\xe2\x80\x87-1\xe2\x80\x87 2\n输出:
\nRun Code Online (Sandbox Code Playgroud)\n8\n解释:
\n这里,位于其边界上的数字之和最大的矩形是:
\n1\xe2\x80\x87-1\xe2\x80\x87 2
\n
\n3\xe2\x80\x87 0\xe2\x80\x87 0
\n2\xe2\x80\x87 2\xe2\x80\x87-1总和为 1 + -1 …
我正在使用二维结构数组,这是另一个结构的一部分.这不是我做了很多事情所以我遇到了问题.在接近结束的"测试"for-loop之后,此函数最终失败.它在出现故障之前正确打印出一行.
我的代码中将数据读入虚拟2-d结构数组的部分工作正常,因此必须将我的赋值数组作为另一个结构(imageStruct)的一部分.
任何帮助将不胜感激!
/*the structure of each pixel*/
typedef struct
{
int R,G,B;
}pixelStruct;
/*data for each image*/
typedef struct
{
int height;
int width;
pixelStruct *arr; /*pointer to 2-d array of pixels*/
} imageStruct;
imageStruct ReadImage(char * filename)
{
FILE *image=fopen(filename,"r");
imageStruct thisImage;
/*get header data from image*/
/*make a 2-d array of of pixels*/
pixelStruct imageArr[thisImage.height][thisImage.width];
/*Read in the image. */
/*I know this works because I after storing the image data in the
imageArr array, I printed …Run Code Online (Sandbox Code Playgroud) 与MSVC++ 2008相比,我最近发现了一个有趣的g ++行为.考虑这个小程序:
#include <cstdlib>
const int ARR_LENGTH = 512;
void doSomething( int iLen );
int main( int argc, char** argv )
{
doSomething( ARR_LENGTH );
return 0;
}
void doSomething( int iLen )
{
int iTest[iLen];
return;
}
Run Code Online (Sandbox Code Playgroud)
它会编译吗?你怎么看?根据我对C(或C++)的了解,这不应该编译,因为我可以用我想要的任何整数调用函数doSomething(),因此在编译时无法确定iTest数组的大小.但是,当我尝试用g ++编译它时,它工作得很好.现在我可以理解这里可能发生了什么 - 编译器注意到我只将一个编译时常量作为参数调用此函数.这里有一些严肃的优化......但是当我尝试使用MSVC++ 2008编译它时,我得到了这个:
1>c:\prj\test\test.cpp(15) : error C2057: expected constant expression
1>c:\prj\test\test.cpp(15) : error C2466: cannot allocate an array of constant size 0
1>c:\prj\test\test.cpp(15) : error C2133: 'iTest' : unknown size
Run Code Online (Sandbox Code Playgroud)
我的问题是:这如何符合语言的定义(C标准(C++标准))?g ++是否可以进行这样的优化(在这种情况下很容易看到,但是第一次遇到它时,它出现在一个大型项目中,乍一看并没有多大意义).
c++ compiler-construction optimization compile-time-constant
#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;
template <class T>
void sort(int n, T a[]){
for(i=0;i<n-1;i++){
for(j=i;j<n;j++){
if(a[i] > a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main(){
int size;
cout<<" Please input the amount of numbers you would like to sort"<<endl;
cin>>size;
int Amta[size];
for(int i=0; i<size; i++){
cout<<"Please enter the "<<size+1<< "number";
cin>>Amta[i];
}
Sleep(100000);
}
Run Code Online (Sandbox Code Playgroud)
我试图获取用户想要从用户输入的数量并将其存储在可变大小中.
但是当我初始化时,array Amta[size]我得到以下编译错误:
表达式必须具有恒定值
和
C2057:预期的常量表达式"编译错误.
我一直认为c ++中不允许使用变长数组(参考:为什么变量长度数组不是C++标准的一部分?).但是为什么这段代码编译和工作?
#include <iostream>
using namespace std;
int main () {
int n;
cin >> n;
int a[n];
for (int i=0; i<n; i++) {
a[i] = i;
}
for (int i=0; i<n; i++) {
cout << a[i] << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 为什么clang(9.0.0)无法编译此代码?GCC(9.2.0)没问题。
使用typedef int arr[y][z]; arr *a;或auto a = ...可以正常使用,但使用内联类型会产生奇怪的错误消息error: assigning to 'int (*)[y][z]' from incompatible type 'int (*)[y][z]'。这些类型对我来说看起来很兼容。两种不同的类型是否可能具有相同的语法?
#include <iostream>
int main() {
int x, y, z;
std::cin >> x >> y >> z;
int (*a)[y][z];
a = (int(*)[y][z]) new int[x * y * z];
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) …Run Code Online (Sandbox Code Playgroud)