473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Segmentation fault on encountering expression GSL_SET_COMPLEX

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
Aug 10 '11 #1
10 2461
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 10 '11 #2
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) ?
Aug 10 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. struct Array
  2. {
  3.    int data;
  4. };
  5. #define dimension 10
  6.  
  7. void GSL_SET_COMPLEX(Array* arg, int real, int imag)
  8. {
  9.       arg->data = real;
  10. }
  11.  
  12. int main()
  13. {
  14. Array M[dimension][dimension];
  15. for (int p = 0; p <= (dimension - 1); p++){
  16.  for (int q = 0; q <= (dimension - 1); q++){
  17.  GSL_SET_COMPLEX(&M[p][q], 0, 0);}}
  18.  
  19. for (int p = 0; p <= (dimension - 1); p++){
  20.  for (int q = 0; q <= (dimension - 1); q++){
  21.  cout << M[p][q].data << endl;}}
  22.  
  23.  
  24. }
Aug 10 '11 #4
donbock
2,426 Expert 2GB
As I understand it, GSL_SET_COMPLEX is a function-like macro. If it were a function, then its prototype would be:
Expand|Select|Wrap|Line Numbers
  1. void GSL_SET_COMPLEX(gsl_complex *p, double realvalue, double imaginaryvalue);
weaknessforcats: you passed the address of an Array rather than of a gsl_complex. Why did that work?
Oops. I see, you used a local definition of GSL_SET_COMPLEX.

M is declared as a two-dimensional array of gsl_complex; or more pedantically, as an array of one-dimensional arrays of gsl_complex. I don't use 2-dimensional arrays enough to remember: is &M[2][3] the address of one gsl_complex or is it the address of an array of gsl_complex? (I would have assumed this works the way you expect, but when something goes wrong you have to examine your assumptions.)
There's a great article in the Insights tab called Arrays Revealed, but I can't seem to call it up.

I suggest you verify you are passing proper arguments to GSL_SET_COMPLEX by adding a function prototype for it (but under a different name, perhaps MY_GSL_SET_COMPLEX) and then calling that new name instead. You will get compiler errors if any of the arguments are of the wrong type.
Aug 10 '11 #5
donbock
2,426 Expert 2GB
Are you sure dimension is greater than 0?

Take a look at gsl/gsl_complex.h and gsl/gsl_complex_math.h to find out how gsl_complex and GSL_SET_COMPLEX are defined.
Aug 10 '11 #6
weaknessforcats
9,208 Expert Mod 8TB
@donbock: It worked because I wrote my own GSL_SET_COMPLEX function. I was trying to show that the loops did not cause the segmentation fault. That meant the fault was in the creation of the array or the processing of it.
Aug 11 '11 #7
Thanks for the replies.

@weaknessforcats:
> Are you sure you have created the array?
Doesn't the declaration statement gsl_complex M[dimension][dimension]; create the array ?

@donblock:
>is &M[2][3] the address of one gsl_complex or is it the address of an array of gsl_complex?
I believe it is the address of one gsl_complex
>Are you sure dimension is greater than 0?
Yes. I printed out the value of dimension before the declaration of M[dimension][dimension] and the for loop to check whether dimension is being initialized correctly and indeed it is (in my program the value of dimension is 876)
> I suggest you verify you are passing proper arguments to GSL_SET_COMPLEX
I believe I am passing the proper arguments. I will try out your suggestion of defining a function prototype and post the results.

Thanks
Aug 11 '11 #8
weaknessforcats
9,208 Expert Mod 8TB
If your dimension is 876 x 876 you will need 767376 elements. Probably this is too large to fit on the stack. I suggest you change your code to create the array on the heap using the new operator:

Expand|Select|Wrap|Line Numbers
  1. gsl_complex (* M)[dimension]= new gas_complex[dimension][dimension];
  2.  
Aug 11 '11 #9
Yes, the dimension indeed seems too large. I reduced the dimension to 243x243 and the code executes without segmentation fault.

I do not quite know what you mean by create the array on the heap ?
Aug 12 '11 #10
weaknessforcats
9,208 Expert Mod 8TB
When you call a function, the compiler generates code to allocate memory for the variables the function needs. This memory is called a stack frame. If the called function itself calls a function another stack frame is created. These stack frames are managed using a data strcture called a heap.

So in programmerspeak,a variable is "on the stack" when the compiler creates it.

There may be some restrictions on the maximum size of a stack frame.

You may also create variables by allocating your own memory at run time. When you do this, the memory allocation is also maintained in a data structure called a heap. Hence, when you allocate your own memory, it is said to be "on the heap".

There are no size restrictions for allocating on the heap.

You use the C++ "new" operator to allocate your own memory.

Just remember that any memory you allocate you must personally release when you no longer need it. Use the C++ "delete" operator to do that.

The code sinppet on my last poat creates your array "on the heap" because it uses the "new" operator.

Lastly, stack and heap are compiler design concepts and not part of the C++ language. All C++ requires is that you and the compiler must be able to create variables.
Aug 12 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
21
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
7
by: Adi | last post by:
hi guys, i have a weird problem with printf statement. I have a function which just prints a string literal. In my program this function will be called > 2000 times. I get a segmentation fault...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.