我现在正在努力从我的代码中解决这个问题,代码可以成功编译,但是当我运行二进制文件时,会出现分段错误,这是问题所在:
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.
Env:ubuntu 16.04 VM工作站 Com:g ++,版本: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。
答案 0 :(得分:4)
不允许您在this
分配的内存上调用free
。这会导致不确定的行为。您必须呼叫new
。
您还只分配了一个 delete
(并使用值uint32_t
初始化),而不是许多img_size
的数组。为此,您将需要img_size
(以及以后的new (std::nothrow) uint32_t[img_size];
而不是delete[]
)。因此,您将使用delete
写越界。
您还需要检查fread
的返回值不是空指针,否则将在分配失败时发生。如果使用投掷版本,则不需要该支票。
尽管如此,请不要使用new(std::nothrow)
,而应使用new
。 C ++中的std::vector
比malloc
还要糟糕。
类似地,不要在C ++中使用C IO库。请改用new
。