C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Segmentation faultwhy???

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Cristian
Guest





PostPosted: Wed May 10, 2006 12:21 am    Post subject: Segmentation faultwhy??? Reply with quote



OGGETTO: Segmentation fault…why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault

I have also made tests on other bases (Ubuntu) but I have always met
the same problem
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!

On WindowsXP32Bit it works ok also for dim1=dim2=10000 for example.


#include <vector>
#include <iostream>
#include <cmath>
#include <string>
#include <time.h>
using namespace std;

int main( ) {
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];

for ( i=0,n=0 ; i < dim1 ; i++ ) {

for ( j=0 ; j < dim2 ; j++) {

m[i][j] = (double) ( (j+1) + 3.0 *1/(j+1)*(j+1)*(j+1)*(j+1) + 50*
(i+1) );

}
}

cout << "v(" << 100 << "," << 99 << ")=" << m[99][98] << endl;

}




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
red floyd
Guest





PostPosted: Wed May 10, 2006 1:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote



Cristian wrote:
Quote:
OGGETTO: Segmentation fault…why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault


We've told you already. You're probably overflowing your stack, by
putting 8MB (or 80MB for the 10000 case) onto the stack.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
rolkA
Guest





PostPosted: Wed May 10, 2006 7:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote



Cristian <cristian (AT) yahoo (DOT) com> wrote in
news:4t3162psocq5j0268s080dnvs6n7rtjcht (AT) 4ax (DOT) com:

Quote:
OGGETTO: Segmentation fault
why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault


My guess is that you use too memory for the stack.
Try using the heap instead by:
- using a std::vector
- or allocating dynamically


--
Sam / rolkA

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

Cristian wrote:
Quote:
OGGETTO: Segmentation fault?why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault

I have also made tests on other bases (Ubuntu) but I have
always met the same problem It perfectly works if I reduce the
dimension of dim1 to 1000 !!!!!!

Which strongly suggests that you are exceeding the stack limits
on your machine. You did set the stack limits high enough,
didn't you? ("ulimit -s unlimited" on most Unix and
Unix-look-alikes).

Quote:
On WindowsXP32Bit it works ok also for dim1=dim2=10000 for example.

#include <vector
#include <iostream
#include <cmath
#include <string
#include <time.h
using namespace std;

int main( ) {
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];

