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

推荐订阅源

Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
AI
AI
Security Latest
Security Latest
GbyAI
GbyAI
P
Proofpoint News Feed
Y
Y Combinator Blog
A
Arctic Wolf
G
Google Developers Blog
U
Unit 42
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
WordPress大学
WordPress大学
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
Project Zero
Project Zero
博客园_首页

博客园 - 悉野

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;
}