读书人

为啥可以重载全局的new 和delete 函数

发布时间: 2012-08-01 17:53:40 作者: rapoo

为什么可以重载全局的new 和delete 函数?
#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void* operator new (size_t sz)
{
printf("new: %d bytes\n", sz);
void* m = malloc(sz);
if (!m)
puts("out of memory.\n");
return m;
}

void operator delete (void* p)
{
printf("delete: \n");
free(p);
return;
}

int main()
{
int *p = new int;
delete p;
getch();
return 0;
}

输入结果是:
new: 4 bytes
delete:

编译器vs2008

全局的new和delete函数,在全局域中定义,而这里又可以重新定义,为什么没有发生重定义呢?
没搞懂函数重载与域的关系,new和delete在难道不是在全局中?不明白了。

请知道的详细解答下,非常感谢(就100分相送作为微薄的回报吧)。

[解决办法]
全局的new和delete函数是可以重载的,这样可以用来实现内存检测的功能
http://blog.csdn.net/hzyong_c/archive/2010/10/18/5949314.aspx
[解决办法]
new delete 可以重载的为了适应不同的需要
[解决办法]
它们是操作符,操作符重载
[解决办法]
LZ不太理解重载的含义,重载和重定义差不多,只要它的参数不都一样就能重载,否则就是重定义,重定义当然编译通不过。重载new操作符没有太大的意议,一般不建议重载new。LZ说的域,其实没多大关系,这里的都是全局的。一般在一个作用域中,只对范围小的可见,若使用全局的,需要加作用域,如::tmp和std::tmp等 。回答完毕。
[解决办法]
编译器的处理!!
[解决办法]
new和delete不是函数,而是操作符,和+-*/一样


[解决办法]

探讨
new和delete不是函数,而是操作符,和+-*/一样

[解决办法]
同。。。
[解决办法]
lz重载的是operator new ,不是new~~~
[解决办法]
只能说这个是特殊情况了。默认全局的被你写的取代了。
[解决办法]
楼主说重载也好,说重定义也好,针对的都是全局域的operator new/operator delete函数(而不是new/delete操作符);
new/delete操作符是不能被重载的,它们分别会调用对应作用域内的operator new/operator delete函数;
在C++中,这两个函数是运行时库提供的(而不是语言本身),因此在程序中如果重写了全局域的operator new/operator delete函数,那么编译器将会用程序员重写的这一对函数替换运行时库中的实现!
[解决办法]
比较特殊,是一个替换.
[解决办法]
情况特殊,编译器做些处理也是很正常的。
[解决办法]
ISO C++03:
3.7.3 Dynamic storage duration

2 The library provides default definitions for the global allocation and deallocation functions. Some global
allocation and deallocation functions are replaceable (18.4.1). A C + + program shall provide at most one
definition of a replaceable allocation or deallocation function. Any such function definition replaces the
default version provided in the library (17.4.3.4). The following allocation and deallocation functions
(18.4) are implicitly declared in global scope in each translation unit of a program
void* operator new(std::size_t) throw(std::bad_alloc);
void* operator new[](std::size_t) throw(std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();
These implicit declarations introduce only the function names operator new, operator new[],
operator delete, operator delete[]. [Note: the implicit declarations do not introduce the


names std, std::bad_alloc, and std::size_t, or any other names that the library uses to declare
these names. Thus, a new-expression, delete-expression or function call that refers to one of these functions
without including the header <new> is well-formed. However, referring to std, std::bad_alloc, and
std::size_t is ill-formed unless the name has been declared by including the appropriate header. ]
Allocation and/or deallocation functions can also be declared and defined for any class (12.5).
[解决办法]

探讨
new和delete不是函数,而是操作符,和+-*/一样

[解决办法]
探讨

ISO C++03:
3.7.3 Dynamic storage duration

2 The library provides default definitions for the global allocation and deallocation functions. Some global
allocation and deallocation functions are ……

[解决办法]
c++里面除了:: . .* sizeof typeid不能重载外,其余的运算符都可以重载,比如:new new[] delete delete[]均可以重载。之所以重载也是为了提高c++语言的灵活性!可以根据程序员的需求让运算符有符合自己需求的运算法则,比较常见的就是+ - * /这些运算符,既可以用于整形又可以用于浮点型等等,因为编译器已经给你提供好重载了!
[解决办法]
未写前是隐藏的,写后覆盖了隐藏的。属于显式定义
或可以理解成,以参数对应为准
说它像- + 符号
但内置类型的重载操作符不能再重新定义,而delete和 new可以。

读书人网 >C++

热点推荐