繁体   English   中英

如果传递的指针为null,placement new会调用构造函数吗?

[英]Does placement new call the constructor if the passed pointer is null?

我试图将vc7.1项目转换为从codeproject获得的vs2010。(这是链接ht p://www.codeproject.com/KB/cpp/transactions.aspx?fid = 11253&df = 90&mpp = 50&noise = 3&sort = Position&view = Expanded&fr = 1#xx0xx

但是经过转换和修改后其配置。

我发现调试失败,它在DrawIt.exe中的0x0028e7b9上显示未处理的异常:0xC0000005:访问冲突写入位置0x00000000。

错误行是这样的

data = new(Mm::Allocate(sizeof(DocData), sid)) DocData();

和功能

void* Allocate(size_t size, SPACEID sid)
{
    AUDIT

    Spaces::iterator s = spaces.find(sid);
    if (s == spaces.end())
        return NULL;

    Space& space = s->second;
    if (!space.transacting) 
        return NULL;

    size = max(size, sizeof(Free));

    // TODO: assert that "data" is allocated in space
    space.AssertData();

    // are there any more free chunks?
    if (!space.data->sFreeHead) {
        space.data->Insert(space.More(size));
    }

    AUDIT

    // find the first chunk at least the size requested
    Free* prev = 0;
    Free* f = space.data->sFreeHead;
    while (f && (f->size < size)) {
        prev = f;
        f = f->next;
    }

    AUDIT
    // if we found one, disconnect it
    if (f) {
        space.data->locTree.remove((size_t)f);

        if (prev) prev->next = f->next;
        else space.data->sFreeHead = f->next;

        f->next = 0;
        memset(&f->loc, 0, sizeof(f->loc));
    } else {
        f = space.More(size);
    }

    // f is disconnected from the free list at this point

    AUDIT

    // if the free chunk is too(?) big, carve a peice off and return
    // the rest to the free list
    if (f->size > (2*(size + sizeof(Free)))) {
        Free* tmp = space.data->Slice(f, size); // slice size byte off 'f'
        space.data->Insert(f); // return the remainder to the free list
        f = tmp;
    }

    AUDIT

    CHECK_POINTER(f)

    void* p = reinterpret_cast<void*>((char*)f + sizeof(Free::SIZE_TYPE));

    CHECK_POINTER(p)

    return p;
}

有人知道吗,老兄?

由于我不擅长C ++,因此需要一段时间才能弄清楚如何解决此问题。 刚刚上传了源代码的源文件 ,如果有人可以帮助,将不胜感激。

[此答案可能是错误的; 查看评论以进行讨论; 我暂时不删除此内容,以便我们找出答案是什么]

在几种失败的情况下, Allocate返回NULL

在使用之前,您无需检查调用“ Allocate的结果。

您需要检查结果。 或者,您可以在失败时引发异常。

好吧,您的Allocate函数显然返回NULL 我们很难说出位置和原因,而您自己设置断点并逐步遍历分配器很简单,因此我建议您这样做并找出函数返回的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM