vendredi 11 novembre 2022

Getting Segmentation violation signal error on copying vector elements?

I have the following code for doing a http post request. I am getting the response in const std::vector<json> value ...The below code works correctly

RpcResponseAndContext<std::vector<Largest>>
Connection::getLargestAccounts() const {
  const json params = {};
  const auto reqJson = jsonRequest("getLargest", params);
  const json res = sendJsonRpcRequest(reqJson);
  const std::vector<json> value = res["value"];
  std::vector<Largest> accounts_list;
  accounts_list.reserve(value.size());
  for (const json &account : value) {
    accounts_list.push_back(account);
  }
  return {res["context"], accounts_list};
}

To improve the appending of elements into accounts_list vector..I am trying to use std::copy() But getting this error: FATAL ERROR: test case CRASHED: SIGSEGV - Segmentation violation signal

I am trying this way:

RpcResponseAndContext<std::vector<Largest>>
Connection::getLargestAccounts() const {
  const json params = {};
  const auto reqJson = jsonRequest("getLargest", params);
  const json res = sendJsonRpcRequest(reqJson);
  const std::vector<json> value = res["value"];
  std::vector<Largest> accounts_list;
  accounts_list.reserve(value.size());
  std::copy(value.begin(), value.end(), accounts_list.begin());
  return {res["context"], accounts_list};
}

What I am doing wrong ?

Aucun commentaire:

Enregistrer un commentaire