简体   繁体   English

Cpp中的内存问题

[英]Memory issues in Cpp

I have several questions: 我有几个问题:

1) When I statically allocate array of 1 000 000 int, I got stack overflow error: 1)当我静态分配1 000 000 int数组时,出现堆栈溢出错误:

int temp1[48][48];
int im2 [1000000];
int step = 8;
int lcol, lrow;
....

Although, 1000000*sizeof(int)=4 000 000 < 4 Mb. 虽然1000000*sizeof(int)=4 000 000 <4 Mb。 And I have about 8 GB of RAM. 而且我有大约8 GB的RAM。

2) What happens when I use dynamical allocation (new and Malloc functions) and forget to delete my memory? 2)当我使用动态分配(新函数和Malloc函数)而忘记删除内存时会发生什么? Will my future compilations be affected by memory leakages from the past compilations? 我将来的编译会受到过去编译的内存泄漏的影响吗?

3) If yes, how can I fix it? 3)如果是,我该如何解决? Should I close and open visual studio, or I have to reboot my PC? 我应该关闭并打开Visual Studio,还是必须重新启动PC?

4) If I use dynamical allocations with corresponding delete operations correctly, but I work in debug mode (step by step compilation) and I compiled "new" command and didn't compile "delete" command, will there be a memory leakage? 4)如果我正确地使用动态分配和相应的删除操作,但是我在调​​试模式下工作(逐步编译),并且我编译了“ new”命令,而没有编译“ delete”命令,是否会发生内存泄漏?

It's not unusual for platforms to have limitations on stack size. 平台对堆栈大小有限制的情况并不少见。

On every modern platform you are likely to use, a process' address space ceases to exist as soon as it terminates. 在您可能使用的每个现代平台上,进程的地址空间一终止就将不复存在。 So there's no need to do anything about leaks of allocated address space, backed or unbacked, across process termination. 因此,在整个过程终止过程中,无需做任何事情来分配分配的地址空间(支持或不支持)。 The address space ceases to exist because it belongs to the process. 地址空间不再存在,因为它属于进程。

If you allocate without deleting, that address space will be wasted until the program terminates. 如果分配而不删除,该地址空间将被浪费,直到程序终止。 In significant amounts, this can create performance problems and resource consumption problems. 这会产生大量的性能问题和资源消耗问题。

Based on your symptoms , these lines are inside a function: 根据您的症状,这些行在函数内:

int temp1[48][48];
int im2 [1000000];

so they are not static. 因此它们不是静态的。 The C term for this sort of storage is automatic . 此类存储的C项是自动的 You could make them static by using the static keyword, which will make them not subject to stack overflow. 您可以使用static关键字将它们设为static ,这将使它们不受堆栈溢出的影响。 It is not required to use malloc here, although that is an option. 尽管这是一个选择,但此处不需要使用malloc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM