编程语言书籍解释了在堆栈上创建了值类型,并且在堆上创建了引用类型,而没有解释这两者是什么.我还没有看清楚这个问题.我理解堆栈是什么.但,
language-agnostic heap stack memory-management dynamic-memory-allocation
以下代码为我生成堆栈溢出错误
int main(int argc, char* argv[])
{
int sieve[2000000];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我正在使用Turbo C++,但我想将我的代码保存在C中
编辑:
感谢您的建议.上面的代码只是例如,我实际上在函数中声明了数组而不是在sub main中.此外,我需要将数组初始化为零,所以当我使用Google搜索时,我发现calloc非常适合我的目的.
Malloc/calloc还具有优于堆栈分配的优势,允许我使用变量声明大小.
我有下面列出的代码,并在运行时报告堆栈溢出.我使用pass by value来showTest().我期望它会将Test结构的副本复制到堆栈(推送到堆栈),然后在函数调用结束时Test释放结构(弹出堆栈).所以我打三次电话.它应该推入堆栈并在每个函数调用结束时弹出.
如果每次调用函数时它都会按下并弹出堆栈,我看不到任何堆栈问题.但是,当我运行此代码时,它会在第一行报告堆栈溢出异常main.(我使用Visual Studio 2017.)
如果我删除其中一个showTest()函数调用,那么我可以让它工作.
任何反馈都将受到高度赞赏.
#include <iostream>
struct Test
{
static int Userid;
int data[100000] = { };
Test()
{
++Userid;
};
};
int Test::Userid = 0;
void showTest(Test i_myint)
{
std::cout << "test" << std::endl;
}
int main()
{
Test *pint = new Test();
showTest(*pint);
Test *pint2 = new Test();
showTest(*pint2);
Test *pint3 = new Test();
showTest(*pint3);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 具有大型全局数组的程序:
int ar[2000000];
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
使用大型本地数组的程序:
int main()
{
int ar[2000000];
}
Run Code Online (Sandbox Code Playgroud)
当我在main函数中声明一个大尺寸的数组时,程序崩溃并出现"SIGSEGV(Segmentation fault)".
但是,当我将其声明为全局时,一切正常.这是为什么?
可能重复:
大型阵列上的分段错误
大家好
我正在尝试使用C++在VS 2010中创建一个非常大的数组.
当我尝试创建如下所示的数组时
int dp[4501][4501]
or
int dp[1000][1000]
Run Code Online (Sandbox Code Playgroud)
它引发了异常"Stack Overflow"然后我将其更改为:
int dp[100][100]
Run Code Online (Sandbox Code Playgroud)
一切都好.
所以,如果我想创建一个像上面这样的大数组,我该怎么办?
最好的祝福,
当我试图运行这个
int N=10000000;
short res[N];
Run Code Online (Sandbox Code Playgroud)
我得到分段错误11
当我换到
int N=1000000;
short res[N];
Run Code Online (Sandbox Code Playgroud)
它工作正常
我正在构建一个使用相对较大的表来完成其工作的应用程序(确切地说是LR表).因为我正在生成代码并且表格不是那么大,所以我决定通过生成使用C#集合初始化程序语法在我生成的程序启动时初始化表的代码来序列化我的表:
public static readonly int[,] gotoTable = new int[,]
{
{
0,1,0,0,0,0,0,0,0,0,0,0,0,0,(...)
},
{
0,0,4,0,5,6,0,0,0,0,0,7,0,0,(...)
},
(...)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当我生成一个只有几十万个条目的表时,我生成的应用程序在启动时因StackOverflowException而崩溃.C#编译器编译得很好; 表生成应用程序也运行得很好.实际上,当我切换到Release模式时,应用程序确实启动了.OutOfMemoryException可能已经有了一些意义,但即便如此,我使用的表对于OutOfMemoryException来说是小的.
重现此代码的代码:
警告:在发布模式下尝试下面的代码让我崩溃了Visual Studio 2010; 注意失去未得救的工作.此外,如果生成编译器生成大量错误的代码,Visual Studio也将挂起.
//Generation Project, main.cs:
using (StreamWriter writer = new StreamWriter("../../../VictimProject/Tables.cs"))
{
writer.WriteLine("using System;");
writer.WriteLine("public static class Tables");
writer.WriteLine("{");
writer.WriteLine(" public static readonly Tuple<int>[] bigArray = new Tuple<int>[]");
writer.WriteLine(" {");
for (int i = 0; i < 300000; i++)
writer.WriteLine(" new Tuple<int>(" + i + "),");
writer.WriteLine(" };");
writer.WriteLine("}");
}
//Victim Project, …Run Code Online (Sandbox Code Playgroud) 此代码在数组声明期间产生分段错误.我很困惑为什么会这样.我故意选择2000000000作为值,因为它低于2 ^ 31并且可以适合整数变量.
int main()
{
int nums_size = 2000000000;
int nums[nums_size];
int i;
for(i = 0; i < nums_size; i++) {
nums[i] = i;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将连续数据块从主内存中的一个位置复制到另一个位置.这是我到目前为止所做的,但它不起作用.似乎在应用'memcpy'之后,我的数组'testDump'的内容变为全零.
//Initialize array to store pixel values of a 640x480 image
int testDump[204800];
for(int k = 0; k<204800; k++)
testDump[k] = -9;
//pImage is a pointer to the first pixel of an image
pImage = dmd.Data();
//pTestDump is a pointer to the first element in the array
int* pTestDump = testDump;
//copy content from pImage to pTestDump
memcpy (pTestDump, pImage, 204800);
for(int px_1 = 0; px_1<300; px_1++)
{
std::cout<<"Add of pPixel: "<<pImage+px_1<<", content: "<<*(pImage+px_1);
std::cout<<"Add of testDump: "<<pTestDump+px_1<<", content: "<<*(pTestDump+px_1); …Run Code Online (Sandbox Code Playgroud) 我的问题与C语言有关。我必须创建一个大约 200 万个元素的大数组,但计算机给出了“分段错误(核心转储)”错误。我简单地说:
int integer_array[2000000];
float float_array[2000000];
Run Code Online (Sandbox Code Playgroud)
我确信这与分配给数组的内存有关,但我无法找出正确的解决方案。