向 curl_easy_setopt 添加变量(段错误)

adding variables to curl_easy_setopt (seg fault)

#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 任何帮助都会非常棒,祝你有美好的一天!

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 似乎是一个拼写错误,应该删除。