Which fails to compile with a C++ compiler -- array dimensions
must be compile time constants. (G++ is not, by default, a C++
compiler. For it to even attempt to compile C++, you need the
-pendantic -std=c++98 options. And when those are given, the
code doesn't compile.)

Quote:
for ( i=0,n=0 ; i < dim1 ; i++ ) {

My compilers all complain about n not being defined here as
well.

In C++, it would be far more idiomatic to not declare i (and j)
before the loop, but rather in the for statement, e.g.:

for ( size_t i = 0 ; i < dim1 ; ++ i ) {
for ( size_t j = 0 ; j < dim2 ; ++ j ) {
// ...

Quote:
for ( j=0 ; j < dim2 ; j++) {

m[i][j] = (double) ( (j+1) + 3.0 *1/(j+1)*(j+1)*(j+1)*(j+1) + 50*
(i+1) );

}
}

cout << "v(" << 100 << "," << 99 << ")=" << m[99][98] << endl;

}

After declaring dim1 and dim2 as long, and removing the
initialization of n -- and, of course, setting ulimit to an
unlimited stack size, it works on every machine I have access
to. Still... I'd be very leary of using variables of that size
on the stack. The problem is that stack overflow is
undefined behavior -- you can't do much about it. If you used
dynamic allocation (e.g. std::vector), you will get an error you
can trap and handle if there is not enough memory. (Supposing
your OS supports this -- in the past, many Linux were delivered
with support for a working malloc/operator new turned off.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Sebastian Redl
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

Cristian wrote:

Quote:
#include <vector
#include <iostream
#include <cmath
#include <string
#include <time.h

Why not <ctime>?

Quote:
using namespace std;

int main( ) {
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];

Declaring an array with non-const bounds is invalid and does not compile.
In addition, if it did compile, the array would be (assuming an 8-byte
double, as is typical) 80 megabytes large, and allocated on the stack. Many
operating systems will not allow this, including Windows and, I think,
Linux.

Quote:

for ( i=0,n=0 ; i < dim1 ; i++ ) {

n is not declared. It's not used, either.

Quote:

for ( j=0 ; j < dim2 ; j++) {

m[i][j] = (double) ( (j+1) + 3.0 *1/(j+1)*(j+1)*(j+1)*(j+1) + 50*
(i+1) );

No need for the cast (and it should be a static_cast anyway), but you should
really think about operator precedence and types within the expression
here. In particular, the entire middle expression will always yield 0.


In conclusion, I suspect the segfault is a stack overflow. I'm really
surprised that it runs in Windows, as you say it does.
Aside from that, there's still a lot of stuff wrong.

--
Sebastian Redl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gene Bushuyev
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

"Cristian" <cristian (AT) yahoo (DOT) com> wrote in message
news:4t3162psocq5j0268s080dnvs6n7rtjcht (AT) 4ax (DOT) com...
Quote:
OGGETTO: Segmentation fault.why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault

I have also made tests on other bases (Ubuntu) but I have always met
the same problem
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!

On WindowsXP32Bit it works ok also for dim1=dim2=10000 for example.


#include <vector
#include <iostream
#include <cmath
#include <string
#include <time.h
using namespace std;

int main( ) {
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];


C++ doesn't have dynamic arrays; dim1 and dim2 must be constants. Assuming
that's fixed, the above line tries to create about 80MB stack frame. On Windows
stack size is defined by linker. Check the linker options for that. It can also
be changed by EDITBIN /STACK utility. On Unix, check unlimit/limit commands.
Other platforms may have other ways of controlling program stack, you need to
read documentation.
Quote:
From a practical standpoint, it's not a good idea to make stack too big. It's
better for performance if your program stack can fully fit into a processor

cache. Instead use heap for large objects:
double* m = new double[dim1 * dim2];

--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
ThosRTanner
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

Cristian wrote:
Quote:
OGGETTO: Segmentation fault...why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault


Knowing /where/ your program segfaulted would be helpful - debugging
the resulting core file and doing 'where' would show you which line.

Quote:
I have also made tests on other bases (Ubuntu) but I have always met
the same problem
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!

On WindowsXP32Bit it works ok also for dim1=dim2=10000 for example.


#include <vector
#include <iostream
#include <cmath
#include <string
#include <time.h
using namespace std;

int main( ) {
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];
That is a flipping big array. 10 million doubles is around 80Mb.


I'd reckon you have a stack overflow going on here. I'm not sure what
the default stack size is on linux, but I'd be surprised if it was THAT
big.

Quote:

for ( i=0,n=0 ; i < dim1 ; i++ ) {

for ( j=0 ; j < dim2 ; j++) {

m[i][j] = (double) ( (j+1) + 3.0 *1/(j+1)*(j+1)*(j+1)*(j+1) + 50*
(i+1) );

}
}

cout << "v(" << 100 << "," << 99 << ")=" << m[99][98] << endl;

}



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
eden
Guest





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

Cristian wrote:
Quote:
OGGETTO: Segmentation fault...why???
I work on Linux system Fedora Core 5 x86_64
Why this ( you see under) program doesn't work ? The program
interrupts him visualizing as error:
segmentation fault

I have also made tests on other bases (Ubuntu) but I have always met
the same problem
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!

On WindowsXP32Bit it works ok also for dim1=dim2=10000 for example.

[...]

long dim1=10000, dim2=1000 ;
double m[dim1][dim2];


I am not sure about the segmentation fault, but It is interesting how
you could dimension the array that way, without constant expresions.

Goran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Eugene Alterman
Guest





PostPosted: Thu May 11, 2006 1:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

"Cristian" <cristian (AT) yahoo (DOT) com> wrote in message
news:4t3162psocq5j0268s080dnvs6n7rtjcht (AT) 4ax (DOT) com...
Quote:
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];

This is not a valid C++ code.
C++ does not support variable size arrays as C99 does.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Cristian
Guest





PostPosted: Fri May 12, 2006 12:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

On 11 May 2006 19:09:02 -0400, "kanze" <kanze@gabi-soft.fr> wrote:

Quote:
Sebastian Redl wrote:


Quote:
In sum, the expression is (almost) the same as the much simpler:
3.0 * (j+1) * (j+1 )
and the fact that he didn't write it this was suggests that it
isn't actually the expression he wanted. I vaguely suspect that
what he was looking for was something like:
3.0 / pow( j+1, 4 )
But I'm not really sure -- that's so much simpler than what he
wrote as well, it's surprising that he wouldn't use it.

Yes, I want to perform the following calculation:
3.0 / pow( j+1, 4 )

The problem is however that I come from the windows'world and in
windows the use of pow() provoked me notable decelerations and at
times problems of overflow on these calculations (great matrixes.
Dimensions: 20000 x 20000 double)

In windows the management of the stack is probably different
considering that up to coarsely 10000x10000 I succeeded in compiling
without problems.
I have resolved using std::vector
The problem is that using
const long dim1=...,dim2=...;
<vector<vector< double>> m(dim1,vector<double>(dim2) )
I get strange behaviors as it regards the speed :
for example:
m--> 10^4 * 10^4 run-time of the calculation: 3 sec
"-> 20^4 * 10^4 " : more than 100 sec
Why?
in windows I get a doubling of the run-time instead in reference to
the said example above


Thanks to every all
Cristian

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Sat May 13, 2006 1:21 am    Post subject: Re: Segmentation faultwhy??? Reply with quote

Cristian wrote:
Quote:
On 11 May 2006 19:09:02 -0400, "kanze" <kanze@gabi-soft.fr> wrote:

Sebastian Redl wrote:

In sum, the expression is (almost) the same as the much
simpler:
3.0 * (j+1) * (j+1 )
and the fact that he didn't write it this was suggests that
it isn't actually the expression he wanted. I vaguely
suspect that what he was looking for was something like:
3.0 / pow( j+1, 4 )
But I'm not really sure -- that's so much simpler than what
he wrote as well, it's surprising that he wouldn't use it.

Yes, I want to perform the following calculation:
3.0 / pow( j+1, 4 )
The problem is however that I come from the windows'world and in
windows the use of pow() provoked me notable decelerations and at
times problems of overflow on these calculations (great matrixes.
Dimensions: 20000 x 20000 double)

Curious. I would expect the speed to be roughly the same, and I
certainly cannot see the results overflowing unless you were
picking up a pow(int,int) as an extension; casting the first
argument to double before the call should have fixed that. (In
earlier times, floating point on Intel machines was very slow,
and offering such functions as an extension made sense. Today,
I think the Intel processors are like most others, with floating
point as fast, if not faster, than integral arithmetic.)

Quote:
In windows the management of the stack is probably different

It is. But I don't know too much about it.

Quote:
considering that up to coarsely 10000x10000 I succeeded in
compiling without problems.
I have resolved using std::vector

For a two dimensional array, I'm not sure that that is the ideal
solution -- unless you wrap it in a class which allows [][].
(The first [] is a member of the class, and returns a helper
object -- double* actually works fine -- which supports the
second [].)

Quote:
The problem is that using
const long dim1=...,dim2=...;
vector<vector< double>> m(dim1,vector<double>(dim2) )
I get strange behaviors as it regards the speed :

I can imagine -- for a 20000x20000 matrice, you're doing 200001
allocations (plus the initializations).

Quote:
for example:
m--> 10^4 * 10^4 run-time of the calculation: 3 sec
"-> 20^4 * 10^4 " : more than 100 sec
Why?

Well, you've doubled the number of allocations. In all likely
hood, you've stumbled across some undocumented internal boundary
which has led to the initialisations not being more or less
contiguous, have ended up with bad locality, and have started
paging.

Quote:
in windows I get a doubling of the run-time instead in
reference to the said example above

It depends on a lot of external factors, over which you have
very little control. Using std::vector is, per se, a good idea,
but I'd allocate a single large array, and do the vector
arithmetic myself. Something like:

class Matrix
{
public:
Matrix( long dim1, long dim2 )
: myDim1( dim1 )
, myDim2( dim2 )
, myData( dim1 * dim2 )
{
}

double* operator[]( long i )
{
return &myData[ 0 ] + (j * myDim1) ;
}
// ...
private:
long myDim1 ;
long myDim2 ;
std::vector< double > myData ;
}

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Cristian
Guest





PostPosted: Sat May 13, 2006 12:21 pm    Post subject: Re: Segmentation faultwhy??? Reply with quote

On 12 May 2006 07:47:23 -0400, Cristian <cristian (AT) yahoo (DOT) com> wrote

Quote:
I get strange behaviors as it regards the speed :
for example:
m--> 10^4 * 10^4 run-time of the calculation: 3 sec
"-> 20^4 * 10^4 " : more than 100 sec
There is an error, I apologize. The second calculation is obviously on

a matrix of dimensions : dim1(= 2* 10^4 ) * dim2 (=10^4)

Thanks
Cristian

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Otis Bricker
Guest





PostPosted: Wed May 17, 2006 2:21 am    Post subject: Re: Segmentation faultwhy??? Reply with quote

"kanze" <kanze@gabi-soft.fr> wrote in news:1147446737.039642.306680
@j33g2000cwa.googlegroups.com:

Quote:
for example:
m--> 104 * 104 run-time of the calculation: 3 sec
"-> 204 * 104 " : more than 100 sec
Why?

Well, you've doubled the number of allocations. In all likely
hood, you've stumbled across some undocumented internal boundary
which has led to the initialisations not being more or less
contiguous, have ended up with bad locality, and have started
paging.



Unless I am missing some of the math here, 2*104 * 104 - 2*108. So for
doubles(8 bytes) that would require 1.6*109 Unless your machine has >2G
I expct you will be have started swapping to the pagefile on disk.

Otis

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.