为什么我会根据标题的包含顺序出现分段错误?

时间:2017-05-23 17:39:48

标签: c++

我有以下文件,它是较大(但仍然很小)程序的一部分。无论头部包含顺序如何,程序都会编译,但如果我在本地文件之后包含<fstream>,则会出现错误;如果我在我的本地头文件之前包含它,程序运行正常。

#include <fstream>
#include "Bitmap.h"
#include "BitmapInfoHeader.h"
#include "BitmapFileHeader.h"

namespace bmap
{

Bitmap::Bitmap(int width, int height) : _width(width), _height(height), _pixels(new uint8_t[width * height * 3]{}) { }

void Bitmap::set_pixel(int x, int y, uint8_t red, uint8_t blue, uint8_t green) { }

bool Bitmap::write(string filename)
{
  BitmapFileHeader file_header;
  BitmapInfoHeader info_header;

  file_header.file_size = sizeof(BitmapInfoHeader) + sizeof(BitmapFileHeader) + _width * _height * 3;
  file_header.data_offset = sizeof(BitmapInfoHeader) + sizeof(BitmapFileHeader);

  info_header.width = _width;
  info_header.height = _height;

  std::ofstream outfile(filename, ios::out | ios::binary);
  if(!outfile) { return false; }
  outfile.write((char *)(&file_header), sizeof(file_header));
  outfile.write((char *)(&info_header), sizeof(info_header));
  outfile.write((char *)(_pixels.get()), _width * _height * 3);
  outfile.close();
  return true;
}

Bitmap::~Bitmap() { }

}

上面文件中包含的头文件如下所示。

Bitmap.h:

#ifndef BITMAP_H_
#define BITMAP_H_

#include <cstdint>
#include <string>

using namespace std;

namespace bmap{

class Bitmap
{
 private:
  int _width{0};
  int _height{0};
  unique_ptr<uint8_t[]> _pixels{nullptr};

 public:
  Bitmap(int width, int height);
  bool write(string filename);
  void set_pixel(int x, int y, uint8_t red, uint8_t blue, uint8_t green);
  virtual ~Bitmap();
};
}

#endif

BitmapFileHeader.h:

#ifndef BITMAPFILEHEADER_H_
#define BITMAPFILEHEADER_H_

#include <cstdint>

namespace bmap
{

#pragma pack(2)
struct BitmapFileHeader
{
  char header[2]{ 'B', 'M' };
  int32_t file_size;
  int32_t reserved{0};
  int32_t data_offset;
};
}

#endif

BitmapInfoHeader.h:

#ifndef BITMAPINFOHEADER_H_
#define BITMAPINFOHEADER_H_

#include <cstdint>

namespace bmap
{

#pragma pack(2)
  struct BitmapInfoHeader
  {
    int32_t header_size{40};
    int32_t width;
    int32_t height;
    int16_t planes{1};
    int16_t bits_per_pixel{24};
    int32_t compression{0};
    int32_t data_size{0};
    int32_t horizontal_resolution{2400};
    int32_t vertical_resolution{2400};
    int32_t colors{0};
    int32_t important_colors{0};
  };
}
#endif

我认为它可能是头文件之间的某种依赖,但似乎没有。我也尝试重命名Bitmap::write方法,因为我认为它可能会以某种方式与<fstream>中的write方法发生冲突,但这并没有改变任何东西。有什么想法吗?

GDB提供以下信息:

Program received signal SIGSEGV, Segmentation fault.
0x00007fff994bdf24 in std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush() () from /usr/lib/libc++.1.dylib
(gdb) where
#0  0x00007fff994bdf24 in std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush() () from /usr/lib/libc++.1.dylib
#1  0x00007fff994c299e in std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) () from /usr/lib/libc++.1.dylib
#2  0x00007fff994c41a6 in std::__1::basic_ostream<char, std::__1::char_traits<char> >::write(char const*, long) () from /usr/lib/libc++.1.dylib
#3  0x0000000100001626 in bmap::Bitmap::write (this=0x7fff5fbffa50, filename=...) at Bitmap.cpp:26
#4  0x0000000100004d48 in main () at Mandelbrot.cpp:11
(gdb) 

0 个答案:

没有答案