遇到表达式GSL_SET_COMPLEX时出现分段错误 [英] Segmentation fault on encountering expression GSL_SET_COMPLEX

查看:54
本文介绍了遇到表达式GSL_SET_COMPLEX时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++编写代码来解决电动力学问题。代码是使具有复杂元素的矩阵M对角化。我使用GSL库函数来表示复数和对角化。矩阵M在代码中由语句定义


gsl_complex M [维度] [维度] //维度是在代码中较早定义和初始化的int


在矩阵M的对角化之前,我使用语句

将其每个元素初始化为0(int p = 0; p< =(dimension - 1); p ++ ){

for(int q = 0; q< =(dimension - 1); q ++){

GSL_SET_COMPLEX(& M [p] [q], 0,0);}}

程序编译时没有错误但执行时会产生分段错误。使用GDB,分段故障被追溯到线GSL_SET_COMPLEX(& M [p] [q],0,0);它将M,M [p] [q]的p,q元素的实部和虚部设置为0.


想知道这里是否有人有任何关于为什么的线索程序正在退出并出现分段错误。我似乎使用正确的语法来设置矩阵元素(http://www.gnu.org/s/gsl/manual/html_node/Representation-of-complex-numbers.html)


谢谢

I am writing a code in C++ to solve a problem in electrodynamics. The code is to diagonalize a matrix M with complex elements. I am using the GSL library functions for representing complex numbers and the diagonalization. The matrix M is defined in the code by the statement

gsl_complex M[dimension][dimension] //dimension is an int defined and initialized earlier in the code

Before diagonalization of the matrix M, I initialize each element of it to 0 using the statements
for (int p = 0; p <= (dimension - 1); p++){
for (int q = 0; q <= (dimension - 1); q++){
GSL_SET_COMPLEX(&M[p][q], 0, 0);}}

The program compiles without error but on execution gives a segmentation fault. Using GDB the segmentation fault is traced to the line GSL_SET_COMPLEX(&M[p][q],0,0); which sets the real and imaginary parts of the p,q-th element of M, M[p][q], to 0.

Wondering if anyone here has any clues on why the program is exiting with a segmentation fault. I seem to be using the correct syntax for setting the matrix elements (http://www.gnu.org/s/gsl/manual/html_node/Representation-of-complex-numbers.html)

Thanks

推荐答案

我认为你的内部循环需要从0运行到p-1,因为pq数组的p元素是q qrrays。因此,每个p实际上是一个q数组,编号从0到q-1而不是维度-1。
I think your inner loop needs to run from 0 to p-1 since a pq array has p elements that are q qrrays. Therefore each p is really a q array numbered from 0 to q-1 and not dimension-1.


你好。感谢您的回复。这里M是尺寸(尺寸)x(尺寸)的矩阵,并且在语句gsl_complex M [dimension] [dimension]中定义如此。 c ++中M的第pq元素被引用为M [p] [q](参见 http://www.cplusplus.com/doc/tutorial/arrays/ 。然后不是for循环写入访问矩阵的每个元素并将其设置为0(将p视为行索引,q作为列索引)?
Hi there. Thanks for your reply. Here M is matrix of size (dimension)x(dimension) and is defined as so in the statement gsl_complex M[dimension][dimension]. The pq-th element of M in c++ is referenced as M[p][q] (see section on Multidimensional arrays in http://www.cplusplus.com/doc/tutorial/arrays/ for e.g.). Doesn''t the for loop then as written access each element of the matrix and set it to 0 (think of p as the row index and q as the column index) ?


你是正确。


实际上我运行你的代码,运行程序时没有分段错误。


你确定你已经创建了数组?


这是我用的:

You are correct.

In fact I ran your code an there is no segmentation fault when I run the program.

Are you sure you have created the array?

Here''s what I used:

展开 | 选择 | 换行 | 行号


这篇关于遇到表达式GSL_SET_COMPLEX时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