执行结束时的c++段错误

c++ segment fault at the end of execution

本文关键字:错误 段错误 c++ 结束 执行      更新时间:2023-10-16

我的代码编译了,大部分都按照预期进行,但在执行结束时出现了段错误,它应该更新(附加)文件,但没有

#include <stdio.h>
#include <curl/curl.h>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
        size_t written;
        written = fwrite(ptr, size, nmemb, stream);
        return written;
}

int main(void)
{
        CURL *curl;
        CURLcode res;
        curl = curl_easy_init();
        if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %sn",
                        curl_easy_strerror(res));
        FILE * pFile;
        pFile = fopen ("myfile.txt","a+");
        if (pFile!=NULL)
        {
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);
                res = curl_easy_perform(curl);
                fclose (pFile);
        }
        /* always cleanup */
        curl_easy_cleanup(curl);
  }
  return 0;
}

这是gdb调试输出:

gdb /home/coinz/cryptsy/getprice.o /home/coinz/cryptsy/core
GNU gdb (Gentoo 7.8 vanilla) 7.8
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/coinz/cryptsy/getprice.o...done.
warning: exec file is newer than core file.
[New LWP 665]
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./getprice.o'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000011e12a0 in ?? ()
(gdb) bt full
#0  0x00000000011e12a0 in ?? ()
No symbol table info available.
#1  0x00007f9e78b9ca48 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#2  0x00007f9e78bb4cd0 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#3  0x00007f9e78bb0a4a in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#4  0x00007f9e78bb8fe8 in ?? () from /usr/lib64/libcurl.so.4
No symbol table info available.
#5  0x00007f9e78bb9e15 in curl_multi_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#6  0x00007f9e78bb22d6 in curl_easy_perform () from /usr/lib64/libcurl.so.4
No symbol table info available.
#7  0x0000000000400a76 in main () at getprice.cpp:35
        pFile = 0x11e12a0
        curl = 0x11ce2c0
        res = CURLE_OK
(gdb)

以下行是一个错误:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pFile);

CURLOPT_WRITEFUNCTION选项需要一个函数指针,但您传递了文件处理程序。此外,您永远不会告诉libcurl使用您的write_data函数。

您应该同时设置写入功能和写入数据选项。

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile);

更多细节可以在那里找到:

  • http://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html
  • http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html