最近看到一段代码,如下:Buffers      *pBuffers = 0;
int     lSize, i, iListIndex;
void         *pMalloc;// Calculate the buffer size taking into acoount the system
// buffer overhead and then make sure the buffers always
// start on a word boundry.sizeIn = 16;
lSize = sizeIn;
lSize += (lSize % sizeof(int));
iListIndex = 0;
  
pMalloc = malloc(sizeIn + sizeof(class Buffers));
pBuffers = new (pMalloc) Buffers (iListIndex);其中Buffers是一个类,没有父类。有一个构造函数Buffers(int iListID),这些都还明白。
但是最后一句new后边的(pMalloc)是怎么出来的就不明白了,有这样的语法吗?
源程序很大,可以在vc中编译执行。可是我没有见过new还能这样用的,请高手详解~~~

解决方案 »

  1.   

    这就表示pBuffers申请成功后得到的地址的初始位置在pMalloc,作用就是在一个预定的内存位置申请空间。
      

  2.   

    pMalloc = malloc(sizeIn + sizeof(class Buffers));
    pBuffers = new (pMalloc) Buffers (iListIndex);
    ===
    运行后pMalloc,pBuffers在数值上是相同的,只是类型不一样。pMalloc是VOID* ,而pBuffers是Buffers* 。使用的时候要注意能够保证pMalloc指向的存储器足够大到能装下Buffers。
      

  3.   

    new可能已经被重载,并且在重载中new操作可以带两个参数,其中pMalloc可能是传入的第二个参数。
      

  4.   

    明白了~
    to:laiyiling(陌生人 求职中)
    我用bounds checker检测,总是说这段代码有memory leak。是不是误报?
      

  5.   

    而这里用的new是传说中placement new
    这样允许你自己分配内存构造对象!而不是由new operator来分配内存然后调用类型的构造函数!
      

  6.   

    new是一个操作符,应该没有这种语法,除非被重载,但是好象new是不可以被重载的
      

  7.   

    注意,这里的new operator不是operator new,而是c++语言的new关键词!!!
    重载new操作符就是operator new,通过operator new你可以自己决定内存分配方式,是由new来调用的!
      

  8.   

    没听说过也没见过这种语法呵呵不过laying的话好像很有道理啊
      

  9.   

    推荐一本书《C++ FAQS》Sencond edition   Marshall Cline 等著,这本书讲了不少的东西,我也是前几天在图书馆里翻到的。
    还有一个new的用发就是:Class *A = new(nothrow) A(); //这样就能防止返回抛出异常
      

  10.   

    to:laiyiling(陌生人 求职中)
    多谢大牛指点!我在msdn中查到了nothrow这种用法。
    程序最后释放掉了pBuffers,但是没有处理pMalloc,指向同一内存释放一个就够了吧?
    是不是因为这个原因造成了bounds checker提示memory leak呢?
    另外,我使用向导建立了一个Console程序(简单的"hello,world!"程序)然后修改了代码想试一下
    但是总提示我:
    error C2661: 'new' : no overloaded function takes 2 parameters
    是不是需要什么设置?或者我少加了什么头文件?试验程序如下:
    #include "stdafx.h"
    #include "malloc.h"class Buffers
    {
    public:
    Buffers(int a) { m_a = a; };
    ~Buffers();
    private:
    int m_a;
    };int main(int argc, char* argv[])
    {
        void *pMalloc;
        Buffers *pBuffer;    pMalloc = malloc(16+sizeof(class Buffers));
        pBuffer = new (pMalloc) Buffers(1);
        printf("pMalloc = %p\n", pMalloc);
        printf("pBuffer = %p\n", pBuffer);    delete [] pBuffer;    return 0;
    }
      

  11.   

    pBuffers = new (pMalloc) Buffers (iListIndex);
    这个不奇怪啊
    new() 是对pMalloc对象进行创建
    malloc(sizeIn + sizeof(class Buffers))
    这个方法是对他分配空间,不过没有具体实现
    Buffers()
    是Buffers类的构造函数
    为指针赋值
    但是这种写法很难懂
    本人是这样认为的!!!
      

  12.   

    带参的new吧,这种写法在symbian里面很常见,甚至都抛弃了一般的new写法,原因是防止失败.
      

  13.   

    算偶孤陋寡闻了,
    More Effection C++ 里第8条里有这个的介绍
    http://www.csdn.com.cn/program/6728.htm
      

  14.   

    to:codewarrior(会思考的草)
    你提到的防止失败指的是new (nothrow)...这种格式吧?
      

  15.   

    If you use the placement new form of the new operator, the form with arguments in addition to the size of the allocation, the compiler does not support a placement form of the delete operator if the constructor throws an exception. For example:// expre_new_Operator2.cpp
    // C2660 expected
    class A {
    public:
        A(int) { throw "Fail!"; }
    };
    void F(void)
    {
        try {
            // heap memory pointed to by pa1 will be deallocated
            // by calling ::operator delete(void*).
            A* pa1 = new A(10);
        } catch (...) {
        }
        try {
            // This will call ::operator new(size_t, char*, int).
            // When A::A(int) does a throw, we should call
            // ::operator delete(void*, char*, int) to deallocate
            // the memory pointed to by pa2.  Since
            // ::operator delete(void*, char*, int) has not been implemented,
            // memory will be leaked when the deallocation cannot occur.        A* pa2 = new(__FILE__, __LINE__) A(20);
        } catch (...) {
        }
    }
      

  16.   

    《C++ Primer(第三版)》 347页 8.4.5 节 : 定位 new 表达式。