C++ 使用向量时堆栈溢出
我为学校的小比赛做了一个节目,我有:C++ 使用向量时堆栈溢出,c++,vector,stack-overflow,C++,Vector,Stack Overflow,我为学校的小比赛做了一个节目,我有: #include <iostream> #include <vector> int main(){ int n; std::cin >> n; int c[n]; std::vector<int> r(n); std::cout << "I made it through"; //rest of program } #包括 #包括 int main(){ int
#include <iostream>
#include <vector>
int main(){
int n;
std::cin >> n;
int c[n];
std::vector<int> r(n);
std::cout << "I made it through";
//rest of program
}
#包括
#包括
int main(){
int n;
标准:cin>>n;
int c[n];
std::向量r(n);
std::cout堆栈是一种非常有限的资源
<>虽然编译器似乎在C++中实现了C风格的VLA(),但它并没有神奇地获得更多内存。
测试1)读取n
的成功,以及2)在执行分配堆栈数组的语句之前,n
不超出您的使用范围
Windows的默认堆栈大小:1MBsizeof(int)
:4因此,大约250000适合。
Linux的默认堆栈大小:8MBsizeof(int)
:4因此,大约适合2000000个
解决方法:对int
s使用动态分配,如使用std::vector
:
std::vector<int> c(n);
std::向量c(n);
或者,至少使用智能指针:
std::unique_ptr<int[]> c(new int[n]);
std::unique_ptr c(新int[n]);
堆栈是一种非常有限的资源
<>虽然编译器似乎在C++中实现了C风格的VLA(),但它并没有神奇地获得更多内存。
测试1)读取n
的成功,以及2)在执行分配堆栈数组的语句之前,n
不超出您的使用范围
Windows的默认堆栈大小:1MBsizeof(int)
:4因此,大约250000适合。
Linux的默认堆栈大小:8MBsizeof(int)
:4因此,大约适合2000000个
解决方法:对int
s使用动态分配,如使用std::vector
:
std::vector<int> c(n);
std::向量c(n);
或者,至少使用智能指针:
std::unique_ptr<int[]> c(new int[n]);
std::unique_ptr c(新int[n]);
堆栈是一种非常有限的资源
<>虽然编译器似乎在C++中实现了C风格的VLA(),但它并没有神奇地获得更多内存。
测试1)读取n
的成功,以及2)在执行分配堆栈数组的语句之前,n
不超出您的使用范围
Windows的默认堆栈大小:1MBsizeof(int)
:4因此,大约250000适合。
Linux的默认堆栈大小:8MBsizeof(int)
:4因此,大约适合2000000个
解决方法:对int
s使用动态分配,如使用std::vector
:
std::vector<int> c(n);
std::向量c(n);
或者,至少使用智能指针:
std::unique_ptr<int[]> c(new int[n]);
std::unique_ptr c(新int[n]);
堆栈是一种非常有限的资源
<>虽然编译器似乎在C++中实现了C风格的VLA(),但它并没有神奇地获得更多内存。
测试1)读取n
的成功,以及2)在执行分配堆栈数组的语句之前,n
不超出您的使用范围
Windows的默认堆栈大小:1MBsizeof(int)
:4因此,大约250000适合。
Linux的默认堆栈大小:8MBsizeof(int)
:4因此,大约适合2000000个
解决方法:对int
s使用动态分配,如使用std::vector
:
std::vector<int> c(n);
std::向量c(n);
或者,至少使用智能指针:
std::unique_ptr<int[]> c(new int[n]);
std::unique_ptr c(新int[n]);
您可能只需要动态分配数组++
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
int *c = new int[n];
if(nullptr == c) {
std::cerr << "Runtime error" << std::endl;
return 1;
}
std::vector<int> r(begin(n), end(n));
std::cout << "I made it through";
delete[] c;
return 0;
}
#包括
#包括
int main()
{
int n;
标准:cin>>n;
int*c=新的int[n];
if(nullptr==c){
您可能只需要动态地分配数组++
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
int *c = new int[n];
if(nullptr == c) {
std::cerr << "Runtime error" << std::endl;
return 1;
}
std::vector<int> r(begin(n), end(n));
std::cout << "I made it through";
delete[] c;
return 0;
}
#包括
#包括
int main()
{
int n;
标准:cin>>n;
int*c=新的int[n];
if(nullptr==c){
您可能只需要动态地分配数组++
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
int *c = new int[n];
if(nullptr == c) {
std::cerr << "Runtime error" << std::endl;
return 1;
}
std::vector<int> r(begin(n), end(n));
std::cout << "I made it through";
delete[] c;
return 0;
}
#包括
#包括
int main()
{
int n;
标准:cin>>n;
int*c=新的int[n];
if(nullptr==c){
您可能只需要动态地分配数组++
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
int *c = new int[n];
if(nullptr == c) {
std::cerr << "Runtime error" << std::endl;
return 1;
}
std::vector<int> r(begin(n), end(n));
std::cout << "I made it through";
delete[] c;
return 0;
}
#包括
#包括
int main()
{
int n;
标准:cin>>n;
int*c=新的int[n];
if(nullptr==c){
std::cerr问题不在于vector
(它在大多数STL实现中使用堆作为其底层扩展存储),而是int c[n]
,它将在堆栈中分配1000000个4字节整数,几乎为4MB。在Win32上,堆栈默认为1MB左右,因此溢出
如果您确实需要使用数组,那么使用<代码> new < /C> >更改要在堆上分配的<代码> C>代码>数组,但是不要忘记<代码> DELTE> [C]<代码>,否则对于大多数扩展存储场景,最好使用<代码> vector < /C>。如果需要固定长度数组,则考虑<代码>数组< /C> >(新C++ 11)这增加了边界检查。
问题不在于vector
(它在大多数STL实现中使用堆作为其底层扩展存储),而是int c[n]
,它将在堆栈中分配1000000个4字节整数,几乎是4MB。在Win32上,堆栈默认为1MB左右,因此溢出
如果您确实需要使用数组,那么使用<代码> new < /C> >更改要在堆上分配的<代码> C>代码>数组,但是不要忘记<代码> DELTE> [C]<代码>,否则对于大多数扩展存储场景,最好使用<代码> vector < /C>。如果需要固定长度数组,则考虑<代码>数组< /C> >(新C++ 11)这增加了边界检查。
问题不在于vector
(它在大多数STL实现中使用堆作为其底层扩展存储),而是int c[n]
,它将在堆栈中分配1000000个4字节整数,几乎是4MB。在Win32上,堆栈默认为1MB左右,因此溢出
如果确实需要使用数组,请使用new
更改要在堆上分配的c
数组,但不要忘记delete[]
,否则最好使用vector