C代码行在完全不相关的代码行上导致分段错误

Mat*_*ell 2 c exc-bad-access llvm opencl segmentation-fault

根据Xcode中的堆栈跟踪,我的程序在这里断开并给出了EXC_BAD_ACCESS:

int main (int argc, const char * argv[]) {
    float usd,btc,bid,ask,order_price,possible_price;
    DateData * prices = load_prices(); <---

DateData * load_prices(){
    FILE * file = fopen("price.dat", "rb"); <---
Run Code Online (Sandbox Code Playgroud)

我确定这与该行代码无关,而是后来的代码.在直到稍后才调用的函数中,有一行代码似乎破坏了程序.当它从执行行之前的函数返回时,程序没有这个问题,但如果它应该从行后的函数返回,则存在这个问题.

代码行是对OpenCL的调用.它是否以某种方式破坏了该计划?

err = clEnqueueReadBuffer(ocl_data->commands, ocl_data->output, CL_TRUE, 0, sizeof(CombinationResult) * PPO_COMBINATIONS, (*PPO_results)[x] + PPO_COMBINATIONS*(p + 5), 0, NULL, NULL); 
Run Code Online (Sandbox Code Playgroud)

PPO_COMBINATIONS定义为整数宏,PPO_results的类型为CombinationResult(*)[3] [PPO_COMBINATIONS*11].ocl_data-> commands的类型为cl_command_queue,ocl_data-> output的类型为cl_mem.错误,p和x的类型为int.

我正在使用Xcode和"Apple LLVM Compiler 3.0"."LLVM GCC 4.2"编译器出于某种原因给出了"架构i386的格式错误的元数据记录".

这是使用命令行编译和运行gdb时的结果:

Matthew-Mitchell:Parrallel BitCoin Trading Algorithm matt$ gcc -g cmain.c -o test -lcurl -framework OpenCL -std=c99 -arch i386
Matthew-Mitchell:Parrallel BitCoin Trading Algorithm matt$ gdb testGNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done

(gdb) run
Starting program: /Users/matt/Programming/Bit Coin algorithm/Parrallel BitCoin Trading Algorithm/test 
Reading symbols for shared libraries .+++.................................................................. done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0xbea7d7cc
0x00003e9a in main (argc=0, argv=0x1000) at cmain.c:572
572 int main (int argc, const char * argv[]) {
Run Code Online (Sandbox Code Playgroud)

直接在命令行中运行时,程序立即退出.

Mat*_*ell 8

感谢MrGomez做了一个很好的猜测,但实际答案一直盯着我.答案是这个网站的名称.问题是主函数中的自动变量太大而导致堆栈溢出.解决方案是使用malloc分配数据.

对于任何从谷歌看到这个的人来说,最好检查你声明的变量有多大.您可能希望在运行时分配内存.