惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
B
Blog
Cisco Talos Blog
Cisco Talos Blog
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
V
Visual Studio Blog
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
S
Security @ Cisco Blogs
博客园 - 聂微东
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
量子位
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tenable Blog
月光博客
月光博客
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
D
Docker
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Help Net Security
Help Net Security
D
DataBreaches.Net

博客园 - 悉野

ssh连结vmware中ubuntu的共享文件夹 spring加载冲突问题 spring一个错误修正 抓安卓日记到文件 注册表删除桌面顽固图标 github买的账号无法拉取代码的解决方法 apk常用命令 java服务器异常处理 ES6 学习难度分层 promise原理 材质, 纹理, shader之间的关系 TCP与UDP区别 javascript中=>与function的区别 javascript中函数解析过程 cocos使用fgui 几种服务注册与发现的区别 mybatis测试 向量点乘与叉乘 unity画布3种渲染模式 go学习笔记10(HTTP) go学习笔记9(TCP) go学习笔记8(反射) c++简单的线程池 C++的定位放置new(Placement new) go学习笔记7(泛型,文件读写,测试) go学习笔记6(协程与channel,select使用,线程安全,异常处理) go学习笔记5(函数,结构体,自定义类型和类别名,接口) go学习笔记4(数组与切片,map,if,switch,for循环) go学习笔记3(变量定义,输入输出,基本数据类型) go学习笔记2 laya打包的apk版本更新脚本 laya列表滚动问题 laya踩坑记 laya列表时计算具体体子项是列表中第几个 子弹射击 laya给自己画边框 查看laya已经加载的资源 laya spine事件
operator new 是 C++ 动态内存分配的核心函数,负责分配原始内存,不调用构造函数
悉野 · 2026-04-12 · via 博客园 - 悉野
// 全局重载示例
void* operator new(std::size_t size) {
    std::cout << "Custom operator new called, size: " << size << std::endl;
    return malloc(size); // 调用 malloc 分配内存
}

// 类专属重载示例
class MyClass {
public:
    static void* operator new(std::size_t size) {
        std::cout << "MyClass::operator new called" << std::endl;
        return ::operator new(size); // 调用全局 operator new
    }
};

3223

#include <iostream>
#include <new> // for std::bad_alloc

class MyClass {
public:
    MyClass() { std::cout << "Constructor called" << std::endl; }
    ~MyClass() { std::cout << "Destructor called" << std::endl; }

    // 重载全局 operator new
    static void* operator new(std::size_t size) {
        std::cout << "Global operator new called, size: " << size << std::endl;
        void* ptr = malloc(size);
        if (!ptr) throw std::bad_alloc();
        return ptr;
    }

    // 重载全局 operator delete
    static void operator delete(void* ptr) noexcept {
        std::cout << "Global operator delete called" << std::endl;
        free(ptr);
    }
};

int main() {
    // 使用 new 操作符(调用 operator new 和构造函数)
    MyClass* obj = new MyClass();

    // 使用定位分配(在栈内存上构造对象)
    char buffer[sizeof(MyClass)];
    MyClass* placementObj = new (buffer) MyClass();

    // 释放资源
    delete obj;               // 调用析构函数和 operator delete
    placementObj->~MyClass(); // 手动调用析构函数(定位分配需手动析构)

    return 0;
}