_DEBUG模式是否具有使“就地"重载的宏?新运算符的语法? [英] Does _DEBUG mode have a macro that overloads the "in place" syntax of the new operator?

查看:63
本文介绍了_DEBUG模式是否具有使“就地"重载的宏?新运算符的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能第一个注意到这一点,或者也许只是我一个人,但是我正在尝试对正在编写的数据结构类的新运算符使用就地语法在_DEBUG模式下,编译器以一些无法识别的语法出现退出.

我假设在_DEBUG模式下重载new的宏不包含用于处理就地语法的版本,但是对Code Project和整个Web的审阅都没有揭示该子项目的任何信息.关于这个故事有什么想法吗?我通过将有问题的代码包装在两个#ifdef _DEBUG块之间来解决此问题,就像这样...

I can''t be the first to notice this, or maybe it IS just me, but I was trying to use the in place syntax for the new operator for a data structure class I was writing, and when I ran it in _DEBUG mode, the compile exited with some verbiage about unrecognized syntax.

I assume that the macro that overloads new in _DEBUG mode doesn''t include a version to handle the in-place syntax, but a review of both Code Project and the web at large didn''t reveal any information on the subjet. Any ideas as to what the story is? I worked around this by wrapping the offending code between two #ifdef _DEBUG blocks like so ...

CObject* pPtr;

#ifdef _DEBUG
#pragma push_macro("new")
#undef new
#endif

new (pPtr) CObject;

#ifdef _DEBUT
pragma pop_macro("new")
#endif



...但是我想知道这是否是preprocessir的失败,或者是我缺少的东西.



... but I would like to know if this is a failing of the preprocessir, or something I''m missing.

推荐答案

预处理程序,这是调试实现的一个众所周知的限制.预处理程序执行文本替换,以便预处理程序执行以下操作:

It is not really failing in the preprocessor, it is a fairly well known limitation of the debug implementation. The preprocessor does textual substitution, so that the preprocessor does something like:

#ifdef _DEBUG
#define new _debugnew(__FILE__, __LINE__)
#endif



然后,预处理器将在代码中愉快地将new的所有实例替换为_debugnew(__FILE__, __LINE__).该实现需要提供_debugnew的实现,例如:



The preprocessor will then happily replace all instances of new with _debugnew(__FILE__, __LINE__) in your code. The implementation needs to provide an implementation of _debugnew, for instance:

void * operator new (size_t size, char const * file, int line)
{
  logallocation(size, filem line);
  return ::new char [size];
}



正如您所发现的那样,这对于C ++内存分配的大多数使用都相当有效,即,对于global new的使用,但不适用于所有这些.

除了解决这个问题的方法外,您还可以提供自己的debug new运算符实现,该实现将在调试模式下调用,例如



This works fairly well for the majority of uses of C++ memory allocation, i.e. for use of global new but does not work for all of them, as you have found.

As well as the way you got round this, you could possibly provide your own implementation of the debug new operator, that would get called in debug mode, e.g.

class MyClass
{
public:
  void* operator new(size_t size)
  {
    return newImplementation(size);
  }

  void* operator _debugnew(size_t size, char const * file, int line)
  {
    return newImplementation(size);
  }

private:
  void* newImplementation(size_t size)
  {
    // your fancy implementation here
  }
};



请注意,替换new的内容取决于您的编译器-您将需要深入研究系统文件来找出调试模式中实际使用的内容.



Note, that what is replaces new depends on your compiler - you will need to dig into the system files to find out what is actually used in debug mode.


我个人不会如果我是不确定的,请不要取消定义new的调试版本,这是一个非常有用的工具,为什么不只是稍微更改一下语法以使其可以使用呢?就像graham所说的那样,宏并不是完美的,但是它们确实可以完成工作.
personally, i wouldn''t undefine the debug version of new if i were you, its a very helpful tool, why not just change your syntax up a bit so that its ok with it? like graham said, macros aren''t perfect but they do get the job done.


神秘的新运算符.运算符new不是宏.您可以使用自己的内存管理功能使全局new运算符超载.因此,您应该始终删除在相同上下文中由new创建的类.
重载new的示例:
The mysterious new operator. The operator new isnt a macro. You can overload the global new operator with your own memory management function. Therefore you should ALWAYS delete a class created by new in the same context.
Example to overload new:
#define	USE_NEW	3
#if(0==USE_NEW)
	// default behaviour
#elif(1==USE_NEW)
void* operator new( size_t cb )
{
  void* res = HeapAlloc(GetProcessHeap(),0,cb);
  return res;
}
void operator delete( void* p)
{
  if(p) HeapFree(GetProcessHeap(),0,p);
}
#elif(2==USE_NEW)
#include <objbase.h>
#pragma comment (lib,"ole32.lib")
void* operator new( size_t cb )
{
  void* res = CoTaskMemAlloc(cb);
  return res;
}
void operator delete( void* p)
{
  if(p) CoTaskMemFree(p);
}
#elif(3==USE_NEW)
void* operator new( size_t cb )
{
  void* res = GlobalAlloc(GMEM_FIXED,cb);
  return res;
}
void operator delete( void* p)
{
  if(p) GlobalFree((HGLOBAL)p);
}
#endif</objbase.h>


问候.


这篇关于_DEBUG模式是否具有使“就地"重载的宏?新运算符的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