数组的动态分配
Dynamic allocation of array
为什么在C++中不能动态分配数组,却能成功编译以下代码?取消注释注释时显示错误??
#include<iostream>
#include <string>
using namespace std;
int main()
{
string aa;
cin>>aa;
int a[aa.size()];// though allocating the array dynamically the compilation succeeded
cout<<"COMPILATION SUCCESS"<<endl;
/*char *p;
cin>>p;
int y=sizeof(p);
int b[y];
cout<<"COMPILATION ERROR"<<endl;
*/
/*
int tt;
cin>>tt;
int c[tt];// shows error
cout<<"ERROR";
*/
}
因为您似乎在使用允许这样做的编译器。C++中的VLA是GNU扩展,你有可能用g++
或clang++
编译它吗?
将编译器设置为严格的ISO C++模式,它会发出警告或出错。
我从clang++
:得到了什么
h2co3-macbook:~ h2co3$ clang++ -o quirk quirk.cpp -Wall -std=c++11 -pedantic
quirk.cpp:6:9: warning: variable length arrays are a C99 feature [-pedantic,-Wvla]
char cs[s.size() + 1];
^
quirk.cpp:6:7: warning: unused variable 'cs' [-Wunused-variable]
char cs[s.size() + 1];
^
2 warnings generated.
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 动态更改数组的大小并读取值.(无矢量)
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 动态维度数组的 C++ 别名指针
- C++ 在析构函数调用之前删除的动态成员数组
- 从类 c++ 动态创建数组
- 具有静态 std::array 的神经网络比使用动态 C 数组的神经网络慢
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 为什么我们在传递动态 2D 数组时不需要列数?
- 如何解决在使用动态 2D 数组进行矩阵乘法的 MPI 进行并行编程时的问题
- 使用动态布尔数组时出现问题
- C++ 在类中删除动态 2D 数组时不断"Aborted (core dumped)"
- 如何在C 中动态输入数组
- 关于删除动态对象数组
- 在C 中动态更改数组大小
- 使用动态指针数组进行动态对象分配 - 使用什么删除?
- 动态字符串数组分配错误
- 函数中的动态 2D 数组分配并将其返回给主数组
- c++动态字符串数组分配
- 使用字符串或字符动态分配(数组)