问题描述
我只是读完K&安培; R,这是所有我认识的温度。我所有的编辑从Windows命令行下使用的MinGW做的,我也没有先进的调试方法知识(因此我下面第二个节目贫民窟调试的评论)。
I am just about finished reading K&R, and that is all the C that I know. All my compilation is done from Windows command line using MinGW, and I have no knowledge of advanced debugging methods (hence the "ghetto debug" comment in my 2nd program below).
我试图做一些小的测试计划,以帮助我更好地了解内存的分配工作。这些第一对夫妇的程序不使用malloc或免费的,我只是想看看内存是如何分配和取消分配函数的局部标准芯片。这个想法是,我看我的正在运行的进程RAM的使用,看其是否与我的理解相对应。对于低于这个第一个程序,它的工作如我所料。在 alloc_one_meg()
函数分配并初始化25万4字节的整数,但MB是只要函数返回取消分配。所以,如果我调用该函数100万倍成一排,我从来没有看到我的内存使用率去远高于1MB。而且,它的工作原理。
I am trying to make a few small test programs to help me better understand how memory allocation works. These first couple programs do not use malloc or free, I just wanted to see how memory is allocated and de-allocated for standard arrays local to a function. The idea is that I watch my running processes RAM usage to see if it corresponds with what I understand. For this first program below, it does work as I expected. The alloc_one_meg()
function allocates and initializes 250,000 4-byte integers, but that MB is de-allocated as soon as the function returns. So if I call that function 1000000 times in a row, I should never see my RAM usage go much above 1MB. And, it works.
#include <stdio.h>
#include <stdlib.h>
void alloc_one_meg() {
int megabyte[250000];
int i;
for (i=0; i<250000; i++) {
megabyte[i] = rand();
}
}
main()
{
int i;
for (i=0; i<1000000; i++) {
alloc_one_meg();
}
}
有关低于这个第二个方案,当时的想法是不允许的功能退出,具有相同的功能运行1000个拷贝一次,我完成递归。我的理论是,该方案将消耗1GB的RAM完成递归后取消分配这一切之前。但是,它不会让过去通过递归第二循环(见我的贫民区调试注释)。该计划以pretty无信息的(对我)消息(Windows弹出说的 _ 的_。exe文件遇到问题)崩溃。通常我总是可以得到的东西与我的贫民区调试方法底部...但它不是在这里工作。我难倒。这有什么code中的问题?谢谢!
For this second program below, the idea was to not allow the function to exit, to have 1000 copies of the same function running at once, which I accomplished with recursion. My theory was that the program would consume 1GB of RAM before it de-allocated it all after the recursion finished. However, it doesn't get past the 2nd loop through the recursion (see my ghetto debug comment). The program crashes with a pretty non-informative (to me) message (a Windows pop-up saying __.exe has encountered a problem). Usually I can always get to the bottom of things with my ghetto debug method... but it's not working here. I'm stumped. What is the problem with this code? Thanks!
#include <stdio.h>
#include <stdlib.h>
int j=0;
void alloc_one_meg() {
int megabyte[250000];
int i;
for (i=0; i<250000; i++) {
megabyte[i] = rand();
}
j++;
printf("Loop %d\n", j); // ghetto debug
if (j<1000) {
alloc_one_meg();
}
}
main()
{
alloc_one_meg();
}
后续问题发布here.
推荐答案
您正在运行到一个堆栈溢出。
You're running into a stack overflow.
本地自动存储变量(如兆
)分配堆栈,这限制了的空间量上。的malloc分配堆,它允许更大的分配。
Local automatic storage variables (such as megabyte
) are allocated on the stack, which has limited amount of space. malloc allocates on the heap, which allows much larger allocations.
您可以在这里阅读更多:
You can read more here:
(我应该注意的是,C语言不指定内存分配 - 堆和栈的实现细节)
(I should note that the C language does not specify where memory is allocated - stack and heap are implementation details)
这篇关于理解内存分配,测试程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!