gpt4 book ai didi

c - 调用 mxCreateDoubleMatrix() 时什么会导致段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 04:06:17 25 4
gpt4 key购买 nike

我正在链接 matlab 以从我的 C 代码中编写 matfiles。
运行以下代码时,出现段错误(在第一行):

pPressure_a = mxCreateDoubleMatrix((MWSIZE)2, (MWSIZE)dim, mxREAL);
if(pPressure_a == NULL){
fatal("Memory alocation error.");
}
copyDoubleToPtr2D(temp2D_a, mxGetPr(pPressure_a), dim, 2);
matPutVariable(matfile, "p", pPressure_a);
mxDestroyArray(pPressure_a);

在上面的代码片段中,“MWSIZE”被#defined as int32_t,因为我正在编译它使用 matlab R14,但 #define 允许轻松更改它以便与其他版本。使用 gdb,我已经确认当第一行被称为 pPressure_a =0x0;和 dim = 501,它们是完全有效的值。

这是回溯:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff73eeb5d in ?? () from /lib/libc.so.6

(gdb) bt
#0 0x00007ffff73eeb5d in ?? () from /lib/libc.so.6
#1 0x00007ffff73f103e in malloc () from /lib/libc.so.6
#2 0x00007ffff7730e35 in ?? () from /usr/local/matlabr14/bin/glnxa64/libut.so
#3 0x00007ffff7731600 in ?? () from /usr/local/matlabr14/bin/glnxa64/libut.so
#4 0x00007ffff7730046 in utCalloc () from /usr/local/matlabr14/bin/glnxa64/libut.so
#5 0x00007ffff7236c76 in mxCreateNumericMatrix () from /usr/local/matlabr14/bin/glnxa64/libmx.so
#6 0x00007ffff7237620 in mxCreateDoubleMatrix () from /usr/local/matlabr14/bin/glnxa64/libmx.so
#7 0x0000000000412c28 in calcCohAcoustPress (settings=0x646ab0) at /home/eey/models/cTraceo/./calcCohAcoustPress.c:324
#8 0x0000000000413736 in main (argc=2, argv=0x7fffffffe038) at cTraceo.c:137

(gdb) f 7
#7 0x0000000000412c28 in calcCohAcoustPress (settings=0x646ab0) at /home/eey/models/cTraceo/./calcCohAcoustPress.c:324
324 pPressure_a = mxCreateDoubleMatrix((MWSIZE)2, (MWSIZE)dim, mxREAL);

我在代码的前面以相同的方式(多次)调用 mxCreateDoubleMatrix(),并且从未遇到过问题。

这可能是什么原因造成的?

编辑:运行 valgrind 会产生几个这样的 block :

==17538== Conditional jump or move depends on uninitialised value(s)
==17538== at 0x4E560B8: csqrt (s_csqrt.c:66)
==17538== by 0x40E149: solveDynamicEq (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x411BA5: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)

并以此结束:

Invalid read of size 8
==17538== at 0x412A3F: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538== Address 0x86a3050 is 0 bytes after a block of size 16 alloc'd
==17538== at 0x4C2815C: malloc (vg_replace_malloc.c:236)
==17538== by 0x401781: mallocDouble2D (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x41296D: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538==
==17538== Invalid write of size 8
==17538== at 0x412A43: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538== Address 0x0 is not stack'd, malloc'd or (recently) free'd

==17538== Process terminating with default action of signal 11 (SIGSEGV)
==17538== Access not within mapped region at address 0x0
==17538== at 0x412A43: calcCohAcoustPress (in cTraceo/bin/cTraceo-64b.bin)
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538== If you believe this happened as a result of a stack
==17538== overflow in your program's main thread (unlikely but
==17538== possible), you can try to increase the size of the
==17538== main thread stack using the --main-stacksize= flag.
==17538== The main thread stack size used in this run was 8388608.
==17538== Invalid free() / delete / delete[]
==17538== at 0x4C27D71: free (vg_replace_malloc.c:366)
==17538== by 0x58FAA0A: free_mem (in /lib/libc-2.12.1.so)
==17538== by 0x58FA5A1: __libc_freeres (in /lib/libc-2.12.1.so)
==17538== by 0x4A2366B: _vgnU_freeres (vg_preloaded.c:62)
==17538== by 0x7FEFFFD27: ???
==17538== by 0x413555: main (in cTraceo/bin/cTraceo-64b.bin)
==17538== Address 0x41508a0 is not stack'd, malloc'd or (recently) free'd

可悲的是,我是 valgrind 的新手,实际上并不知道那是什么意思...:p

最佳答案

是否有可能实际崩溃不是发生在 mxCreateDoubleMatrix() 调用中,而是发生在 copyDoubleToPtr2D() 调用中?如果是这样,这可能是由于提到的 MEX 的内部[非常烦人且难以追踪]限制 here .解决方案是简单地使用 memcpy() 而不是 copyDoubleToPtr2D()。

关于c - 调用 mxCreateDoubleMatrix() 时什么会导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5513572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com