core dump when using ifstream

J

Jing Cheng

Hi,

I'm using ifstream reading data from a file, as following:


ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}

int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;

finput >> x >> xp >> z >> zp;

......
}

finput.close();


but I met a segment fault(core dumped),

then I use gdb to trace the core:
lyyang@walnut ~/research $ gdb lcsimp core
GNU gdb 6.0
Copyright 2003 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 "i686-pc-linux-gnu"...Using host libthread_db
library "/lib/libthread_db.so.1".

Core was generated by `./lcsimp'.
Program terminated with signal 11, Segmentation fault.

warning: current_sos: Can't read pathname for load map: Input/output error

Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5...done.
Loaded symbols for /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1...done.
Loaded symbols for /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0xb7e379e3 in free () from /lib/libc.so.6
(gdb) backtrace
#0 0xb7e379e3 in free () from /lib/libc.so.6
#1 0xb7fad8e7 in operator delete(void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#2 0xb7fad939 in operator delete[](void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#3 0xb7f576b7 in std::basic_filebuf said:
::_M_destroy_internal_buffer() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#4 0xb7f575c1 in std::basic_filebuf said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#5 0xb7f5a746 in std::basic_ifstream said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#6 0x080496f0 in main () at lcsimp.cpp:158
(gdb)


Here the 158th line is the end of my main().

What's the problem ?
my data file track.dat is only one line, here is the hexdump, no special
characters:

lyyang@walnut ~/research $ hexdump -C track.dat
00 31 30 30 20 2e 30 32 36 33 20 30 2e 30 20 30 2e |100 .0263 0.0 0.|
10 30 34 32 35 20 30 2e 30 |0425 0.0|
018



Any help would be apreciated. Thanks!

Lingyun
 
L

Larry Brasfield

Jing Cheng said:
Hi, Hi.
I'm using ifstream reading data from a file, as following:


ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}

int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;

finput >> x >> xp >> z >> zp;

......
}

finput.close();


but I met a segment fault(core dumped),

then I use gdb to trace the core:


[Cut gdb session noise.]


The bug is not apparent in the code you show.
What are 'x', 'xp', 'z', and 'zp'? Are they
pointers? If so, to what do they point?
 
J

Jing Cheng

Larry said:
Hi,
Hi.

I'm using ifstream reading data from a file, as following:


ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}

int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;

finput >> x >> xp >> z >> zp;

......
}

finput.close();


but I met a segment fault(core dumped),

then I use gdb to trace the core:



[Cut gdb session noise.]


The bug is not apparent in the code you show.
What are 'x', 'xp', 'z', and 'zp'? Are they
pointers? If so, to what do they point?
no, they are not points:
double x, xp, z, zp;

actually, If I add some more data into the track.dat I want to read
from. only after I read the last line and finished it, the program
met segment fault.
 
A

Artie Gold

Jing said:
Hi,

I'm using ifstream reading data from a file, as following:


ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}

int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;

finput >> x >> xp >> z >> zp;

......
}

finput.close();


but I met a segment fault(core dumped),

then I use gdb to trace the core:
lyyang@walnut ~/research $ gdb lcsimp core
GNU gdb 6.0
Copyright 2003 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 "i686-pc-linux-gnu"...Using host libthread_db
library "/lib/libthread_db.so.1".

Core was generated by `./lcsimp'.
Program terminated with signal 11, Segmentation fault.

warning: current_sos: Can't read pathname for load map: Input/output error

Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5...done.
Loaded symbols for /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1...done.
Loaded symbols for /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0xb7e379e3 in free () from /lib/libc.so.6
(gdb) backtrace
#0 0xb7e379e3 in free () from /lib/libc.so.6
#1 0xb7fad8e7 in operator delete(void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#2 0xb7fad939 in operator delete[](void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#3 0xb7f576b7 in std::basic_filebuf said:
::_M_destroy_internal_buffer() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#4 0xb7f575c1 in std::basic_filebuf said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#5 0xb7f5a746 in std::basic_ifstream said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#6 0x080496f0 in main () at lcsimp.cpp:158
(gdb)


Here the 158th line is the end of my main().

