编译c++代码时出现段错误

segmentation fault when compling c++ code

我现在正在为我的代码中的这个问题苦苦挣扎,代码可以成功编译但是当我 运行 二进制文件时,会发生分段错误,问题如下:

Program received signal SIGSEGV, Segmentation fault. _int_malloc (av=av@entry=0x7ffff6adfb20 <main_arena>, bytes=bytes@entry=15859713) at malloc.c:3802     malloc.c: No such file or directory.

环境:ubuntu 16.04 虚拟机工作站 com: g++, version:5.4.0 C++:C++11 库:imebra 5.0.1 这是我的代码:

#include <imebra/imebra.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>

#define img_height 2816
#define img_width  2816
#define img_bit    2
#define img_size img_height*img_width*img_bit //15.1MB
using namespace std;

//MONOCHROME1: indicates that the greyscale ranges from bright to dark with ascending pixel values
//MONOCHROME2: indicates that the greyscale ranges from dark to bright with ascending pixel values
/*
create an Image object
fill the image object with raw data
create a DICOM dataset
add the image to the DICOM dataset
fill all the necessary DICOM tags (e.g. sop class, instance, patient name, etc)
save the DICOM dataset
*/

int main()
{
    //ifstream mydata("/home/lixingyu/GH1.raw",ios::binary);
    //uint32_t *pImgData = (uint32_t *)malloc(img_size*sizeof(uint32_t));
    //mydata.read(pImgData,img_size);   

    FILE *fp = NULL;
    fp = fopen("/home/lixingyu/123.raw","rb");
    uint32_t *pImgData = new (std::nothrow) uint32_t (img_size);
    fread(pImgData,sizeof(uint32_t),img_size,fp);
    cout<<"success"<<endl;
/*---------program stop here -------*/

    // Creat an image 500 pixels wide , 400 pixels height
    // each sample is a 16 bit unsigned value, the colorspace
    // is monochrome_2, the higher bit used is 15
    // imebra ::MutableImage image(500,400,imebra::bitDepth_t::depthU16,"MONOCHROME_2",15);

    imebra ::MutableImage image(img_height,img_width,imebra::bitDepth_t::depthU16,"MONOCHROME2",15);

        // 1. Fill the image with data
        // We use a writing data handler to write into the image.
        // The data is committed into the image only when the writing
        // data handler goes out of scope.

        imebra::WritingDataHandlerNumeric writeIntoImage(image.getWritingDataHandler());

        for (size_t y=0;y!=img_width;++y)
        {
            for (size_t x=0; x!= img_height; ++x)
            {
                writeIntoImage.setUnsignedLong(y*img_height+x,pImgData[y*img_height+x]);
            }
        }
    // specify the tansfer syntax and the charset

    imebra::charsetsList_t charsets;

    charsets.push_back("ISO 2022 IR 6");

    //Explicit VR little endian

    imebra::MutableDataSet dataSet("1.2.840.10008.1.2.1",charsets);

    // add the image to the dataSet
    dataSet.setImage(0,image,imebra::imageQuality_t::veryHigh);

    // set the patient name
dataSet.setUnicodePatientName(imebra::TagId(imebra::tagId_t::PatientName_0010_0010),imebra::UnicodePatientName(L"fjx",L"",L""));

    // save to a file
    imebra::CodecFactory::save(dataSet,"GH1.dcm",imebra::codecType_t::dicom);
    free(pImgData);
}

当我使用gdb 调试我的代码时,问题出现了,我将堆栈大小更改为100MB,但随后会出现分段错误。 也许动态内存申请有问题?? 谁能帮帮我? 仅供参考,imebra::XXX 的函数均来自 imebra lib。

您不能在 new 分配的内存上调用 free。这会导致未定义的行为。您 必须 调用 delete

您也只分配 一个 uint32_t(并用值 img_size 初始化它),而不是 img_size 个数组.为此你需要 new (std::nothrow) uint32_t[img_size]; 代替(后来 delete[] 而不是 delete)。所以你要用 fread.

写越界

您还需要检查 new(std::nothrow) 的 return 值是否不是空指针,这会在分配失败时发生。如果您使用投掷版本,则不需要该检查。

请不要像这样使用 new,而是使用 std::vectormalloc 在 C++ 中甚至比 new 更糟糕。

同样,不要在 C++ 中使用 C IO 库。请改用 std::ifstream