c++ - 转换和传递数组

标签 c++ arrays pointers

我基本上想将我正在读取的这个数据数组传递给不同的函数并最终绘制它。

该数组包含一个由“1”和“0”组成的 32 位字,然后我想将这些单独的位加在一起以查看我的数据峰值在哪里。所以换句话说,如果我将“0100”添加到“0110”,我会得到“0210”——这可能通过单独的 bin 和绘图更容易完成。

目前我只是在清理垃圾。

void binary(int convert, int* dat) {
  bitset<32> bits(convert);
  //cout << bits.to_string() << endl;
  char data[32];

  for(unsigned i = 0; i < 32; ++i) {
    data[i] = bits[i];
  }
  for(unsigned i = 32; i; --i) {
    dat[i] = (int(data[i-1]))+dat[i];
  }
}


void SerDi() {
  int dat[32];
  cout << "    Reading data from memory..." << endl;
  ValVector< uint32_t> data=hw.getNode("SerDi.RAM").readBlock(8);
  hw.dispatch();
  cout << data[0]<<endl;
  cout << data[1]<<endl;
  for (unsigned i = 2; i < 7; i++) {
    binary(data[i], dat);
  }
  cout << dat[7] << endl;
  graph(dat); //passes the array to a place where I can plot the graph
}

最佳答案

你有

int dat[32];

但是在 convert 中你有 i = 32dat[i] 这将访问数组之外​​的东西并且会发生不好的事情。

还有就是没有初始化。在某处添加内存集/循环以使 dat 最初为 0

关于c++ - 转换和传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773112/

相关文章:

c++ - C++ 中的无限 While 循环

javascript - 通过元素的子字符串环回过滤数组中的对象

javascript - ES6/7等同于underscore的range函数

c++ - 修改指向另一个指针的指针

c++ - g++ 悬挂指针警告不一致?

c - 使用结构体添加两个二维数组时出现段错误 11

c# - 捕捉引擎ExecutionException

c++ - 如何在 C++ 中完成 corecursion?

c++ - g++ undefined reference 在非常简单的例子中

c++ - 如何在结构中动态添加数据,该结构是指向指针数组的指针