我正在检测一些代码并注意到 C++14 特性有两个新的 delete 运算符(来自 http://en.cppreference.com/w/cpp/memory/new/operator_delete ):
These are 5-6) Called instead of (1-2) if a user-defined replacement is provided except that it's implementation-defined whether (1-2) or (5-6) is called when deleting objects of incomplete type and arrays of non-class and trivially-destructible class types (since C++17). The standard library implementations are identical to (1-2).
我已经重载了这些并且想专门调用这两个。当我用 gcc 重载这两个时,我没有问题。使用 clang++,我得到了对 operator delete(void*)
这是代码
void* operator new(long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}
void* operator new[](long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}
void operator delete(void* what, long unsigned int howmuch) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
if(howmuch != 1) __builtin_trap();
}
extern "C"
void _start() {
delete new char;
asm("syscall" : : "a"(60) : );
}
用gcc编译:g++ -ggdb -std=c++14 -nostdlib -fno-builtin -fno-exceptions 1.cc 没问题,运行正常。
是否可以用 llvm/clang 做到这一点?
最佳答案
您可以像这样显式调用大小或非大小删除运算符:
char* ptr = new char;
delete ptr; // compiler selects which to call
operator delete(ptr); // explicitly call the non-sized delete
operator delete(ptr, 1); // explicitly call sized delete
完整示例:
void* operator new(long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}
void* operator new[](long unsigned int howMuch) {
return reinterpret_cast<void*>(0xdeadbeef);
}
void operator delete(void* what) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
}
void operator delete(void* what, long unsigned int howmuch) {
if(what != reinterpret_cast<void*>(0xdeadbeef)) __builtin_trap();
if(howmuch != 1) __builtin_trap();
}
extern "C"
void _start() {
char* ptr = new char;
delete ptr;
operator delete(ptr);
operator delete(ptr, 1);
asm("syscall" : : "a"(60) : );
}
编译并查看生成的代码,很清楚在以下情况下调用了哪些运算符:
$ clang++ -std=c++14 -nostdlib -fno-builtin -fno-exceptions -fsized-deallocation sized-deallocation.cpp -o sized-deallocation.bin && gdb -batch -ex 'file sized-deallocation.bin' -ex 'disassemble _start' | c++filt
Dump of assembler code for function _start:
0x0000000000401070 <+0>: push %rbp
0x0000000000401071 <+1>: mov %rsp,%rbp
0x0000000000401074 <+4>: sub $0x10,%rsp
0x0000000000401078 <+8>: mov $0x1,%eax
0x000000000040107d <+13>: mov %eax,%edi
0x000000000040107f <+15>: callq 0x401000 <operator new(unsigned long)>
0x0000000000401084 <+20>: mov %rax,-0x8(%rbp)
0x0000000000401088 <+24>: mov -0x8(%rbp),%rax
0x000000000040108c <+28>: cmp $0x0,%rax
0x0000000000401090 <+32>: mov %rax,-0x10(%rbp)
0x0000000000401094 <+36>: je 0x4010aa <_start+58>
0x000000000040109a <+42>: mov $0x1,%eax
0x000000000040109f <+47>: mov %eax,%esi
0x00000000004010a1 <+49>: mov -0x10(%rbp),%rdi
0x00000000004010a5 <+53>: callq 0x401040 <operator delete(void*, unsigned long)>
0x00000000004010aa <+58>: mov -0x8(%rbp),%rdi
0x00000000004010ae <+62>: callq 0x401020 <operator delete(void*)>
0x00000000004010b3 <+67>: mov $0x1,%eax
0x00000000004010b8 <+72>: mov %eax,%esi
0x00000000004010ba <+74>: mov -0x8(%rbp),%rdi
0x00000000004010be <+78>: callq 0x401040 <operator delete(void*, unsigned long)>
0x00000000004010c3 <+83>: mov $0x3c,%eax
0x00000000004010c8 <+88>: syscall
0x00000000004010ca <+90>: add $0x10,%rsp
0x00000000004010ce <+94>: pop %rbp
0x00000000004010cf <+95>: retq
End of assembler dump.
虽然您使用 Clang 得到 undefined reference to `operator delete(void*)' 的实际原因是(如 @T.C. 所说)Clang 需要 -fsized-deallocation 标志以启用 C++14 sized deallocation .
如果使用以下命令,您的示例编译没有错误:
clang++ -ggdb -std=c++14 -nostdlib -fno-builtin -fno-exceptions -fsized-deallocation 1.cc
由于 Clang 3.7 C++14 大小的释放在默认情况下是禁用的:
C++ Support in Clang > C++14 implementation status > C++ Sized Deallocation N3778
(7): In Clang 3.7 and later, sized deallocation is only enabled if the user passes the
-fsized-deallocationflag. The user must supply definitions of the sized deallocation functions, either by providing them explicitly or by using a C++ standard library that does.libstdc++added these functions in version 5.0, andlibc++added them in version 3.7.
Clang 3.7 Release Notes > What’s New in Clang 3.7? > New Compiler Flags
The sized deallocation feature of C++14 is now controlled by the
-fsized-deallocationflag. This feature relies on library support that isn’t yet widely deployed, so the user must supply an extra flag to get the extra functionality.
此更改的原因是当时(2015-03-19)广泛部署的标准库中缺少这些功能:
C++14: Disable sized deallocation by default due to ABI breakage
There are no widely deployed standard libraries providing sized deallocation functions, so we have to punt and ask the user if they want us to use sized deallocation. In the future, when such libraries are deployed, we can teach the driver to detect them and enable this feature.
可在此处找到此选项的手动条目:
Clang command line argument reference > Compilation flags > Target-independent compilation options
-fsized-deallocation,-fno-sized-deallocationEnable C++14 sized global deallocation functions
关于c++ - 在 C++ 中是否可以指定使用哪个删除运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35077781/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h