When i run this code in my Devcpp compiler->
当我在我的Devcpp编译器——>中运行这段代码时
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vec;
for(int i=0;i<100000000;i++)
vec.push_back(i);
}
It works even on run time. But when i run->
它甚至可以在运行时运行。但当我运行- >
#include<bits/stdc++.h>
using namespace std;
int arr[1000000000];
int main()
{
return 0;
}
It gives me link error.
它给了我链接错误。
As long as space is required both arr and vec requires the same space.Then why is it that vec code runs even fine on run time but arr code doesnt even compile.
只要需要空间,arr和vec都需要相同的空间。那么为什么vec代码在运行时运行得很好,而arr代码甚至没有编译。
2 个解决方案
#1
9
The issue is with the allocation. In the first case, std::vector
default allocator uses dynamic allocation, which in principle can allocate as much memory as you want (bounded of course by the OS and the amount of physical memory) whereas in the second case it uses the memory available for static allocation (technically the array has static storage duration), which in your case is smaller than 1000000000 * sizeof int
bytes. See this for a nice answer regarding the various types of allocations in a C program (which also applies for C++).
问题在于分配。在第一种情况下,std::向量默认分配器使用动态分配,原则上可以分配你想要尽可能多的内存(当然有界由操作系统和物理内存的数量),而在第二种情况下它使用静态分配的可用内存(技术数组静态存储时间),在你的情况下小于1000000000 * sizeof int字节。关于C程序中各种类型的分配(也适用于c++),请参见本文获得一个很好的答案。
Btw, avoid #include<bits/stdc++.h>
, as it is non-standard. Include only the standard headers you need. One more issue: I don't think you get a compile-time error, you probably get a run-time error. In other words, the code compiles just fine, but fails to run.
顺便说一句,避免# include <比特 stdc + +。h> ,因为它是非标准的。只包含您需要的标准头。还有一个问题:我认为您不会得到编译时错误,您可能会得到运行时错误。换句话说,代码编译良好,但无法运行。
#2
3
It seems that the object
似乎是物体
int arr[1000000000];
is too large to fit in the global data of your program for your environment. I don't get a compile time error but I get a link time error in my environment also (cygwin/g++ 4.9.3).
对于您的环境来说,它太大了,无法容纳程序的全局数据。我没有得到编译时间错误,但是在我的环境中也有一个链接时间错误(cygwin/g+ 4.9.3)。
Reducing the size by one tenth work for me. It may work for you also. I don't know how you can determine the maximum size of objects that can fit in global data.
我把尺寸缩小了十分之一。它可能对你也有用。我不知道如何确定能够容纳全局数据的对象的最大大小。
Space available in stack is the smallest in size.
Space available in global data is larger that that.
Space available in heap is the largest of all.
堆栈中的可用空间是最小的。全局数据中可用的空间比这个要大。堆中可用的空间最大。
If your object is too large to fit in stack, try to put into global data.
If your object is too large to fit in global data, use heap.
如果您的对象太大,无法放入堆栈,请尝试将其放入全局数据。如果您的对象太大,无法容纳全局数据,请使用heap。
#1
9
The issue is with the allocation. In the first case, std::vector
default allocator uses dynamic allocation, which in principle can allocate as much memory as you want (bounded of course by the OS and the amount of physical memory) whereas in the second case it uses the memory available for static allocation (technically the array has static storage duration), which in your case is smaller than 1000000000 * sizeof int
bytes. See this for a nice answer regarding the various types of allocations in a C program (which also applies for C++).
问题在于分配。在第一种情况下,std::向量默认分配器使用动态分配,原则上可以分配你想要尽可能多的内存(当然有界由操作系统和物理内存的数量),而在第二种情况下它使用静态分配的可用内存(技术数组静态存储时间),在你的情况下小于1000000000 * sizeof int字节。关于C程序中各种类型的分配(也适用于c++),请参见本文获得一个很好的答案。
Btw, avoid #include<bits/stdc++.h>
, as it is non-standard. Include only the standard headers you need. One more issue: I don't think you get a compile-time error, you probably get a run-time error. In other words, the code compiles just fine, but fails to run.
顺便说一句,避免# include <比特 stdc + +。h> ,因为它是非标准的。只包含您需要的标准头。还有一个问题:我认为您不会得到编译时错误,您可能会得到运行时错误。换句话说,代码编译良好,但无法运行。
#2
3
It seems that the object
似乎是物体
int arr[1000000000];
is too large to fit in the global data of your program for your environment. I don't get a compile time error but I get a link time error in my environment also (cygwin/g++ 4.9.3).
对于您的环境来说,它太大了,无法容纳程序的全局数据。我没有得到编译时间错误,但是在我的环境中也有一个链接时间错误(cygwin/g+ 4.9.3)。
Reducing the size by one tenth work for me. It may work for you also. I don't know how you can determine the maximum size of objects that can fit in global data.
我把尺寸缩小了十分之一。它可能对你也有用。我不知道如何确定能够容纳全局数据的对象的最大大小。
Space available in stack is the smallest in size.
Space available in global data is larger that that.
Space available in heap is the largest of all.
堆栈中的可用空间是最小的。全局数据中可用的空间比这个要大。堆中可用的空间最大。
If your object is too large to fit in stack, try to put into global data.
If your object is too large to fit in global data, use heap.
如果您的对象太大,无法放入堆栈,请尝试将其放入全局数据。如果您的对象太大,无法容纳全局数据,请使用heap。