关于新运营商的问题 [英] question about new operator

查看:68
本文介绍了关于新运营商的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a [10];







int * p;



p = new int [10];



i声明大小为10的数组,然后再次使用new operator




int a [10] //这也包含相同的内存空间



p = new int [10] //这也包含相同的内存空间。



它们之间有什么区别。

int a[10];

or

int *p;

p=new int [10];

i declare array of size 10 and then again declare array using new operator


int a[10]//this also contain same memory space

p=new int[10] //this also contain same memory space.

what is difference between them.

推荐答案

它们不包含相同的内存空间 - 它们包含相同数量的内存。但是有一个 的区别。



当你写
They don''t contain the same memory space - they contain the same amount of memory. But there is a big difference.

when you write
int a[10];

在堆栈上声明一个包含十个整数的数组。

当你写

You declare an array of ten integers on the stack.
When you write

int *p;
p=new int [10];

你声明了相同的空间量,但这次是在堆上。



区别很重要:堆栈空间被回收当内存分配的函数返回时:内存超出范围,并自动释放。堆空间没有,它必须由 delete 关键字明确发布。

这有两个效果:

1)如果你使用 new 而不使用 delete 那么你的应用程序会占用越来越多的内存,而它永远不会让go - 最终你的机器耗尽内存并崩溃。这称为内存泄漏。

2)更糟糕的是,如果在堆栈上分配内存,然后将该地址传递到函数外部,那么内存有效,但可能会被释放:

You declare the same amount of space, but this time on the heap.

The difference is important: stack space is reclaimed when the function the memory was allocated in returns: the memory goes out of scope, and is automatically released. Heap space does not, it has to be explicitly released by the delete keyword.
There are two effects of this:
1) If you use new without using delete then your application consumes more and more memory, which it never lets go - eventually your machine runs out of memory and crashes. This is called a "memory leak".
2) Worse, if you allocate memory on the stack, and then pass that address outside the function, then memory is valid, but may be released:

int* getP()
   {
   int a[10];
   return &(a[0]);
   }

是有效代码,但返回的内存将被您调用的下一个函数重用,因此相同的内存将用于两个目的。这被称为悬挂参考并且可能导致可怕的间歇性故障,或者是老式的一般保护故障。



错字 - 堆叠 forheap - OriginalGriff [/ edit]

[edit2] Brain fart - freefordelete - OriginalGriff [/ edit2]

Is valid code, but the memory that is returned will be reused by the next function you call, so the same memory will be used for two purposes. This is called a "hanging reference" and can cause horribly intermittent faults, or an old-fashioned "general protection fault".

[edit]Typo - "stack" for "heap" - OriginalGriff[/edit]
[edit2]Brain fart - "free" for "delete" - OriginalGriff[/edit2]


int a[10];



静态声明数组:数组大小在编译时是已知的,它是在堆栈上自动分配的,程序员没有清理的可靠性获得的内存(内存释放自动处理)。




Statically declares the array: the array size is known at compile time, it is automatically allocated on the stack, the programmer has not the resposibility to cleanup the obtained memory (memory deallocation is automatically handled).

int * p;
p = new int [10];



动态分配数组,(一般来说)数组大小在编译时是未知的(如果你写 p = new int [k]; ,而 int a [k]; 不允许 的)。内存分配在上,程序员有责任清理它(使用 delete [] p; )否则发生内存泄漏





如果您不熟悉堆栈内存清理内存泄漏概念,然后我强烈建议你填补你的知识空白。


Dynamically allocates the array, (generally speaking) the array size is not known at compile time, (this is more evident if you write p = new int[k];, while int a[k]; is not allowed). The memory is allocated on the heap, the programmer has the responsibility to clean it up (using delete [] p;) otherwise a memory leak happens.


If you are not familiar with stack, heap, memory cleanup, memory leak concepts, then I strongly suggest you to fill such gaps in your knowledge.


这篇关于关于新运营商的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