What's the problem ?
my data file track.dat is only one line, here is the hexdump, no special
characters:

lyyang@walnut ~/research $ hexdump -C track.dat
00 31 30 30 20 2e 30 32 36 33 20 30 2e 30 20 30 2e |100 .0263 0.0 0.|
10 30 34 32 35 20 30 2e 30 |0425 0.0|
018
Your bug is quite likely elsewhere in your code; a segmentation fault in
operator delete (implemented on top of free() in your implementation) is
almost always due to a corrupted free store.

HTH,
--ag
 
J

Jing Cheng

Better to post all my code:


#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>

using namespace std;

void nonlin(double* nl, double* bn, double x, double z, double& fnlx,
double& fnlz)
{
complex<double> y, fy, aima, fact;

aima = complex<double> (0.0,1.0);

y = complex<double> (x,z);
fy=complex<double> (0.0,0.0);

for(int i = 0; i < 10; ++i){
//DO 600 m=1,10
double inl = abs(nl);
fact = complex<double> (1.0,0.0);
if (nl < 0) fact = aima;

fy += fact*bn*pow(y,inl);
// CONTINUE
}

fnlx=real(fy);
fnlz=imag(fy);
}


int main()
{
//double amux = 4.82, freqx = 0.0, amptx = 0.0, alphax = 0.0, betax
= 10.0;
//double amuz = 3.825, freqz = 0.0, amptz = 0.0, alphaz = 0.0,
betaz = 10.0;
double amux = 4.82, alphax = 0.0, betax = 10.0;
double amuz = 3.825, alphaz = 0.0, betaz = 10.0;
//double dx = 0.0, dxp = 0.0;

double anl[10], bn[10], al = 1.0, rho = 159.15;
double nl[10];
double strnl = al/rho;

anl[0] = -1.0;
bn[0] = 1.0;
nl[0] = anl[0];
for(int i = 1; i < 10; ++i){
nl = anl = bn = 0;
}

int turns = 0;
//double x(0.0263), xp(0.0), z(0.0425), zp(0.0);
double x, xp, z, zp;



double qrot, prot;

double cx, sx, cz, sz;

double emitx, emitz, phix, phiz;
double phi1;

double px, pz;

ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}


int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;
//cerr << "ntrial= " << ntrial << " turns= " << turns << "\n";

finput >> x >> xp >> z >> zp;

double am[4][4];

int nt = 0;

for(int i = 0; i < turns; ++i){

// Courant-Snyder Invariant (AP, p53, 2.93)
emitx = (1+alphax*alphax)/betax*x*x+2*alphax*x*xp+betax*xp*xp;
emitz = (1+alphaz*alphaz)/betaz*z*z+2*alphaz*z*zp+betaz*zp*zp;

// phi is in (2.86), AP, p53, 2.90, 2.91
phix = -atan2((betax*xp+alphax*x),x);
phiz = -atan2((betaz*zp+alphaz*z),z);

// p171
phi1 = phix-phiz;

px=xp*betax+x*alphax;
pz=zp*betaz+z*alphaz;

// (2.350) J_1 = J_x = 1/2*C=1/2*emitx
qrot = sqrt(emitx*betax)*cos(phi1);
prot = sqrt(emitx*betaz)*sin(phi1);

// output initial states
cout << ntrial << ' ' << nt << ' '
<< x << ' ' << xp << ' ' << z << ' ' << zp << ' '
<< qrot << ' ' << prot << endl;

//amamux = amux + (amptx*cos(M_PI*2.0*freqx*i));
//amamuz = amuz + (amptz*cos(M_PI*2.0*freqz*i));

//cerr << amamux << ' ' << amamuz << endl;

cx = cos(amux*2.0*M_PI);
sx = sin(amux*2.0*M_PI);
cz = cos(amuz*2.0*M_PI);
sz = sin(amuz*2.0*M_PI);

am[1][1] = cx+alphax*sx;
am[1][2] = betax*sx;
am[2][1] = -(1+alphax*alphax)*sx/betax;
am[2][2] = cx-alphax*sx;
am[3][3] = cz+alphaz*sz;
am[3][4] = betaz*sz;
am[4][3] = -(1+alphaz*alphaz)*sz/betaz;
am[4][4] = cz-alphaz*sz;


double xpm= am[2][1]*x+ am[2][2]*xp;
x = am[1][1]*x+ am[1][2]*xp;

double zpm= am[4][3]*z+ am[4][4]*zp;
z = am[3][3]*z+ am[3][4]*zp;

double fnlx, fnlz;
nonlin(nl, bn, x, z, fnlx, fnlz);

xp = xpm+strnl*fnlx;
zp = zpm-strnl*fnlz;

nt=nt+1;

//if(dabs(x).gt.1.d0)goto 200

if(abs(x) > 1.0) {
cerr << "x > 1.0, at i = " << i << " of " << turns << endl;
break;
}

}//for

