我全局替换了new / delete以确保我的对象正确对齐以进行快速计算:
#ifdef __WAND__
target[name[new.o] type[object] platform[;GNU/Linux]]
#endif
#include <cstddef>
#include <stdlib.h>
#include <cstdio>
void* operator new(size_t count)
{
void* buffer;
int res=posix_memalign(&buffer,64,count);
if(res==0)
{
printf("Allocated %zu bytes at %p\n",count,buffer);
return buffer;
}
return NULL;
}
void operator delete(void* buffer)
{
printf("Free buffer %p\n",buffer);
free(buffer);
}
void* operator new[](size_t count)
{
void* buffer;
int res=posix_memalign(&buffer,64,count);
if(res==0)
{
printf("Allocated %zu bytes at %p\n",count,buffer);
return buffer;
}
return NULL;
}
void operator delete[](void* buffer)
{
printf("Free buffer %p\n",buffer);
free(buffer);
}
在stdout
获得比预期更多的内容后,我检查了谁调用了我的new
运算符。这是回溯的样子:
#0 operator new (count=4) at new.cpp:13
#1 0xb753a87d in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#2 0xb7420e96 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#3 0xb7420f31 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#4 0xb73cb710 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#5 0xb7422dc5 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#6 0xb7422ec4 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#7 0xb7467da0 in ?? () from /usr/lib/i386-linux-gnu/dri/i915_dri.so
#8 0xb7ed02cc in fgDisplayMenu () from /usr/lib/i386-linux-gnu/libglut.so.3
#9 0xb7eced1c in ?? () from /usr/lib/i386-linux-gnu/libglut.so.3
#10 0xb7ed285f in fgEnumWindows () from /usr/lib/i386-linux-gnu/libglut.so.3
#11 0xb7ecf26e in glutMainLoopEvent () from /usr/lib/i386-linux-gnu/libglut.so.3
#12 0xb7ecfabc in glutMainLoop () from /usr/lib/i386-linux-gnu/libglut.so.3
#13 0x0804acaf in GlutWindow::mainLoopRun () at glutwindow.cpp:28
#14 0x080496db in main (argc=1, argv=0xbfffeb34) at main.cpp:28
那么为什么低级图形库会调用我的new
运算符?是否有任何其他原因(一些黑魔法)比用C ++编写的这个库(我认为大多数这样的东西是用C或汇编编写的)使用new / delete而不是malloc / free?
答案 0 :(得分:1)
低级库可能部分或完全写入C++
。没有其他原因(黑魔法)来调用新的操作符。