[英]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.
env:ubuntu 16.04 VM 工作站 Com: g++, version:5.4.0 c++:c++11 Lib: 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)
的返回值是否不是空指針,這會在分配失敗時發生。 如果您使用投擲版本,那么您將不需要該檢查。
請不要像這樣使用new
而是使用std::vector
。 C++ 中的malloc
甚至比new
更糟糕。
同樣,不要在 C++ 中使用 C IO 庫。 改用std::ifstream
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.