大阵列大小时出现段故障的原因

时间:2019-06-14 05:02:09

标签: c++ segmentation-fault

我必须制作一个rv32模拟器。为此,我目前正在研究“内存”类,它将帮助我跟踪内存。

由于我正在使用的版本是32位,因此我需要一个数据结构来表示内存存储。从概念上讲,内存只是一个字节数组,使用字节地址进行索引,因此我正在使用数组来做到这一点。

class memory {

 private:

  // TODO: Add private members here
  int * storage= new int [100000000];

 public:

  // Constructor
  memory(bool verbose);

  // Read a word of data from a word-aligned address.
  uint32_t read_word (uint32_t address);

  // Write a word of data to a word-aligned address.
  void write_word (uint32_t address, uint32_t data, uint32_t mask);

  // Load a hex image file and provide the start address for execution from the file in start_address.
  bool load_file(string file_name, uint32_t &start_address);

};

当我使用“存储”数组时,如果地址类似80000000,它将在return语句中给我一个段错误。

uint32_t memory::read_word (uint32_t address) {
  //rounding up multiple
  if((address %4 )!=0){
    int remainder =(address)%4;
    address=address-remainder;
  }
  return (storage[address]); 

}

0 个答案:

没有答案