执行结束时的c++段错误
c++ segment fault at the end of execution
我的代码编译了,大部分都按照预期进行,但在执行结束时出现了段错误,它应该更新(附加)文件,但没有
#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
相关文章:
- 为什么PyImport_ImportModule python 3.7.2 中出现段错误?
- 为什么在访问 vtkRenderWindow 的"交互器"变量时会发生段错误?
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 更改条件段错误
- 使用 TTF_RenderText() 加载字体时获取段错误 TTF_OpenFontRW()
- 注册对对象工厂的调用会导致段错误
- pthread_create在构造函数段错误中
- Nanoflann发现邻居提出段错误
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- 具有unique_ptr的 CRTP 会导致段错误
- 增强纤维work_stealing屏障会导致段错误
- 当我返回指向结构的指针向量时出现段错误
- C++为什么我的代码没有爆炸/段错误?
- Pthread段错误,使用指向main中变量的指针
- C++ 模板中的段错误
- 相当于Windows/MSVC上的段错误?
- 为什么我的 LLVM JIT 实现出现段错误?
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 为什么自删除的全局 Vulkan 实例仅在添加层时才导致段错误?