Grandma Kisses
Grandma Kisses

Reputation: 55

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);

}

I do not understand why this is seg faulting. I am new to C++ and only really know python3 any help would be super great, have a good day!

Upvotes: 0

Views: 68

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38465

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

This does not do what you expect. This adds rad bytes to the address of the string literal. Try

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

or

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

Unrelated to your question. int rad = rand() % 100000; will give the same random number rad each run if the pseudo random number generator is not seeded with srand.

Update. You have two variables agent_name and full_agent_name. It seems + rad in the first variable assignment is a typo and should be removed.

Upvotes: 2

Related Questions