简体   繁体   English

C ++:程序因[memcpy]的核心转储而崩溃

[英]C++: Program crashes with core dump at [memcpy]

I'm working on Solaris 5.8, C++, using the Json parser. 我正在使用Json解析器在Solaris 5.8,C ++上工作。

The problem is: while parsing a file of size greater than 700 MB, the process crashes with core dump error. 问题是:解析大小大于700 MB的文件时,该过程因核心转储错误而崩溃。 It roughly occurs at below code point - 它大致发生在以下代码点-

int printbuf_memappend(struct printbuf *p, char *buf, int size)
{
    char *t;
    if(p->size - p->bpos <= size)
    {
        int new_size = json_max(p->size * 2, p->bpos + size + 8);
        if (!(t = realloc(p->buf, new_size)))
            return -1;
        p->size = new_size;
        p->buf = t;
    }
    memcpy(p->buf + p->bpos, buf, size); // CORE DUMP HERE
    p->bpos += size;
    p->buf[p->bpos]= '\0';
    return size;
}

Could you please help to identify the problem? 您能帮忙找出问题所在吗? The core dump file contain only the data being copied. 核心转储文件仅包含要复制的数据。 Can increase of RAM be a solution ? 可以增加RAM吗? Or do I need to limit the file size to 700MB ? 还是我需要将文件大小限制为700MB?

If crash happened in memcpy, you have two variants something wrong with input or output. 如果崩溃的memcpy发生的事情,你有两个变种什么毛病输入或输出。

To test the second variant add memset after realloc: 要测试第二个变体,请在realloc之后添加memset:

    int new_size = json_max(p->size * 2, p->bpos + size + 8);
    if (!(t = realloc(p->buf, new_size)))
        return -1;
    p->size = new_size;
    p->buf = t;
    memset(p->buf + p->bpos, 0, size);

On Linux (depend on configuration) possible to allocate not existing virtual memory. 在Linux上(取决于配置),可以分配不存在的虚拟内存。 The real allocation happens after the first usage. 真正的分配发生在第一次使用之后。 May the same happens on your Solaris? 您的Solaris上可能会发生同样的情况吗? relloc return ok, but system really have no enought memory? relloc返回确定,但是系统确实没有足够的内存吗? memset should give answer to this question. memset应该给出这个问题的答案。

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

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