【问题标题】:adding variables to curl_easy_setopt (seg fault)向 curl_easy_setopt 添加变量(段错误)
【发布时间】:2021-05-04 14:28:04
【问题描述】:
#include <curl/curl.h>
#include <cstdlib>
#include <iostream>


int rad = rand() % 100000;
std::string agent_name = "https://127.0.0.1:443/agent/" + rad;
std::string full_agent_name = agent_name + std::to_string(rad);

int main(int argc, char **)
{
  CURLcode ret;
  CURL *curl;

  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(curl, CURLOPT_URL, full_agent_name.c_str());
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

  ret = curl_easy_perform(curl);

  curl_easy_cleanup(curl);

}

我不明白为什么这是段错误。我是 C++ 新手,只知道 python3 任何帮助都会非常棒,祝你有美好的一天!

【问题讨论】:

  • rad 添加到agent_namefull_agent_name 有什么意义?似乎您根本不​​应该将rad 添加到agent_namestd::string agent_name = "https://127.0.0.1:443/agent/"; std::string full_agent_name = agent_name + std::to_string(rad);
  • 雅哈哈,我应该更好地审查它,错字:(

标签: c++ curl segmentation-fault


【解决方案1】:
std::string agent_name = "https://127.0.0.1:443/agent/" + rad;

这并不符合您的预期。这会将rad 字节添加到字符串文字的地址。试试

std::string agent_name = "https://127.0.0.1:443/agent/" + std::to_string(rad);

using namespace std::string_literals;
std::string agent_name = "https://127.0.0.1:443/agent/"s + std::to_string(rad);

与您的问题无关。 int rad = rand() % 100000; 如果伪随机数生成器未使用 srand 播种,则每次运行都会给出相同的随机数 rad。

更新。您有两个变量 agent_name 和 full_agent_name。第一个变量赋值中的+ rad 似乎是一个错字,应该删除。

【讨论】:

  • 是的,我知道它会给同样数量的需要它,但无论如何你是最好的!
猜你喜欢
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多