++ntrial;
}// while

finput.close();
}
 
J

Jing Cheng

Artie said:
Jing said:
Hi,

I'm using ifstream reading data from a file, as following:


ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}

int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;

finput >> x >> xp >> z >> zp;

......
}

finput.close();


but I met a segment fault(core dumped),

then I use gdb to trace the core:
lyyang@walnut ~/research $ gdb lcsimp core
GNU gdb 6.0
Copyright 2003 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 "i686-pc-linux-gnu"...Using host
libthread_db library "/lib/libthread_db.so.1".

Core was generated by `./lcsimp'.
Program terminated with signal 11, Segmentation fault.

warning: current_sos: Can't read pathname for load map: Input/output
error

Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5...done.
Loaded symbols for
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1...done.
Loaded symbols for /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0xb7e379e3 in free () from /lib/libc.so.6
(gdb) backtrace
#0 0xb7e379e3 in free () from /lib/libc.so.6
#1 0xb7fad8e7 in operator delete(void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#2 0xb7fad939 in operator delete[](void*) () from
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#3 0xb7f576b7 in std::basic_filebuf said:
::_M_destroy_internal_buffer() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#4 0xb7f575c1 in std::basic_filebuf said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#5 0xb7f5a746 in std::basic_ifstream said:
::close() ()
from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
#6 0x080496f0 in main () at lcsimp.cpp:158
(gdb)


Here the 158th line is the end of my main().

What's the problem ?
my data file track.dat is only one line, here is the hexdump, no
special characters:

lyyang@walnut ~/research $ hexdump -C track.dat
00 31 30 30 20 2e 30 32 36 33 20 30 2e 30 20 30 2e |100 .0263 0.0 0.|
10 30 34 32 35 20 30 2e 30 |0425 0.0|
018
Your bug is quite likely elsewhere in your code; a segmentation fault in
operator delete (implemented on top of free() in your implementation) is
almost always due to a corrupted free store.

HTH,
--ag

I didn't new or allocate any memory by myself.
except the complex template library.
but my another program works fine and they are using
same code that related to the complex template
 
A

Artie Gold

Jing said:
Better to post all my code:


#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>

using namespace std;

void nonlin(double* nl, double* bn, double x, double z, double& fnlx,
double& fnlz)
{
complex<double> y, fy, aima, fact;

aima = complex<double> (0.0,1.0);

y = complex<double> (x,z);
fy=complex<double> (0.0,0.0);

for(int i = 0; i < 10; ++i){
//DO 600 m=1,10
double inl = abs(nl);
fact = complex<double> (1.0,0.0);
if (nl < 0) fact = aima;

fy += fact*bn*pow(y,inl);
// CONTINUE
}

fnlx=real(fy);
fnlz=imag(fy);
}


int main()
{
//double amux = 4.82, freqx = 0.0, amptx = 0.0, alphax = 0.0, betax
= 10.0;
//double amuz = 3.825, freqz = 0.0, amptz = 0.0, alphaz = 0.0, betaz
= 10.0;
double amux = 4.82, alphax = 0.0, betax = 10.0;
double amuz = 3.825, alphaz = 0.0, betaz = 10.0;
//double dx = 0.0, dxp = 0.0;

double anl[10], bn[10], al = 1.0, rho = 159.15;
double nl[10];
double strnl = al/rho;

anl[0] = -1.0;
bn[0] = 1.0;
nl[0] = anl[0];
for(int i = 1; i < 10; ++i){
nl = anl = bn = 0;
}

int turns = 0;
//double x(0.0263), xp(0.0), z(0.0425), zp(0.0);
double x, xp, z, zp;



double qrot, prot;

double cx, sx, cz, sz;

double emitx, emitz, phix, phiz;
double phi1;

double px, pz;

ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}


int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;
//cerr << "ntrial= " << ntrial << " turns= " << turns << "\n";

finput >> x >> xp >> z >> zp;

double am[4][4];

int nt = 0;

for(int i = 0; i < turns; ++i){

// Courant-Snyder Invariant (AP, p53, 2.93)
emitx = (1+alphax*alphax)/betax*x*x+2*alphax*x*xp+betax*xp*xp;
emitz = (1+alphaz*alphaz)/betaz*z*z+2*alphaz*z*zp+betaz*zp*zp;

// phi is in (2.86), AP, p53, 2.90, 2.91
phix = -atan2((betax*xp+alphax*x),x);
phiz = -atan2((betaz*zp+alphaz*z),z);

// p171
phi1 = phix-phiz;

px=xp*betax+x*alphax;
pz=zp*betaz+z*alphaz;

// (2.350) J_1 = J_x = 1/2*C=1/2*emitx
qrot = sqrt(emitx*betax)*cos(phi1);
prot = sqrt(emitx*betaz)*sin(phi1);

// output initial states
cout << ntrial << ' ' << nt << ' '
<< x << ' ' << xp << ' ' << z << ' ' << zp << ' '
<< qrot << ' ' << prot << endl;

//amamux = amux + (amptx*cos(M_PI*2.0*freqx*i));
//amamuz = amuz + (amptz*cos(M_PI*2.0*freqz*i));

//cerr << amamux << ' ' << amamuz << endl;

cx = cos(amux*2.0*M_PI);
sx = sin(amux*2.0*M_PI);
cz = cos(amuz*2.0*M_PI);
sz = sin(amuz*2.0*M_PI);


Notice your indices below -- I think you missed some of your porting
from Fortran. ;-) (am is declared as am[4][4] so your index is out of
range).
am[1][1] = cx+alphax*sx;
am[1][2] = betax*sx;
am[2][1] = -(1+alphax*alphax)*sx/betax;
am[2][2] = cx-alphax*sx;
am[3][3] = cz+alphaz*sz;
am[3][4] = betaz*sz;
am[4][3] = -(1+alphaz*alphaz)*sz/betaz;
am[4][4] = cz-alphaz*sz;


double xpm= am[2][1]*x+ am[2][2]*xp;
x = am[1][1]*x+ am[1][2]*xp;

double zpm= am[4][3]*z+ am[4][4]*zp;
z = am[3][3]*z+ am[3][4]*zp;

double fnlx, fnlz;
nonlin(nl, bn, x, z, fnlx, fnlz);

xp = xpm+strnl*fnlx;
zp = zpm-strnl*fnlz;

nt=nt+1;

//if(dabs(x).gt.1.d0)goto 200

if(abs(x) > 1.0) {
cerr << "x > 1.0, at i = " << i << " of " << turns << endl;
break;
}

}//for

++ntrial;
}// while

finput.close();
}

HTH,
--ag
 
J

Jing Cheng

Artie said:
Jing said:
Better to post all my code:


#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>

using namespace std;

void nonlin(double* nl, double* bn, double x, double z, double& fnlx,
double& fnlz)
{
complex<double> y, fy, aima, fact;

aima = complex<double> (0.0,1.0);

y = complex<double> (x,z);
fy=complex<double> (0.0,0.0);

for(int i = 0; i < 10; ++i){
//DO 600 m=1,10
double inl = abs(nl);
fact = complex<double> (1.0,0.0);
if (nl < 0) fact = aima;

fy += fact*bn*pow(y,inl);
// CONTINUE
}

fnlx=real(fy);
fnlz=imag(fy);
}


int main()
{
//double amux = 4.82, freqx = 0.0, amptx = 0.0, alphax = 0.0,
betax = 10.0;
//double amuz = 3.825, freqz = 0.0, amptz = 0.0, alphaz = 0.0,
betaz = 10.0;
double amux = 4.82, alphax = 0.0, betax = 10.0;
double amuz = 3.825, alphaz = 0.0, betaz = 10.0;
//double dx = 0.0, dxp = 0.0;

double anl[10], bn[10], al = 1.0, rho = 159.15;
double nl[10];
double strnl = al/rho;

anl[0] = -1.0;
bn[0] = 1.0;
nl[0] = anl[0];
for(int i = 1; i < 10; ++i){
nl = anl = bn = 0;
}

int turns = 0;
//double x(0.0263), xp(0.0), z(0.0425), zp(0.0);
double x, xp, z, zp;



double qrot, prot;

double cx, sx, cz, sz;

double emitx, emitz, phix, phiz;
double phi1;

double px, pz;

ifstream finput("track.dat");

if(finput.fail()){
cerr << "Open input DATA file error!\n";
exit(-1);
}


int ntrial = 0;

while(finput >> turns){

if(turns < 1) continue;
//cerr << "ntrial= " << ntrial << " turns= " << turns << "\n";

finput >> x >> xp >> z >> zp;

double am[4][4];

int nt = 0;

for(int i = 0; i < turns; ++i){

// Courant-Snyder Invariant (AP, p53, 2.93)
emitx =
(1+alphax*alphax)/betax*x*x+2*alphax*x*xp+betax*xp*xp;
emitz =
(1+alphaz*alphaz)/betaz*z*z+2*alphaz*z*zp+betaz*zp*zp;

// phi is in (2.86), AP, p53, 2.90, 2.91
phix = -atan2((betax*xp+alphax*x),x);
phiz = -atan2((betaz*zp+alphaz*z),z);

// p171
phi1 = phix-phiz;

px=xp*betax+x*alphax;
pz=zp*betaz+z*alphaz;

// (2.350) J_1 = J_x = 1/2*C=1/2*emitx
qrot = sqrt(emitx*betax)*cos(phi1);
prot = sqrt(emitx*betaz)*sin(phi1);

// output initial states
cout << ntrial << ' ' << nt << ' '
<< x << ' ' << xp << ' ' << z << ' ' << zp << ' '
<< qrot << ' ' << prot << endl;

//amamux = amux + (amptx*cos(M_PI*2.0*freqx*i));
//amamuz = amuz + (amptz*cos(M_PI*2.0*freqz*i));

//cerr << amamux << ' ' << amamuz << endl;

cx = cos(amux*2.0*M_PI);
sx = sin(amux*2.0*M_PI);
cz = cos(amuz*2.0*M_PI);
sz = sin(amuz*2.0*M_PI);



Notice your indices below -- I think you missed some of your porting
from Fortran. ;-) (am is declared as am[4][4] so your index is out of
range).


Woh !!!!
How do you know I'm porting it from fortran ?
You are right!
I should be carefull to these difference between C and FORTRAN.

Thank you very much !

Jing Cheng
 
A

Artie Gold

Jing said:
Artie said:
Jing said:
Better to post all my code:


#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>

using namespace std;

void nonlin(double* nl, double* bn, double x, double z, double& fnlx,
double& fnlz)
{
complex<double> y, fy, aima, fact;

aima = complex<double> (0.0,1.0);

y = complex<double> (x,z);
fy=complex<double> (0.0,0.0);

for(int i = 0; i < 10; ++i){
//DO 600 m=1,10
double inl = abs(nl);
fact = complex<double> (1.0,0.0);
if (nl < 0) fact = aima;

fy += fact*bn*pow(y,inl);
// CONTINUE
}

fnlx=real(fy);
fnlz=imag(fy);
}
[snip]


Woh !!!!
How do you know I'm porting it from fortran ?
You are right!
I should be carefull to these difference between C and FORTRAN.

Thank you very much !

Jing Cheng


The leftover `DO' and `CONTINUE' above were dead giveaways. ;-)
(Not t mention the `1'-based arrays.)

Cheers,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,821
Messages
2,569,748
Members
45,726
Latest member
RaleighAll

Latest Threads

Top