• 5207阅读
  • 8回复

大量使用 new 的好处? [复制链接]

上一主题 下一主题
离线snailbing
 

只看楼主 倒序阅读 楼主  发表于: 2010-03-03
我总监很喜欢在代码中大量的使用 指针 和 new,无论是在结构体中,类中还是函数中
比较典型的如下

定义一个类
class classa{
....
};

调用这个类的函数
fun(){
classa *a = new classa;
a.value();
delete a;
}

这么做有什么好处,能节省系统资源还是提高速度?


fun(){
classa a;
a.value();
}

有什么区别?
如果谁懂的能和我说一下吗。(他这样写有些地方太容易出内存泄露的情况了,但他是领导,如果他这样有理我也只能尽量说服自己他是对,但这样没什么区别,那么我就只有.....他还领导我还是不能怎么样)
离线programmerhu
只看该作者 1楼 发表于: 2010-03-03
大量new, 就是说明有大量指针
大量指针的好处是参数传递会快.
使用new要小心加上delete.
一般局部变量, 比较少用new吧, 会更安全.
如果a->value()中有抛出异常, 就不会delete了, 你要保证value()是异常安全的.
豌豆框架 Wonderful Framework
http://www.wonderfulproject.cn

GreyFrame: 简单,易用,好用的Javascript遮罩弹出框架.
http://programmer.huang-home.net/GreyFrame/
离线joel
只看该作者 2楼 发表于: 2010-03-03
变量存储位置不一样
classa *a = new classa;   ----堆
classa a;   ----栈

两个还是有些区别的
离线hbsunjp
只看该作者 3楼 发表于: 2010-03-03
在函数中,使用new没有大小限制,直接分配受栈大小限制。
离线steinlee

只看该作者 4楼 发表于: 2010-03-04
Memory has mainly two parts: heap(堆)  and stack(栈).
    1. anything related to new: heap memory use
         for example: int * a = new int[ 10 ];
                                class A; A * a = new A;
     2. otherwise it is stack memory use.
         for example: int a[20];
                                A a ;
         note that here (A)a sits on stack and may have pointers which point to heap if there
are dynamically memory allocations in the constructor.

    Using new means dynamically allocating heap(堆) memory to variables. One of the main
reasons is that the sizes of many variables are often unknown in compiling time.
Their memory use has to be determined in running time. Therefore, new has to be used.
Note that on Windows stack(栈) memory is limited. Heap memory access can be slower than
stack memory access because the data in stack memory is together. The data in heap
memory may be separated(think of new and delete).

    One of the main disadvantages of using new is that in pure C++ code it is the responsibility
of users to clean heap memory(delete pointer) up. If users do not do it, there is memory
leak. If users do it twice(duplicate delete), their code will crash. I normally work this way to
avoid memory leak and duplicate delete: after I use new to a pointer, I write delete this pointer
immediately at the right spot before this pointer is used anywhere. In this way, I never forget
to clean my heap memory.  Also, I do this only once. Therefore, duplicate delete can be prevented.

   Memory leak is not hard to detect and fix. Valgrind is a good free tool for this on Linux.
Run Valgrind to your code regularly to check memory problems.

Note that in Qt all GUI components have to be created by new because of garbage collection
and do not need to be cleaned. It is same as Java. Qt is much quicker than Java.

One typical delete error:
   double * a = new double[ 10 ];
   delete a; a = NULL;
It should be:
   delete[] a; a = NULL;

For a dynamic array of primitive variables(float, int, double etc.), delete[] is required to clean heap memory.

After a pointer is deleted, always assign NULL to the pointer. Otherwise, the code will crash if it is deleted
again(duplicate delete) since it still points to the same location(called dangled pointer) after it is deleted.
If this pointer is assigned with NULL, nothing happens when a NULL pointer is deleted.
[ 此帖被steinlee在2010-03-04 14:45重新编辑 ]
Looking for remote C/C++ and Qt 兼职
离线luohui8891

只看该作者 5楼 发表于: 2010-03-04
英文看的好累。不过大多看懂了。
请问这个是楼上自己写的还是借鉴的?请问出处?
离线steinlee

只看该作者 6楼 发表于: 2010-03-04
Sorry for my awkward English. I am pretty slow at typing Chinese words.
The stuff above is a bit experience of mine. No link for you.
Looking for remote C/C++ and Qt 兼职
离线zcycool
只看该作者 7楼 发表于: 2010-03-04
栈由系统自动分配,速度较快。但程序员是无法控制的。
堆是由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便.
离线luohui8891

只看该作者 8楼 发表于: 2010-03-04
引用第6楼steinlee于2010-03-04 11:24发表的  :
Sorry for my awkward English. I am pretty slow at typing Chinese words.
The stuff above is a bit experience of mine. No link for you.


That is great.Your experience is helpful.And sorry for my poor English.
快速回复
限100 字节
 
上一个 下一个