分页: 1 / 1

关于libcurl的问题

发表于 : 2012-07-31 17:38
250871082
因为要POST数据,所以就用libcurl,可每次输完信息会出现什么正在连接,然后就段错误,用gdb显示
Program received signal SIGSEGV, Segmentation fault.
0xb7fa92ce in Curl_raw_nequal () from /usr/lib/i386-linux-gnu/libcurl-gnutls.so.4
这是部分代码:

代码: 全选

char username[32];
char password[32];
char score[7];
double double_score;
CURL *easy_handle = curl_easy_init();
curl_easy_setopt(easy_handle, CURLOPT_VERBOSE, 1);
curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1);
curl_easy_setopt(easy_handle, CURLOPT_URL, "某个地址");
printf("请输入用户名:");
scanf("%s", username);
fflush(stdin);
printf("请输入密码:");
scanf("%s", password);
fflush(stdin);
for(;;)
{
    printf("请输入一个数:");
    scanf("%lf", &double_score);
    if(double_score<100)
    {
        break;
    }
    else
    {
        printf("太大了!\n");
        continue;
    }
}
gcvt(double_score, 5, score);
struct curl_httppost* post=NULL;
struct curl_httppost* last=NULL;
curl_formadd(&post, &last, CURLFORM_COPYNAME, "a", CURLFORM_COPYCONTENTS, score, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "b", CURLFORM_COPYCONTENTS, password, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "c", CURLFORM_COPYCONTENTS, username, CURLFORM_END);
curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST, post);
curl_easy_perform(easy_handle);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();

Re: 关于libcurl的问题

发表于 : 2012-07-31 18:37
YeLee
段错误……单步调试吧,看错误出现在哪一步。 :em20 :em20 :em20

Re: 关于libcurl的问题

发表于 : 2012-08-01 15:45
250871082
YeLee 写了:段错误……单步调试吧,看错误出现在哪一步。 :em20 :em20 :em20
肯定是

代码: 全选

curl_easy_perform(easy_handle);

Re: 关于libcurl的问题

发表于 : 2012-08-01 16:41
eexpress
*easy_handle = curl_easy_init();

说不定init没实际分配内存空间。段错误,通常是地址越界,没实际空间。猜的。

Re: 关于libcurl的问题

发表于 : 2012-08-01 19:21
YeLee
我想起了curl_global_init而已,调试不仅仅要看运行到哪一步,还得看一些变量的值的嘛。 :em06 :em06 :em06

Re: 关于libcurl的问题

发表于 : 2012-08-03 18:29
250871082
YeLee 写了:我想起了curl_global_init而已,调试不仅仅要看运行到哪一步,还得看一些变量的值的嘛。 :em06 :em06 :em06
把断点设在那个curl_easy_perform前,好像username变量和password变量后面有一些乱码