C/C++ Basic-- the differs between [malloc&free] and [new &delete]
2008-04-08 15:05
701 查看
1.basic concepts
malloc&free and new&delete are all used to request memory allocation and release memory allocated from heap area.
eg.
malloc.h must be referenced when using malloc&free
i)
int *array;
array=(int *)malloc(4*sizeof(int));// declare an array which has four int elements
....
free(array);//release the memory which is allocated to array.
ii)
int *array
array=new int[4];
...
delete[] array;
all these two cases works the same. but what is their differs then? are they the same functioanlity? NO!
2.diffs.
we can only say that in some cases, they work the same.that is to say, we can use malloc&free to take place of new&delete. but this can not applied all the time.
the main reason is :malloc will not invoke class construtor in C++, while new do. At the same time, free will not invoke destructor,while delete do.
eg.
class TDate
{
public:
TDate()
{
month=1;
day=1;
year=1;
printf("constructor is invoked");
}
~TDate()
{
printf("destructor is invoked");
}
protected:
int month;
int day;
int year;
}
case 1: use Malloc
TDate *p;
p=(TDate *)malloc(sizeof(TDate));//do not invoke constructor at all
...
free(p);//do not invoke destructor at all
in this case, the constructor of TDate will not be invoked. so all its memebers can not be initialized.
case 2: use new
TDate *p;
p=new Tdate;//invoke constructor to initialize the instance
...
delete p;//invoke destructor to release resource.
malloc&free and new&delete are all used to request memory allocation and release memory allocated from heap area.
eg.
malloc.h must be referenced when using malloc&free
i)
int *array;
array=(int *)malloc(4*sizeof(int));// declare an array which has four int elements
....
free(array);//release the memory which is allocated to array.
ii)
int *array
array=new int[4];
...
delete[] array;
all these two cases works the same. but what is their differs then? are they the same functioanlity? NO!
2.diffs.
we can only say that in some cases, they work the same.that is to say, we can use malloc&free to take place of new&delete. but this can not applied all the time.
the main reason is :malloc will not invoke class construtor in C++, while new do. At the same time, free will not invoke destructor,while delete do.
eg.
class TDate
{
public:
TDate()
{
month=1;
day=1;
year=1;
printf("constructor is invoked");
}
~TDate()
{
printf("destructor is invoked");
}
protected:
int month;
int day;
int year;
}
case 1: use Malloc
TDate *p;
p=(TDate *)malloc(sizeof(TDate));//do not invoke constructor at all
...
free(p);//do not invoke destructor at all
in this case, the constructor of TDate will not be invoked. so all its memebers can not be initialized.
case 2: use new
TDate *p;
p=new Tdate;//invoke constructor to initialize the instance
...
delete p;//invoke destructor to release resource.
相关文章推荐
- C++ Memory Management : What is the difference between malloc/free and new/delete?
- The differences between new(delete) and malloc(free) in c/c++
- What is the difference between new/delete and malloc/free?
- Difference between new (delete) and malloc (free)
- Effective C++(Item3) Prefer new and delete to malloc and free
- malloc/free and new/delete in C++
- malloc/free and new/delete in C++
- malloc/free and new/delete in C++
- 浅谈malloc-free and new-delete
- 关于new/delete and malloc/free
- What is the difference between new and malloc()?
- The difference between "malloc" and "new"
- malloc/free and new/delete 工作机制
- Java equivalents of malloc(), new, free() and delete
- malloc, free, new and delete
- 内存分配方式,堆区,栈区,new/delete/malloc/free
- malloc/free与new/delete的区别
- malloc/free 和 new/delete
- C/C++ 内存分配方式,堆区,栈区,new/delete/malloc/free
- 学习之第一周小结 malloc、calloc、realloc、free、new、delete函数应用