Segmentation Fault ...why ?? I implore you helped me !!!!

C

Cristian

I work on Linux system Fedora Core 5 x86_64
Why this program doesn't work?
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!


#include <vector>
#include <iostream>
#include <cmath>

#include <string>
#include <time.h>

using namespace std;
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[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;


Thanks in advance
 
V

Victor Bazarov

Cristian said:
I work on Linux system Fedora Core 5 x86_64

That shouldn't matter (at least here in c.l.c++).
Why this program doesn't work?

It's not a program. Not one that would compile, anyway.
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!


#include <vector>
#include <iostream>
#include <cmath>

#include <string>
#include <time.h>

using namespace std;
long i, j ;
long dim1=10000, dim2=1000 ;
double m[dim1][dim2];

In C++ this should only compile if 'dim1' and 'dim2' are both declared
_const_. And even then, you seem to declare a _static_ array of size
about 80 Mebibytes. Perhaps your compiler/platform can't handle it.
The fact that when you reduce it to 8 MB it works only strengthens my
conviction. Memory of that size should most likely be allocated from
the free store (dynamically).
for ( i=0,n=0 ; i < dim1 ; i++ ) {

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



m[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;


V
 
R

red floyd

Victor said:
Cristian said:
double m[dim1][dim2];

In C++ this should only compile if 'dim1' and 'dim2' are both declared
_const_. And even then, you seem to declare a _static_ array of size
about 80 Mebibytes.

Actually, Victor, assuming an 8 byte double, it's 80 *Megabytes*. it's
10000 * 1000, not 10*1024*1024.
 
V

Victor Bazarov

red said:
Victor said:
Cristian said:
double m[dim1][dim2];

In C++ this should only compile if 'dim1' and 'dim2' are both
declared _const_. And even then, you seem to declare a _static_
array of size about 80 Mebibytes.

Actually, Victor, assuming an 8 byte double, it's 80 *Megabytes*. it's
10000 * 1000, not 10*1024*1024.

That's why I said "about"...
 
C

Cristian

In C++ this should only compile if 'dim1' and 'dim2' are both declared
_const_. And even then, you seem to declare a _static_ array of size
about 80 Mebibytes.

I apologize you, but I am not an expert of the language c. I don't
succeed in understanding wath is the problem . We are speaking of 80
MB and also in the case of an operating system to 32 bit I should not
have problems to declare an such static array
On windows I have succeeded in actually compiling it to dim1=10000
and dim2=10000 without any problem


Perhaps your compiler/platform can't handle it.
The fact that when you reduce it to 8 MB it works only strengthens my
conviction. Memory of that size should most likely be allocated from
the free store (dynamically).

Because it should be best to dynamically memorize?

Thanks in advance
Cristian
 
V

Victor Bazarov

Cristian said:
I apologize you, but I am not an expert of the language c. I don't
succeed in understanding wath is the problem . We are speaking of 80
MB and also in the case of an operating system to 32 bit I should not
have problems to declare an such static array
On windows I have succeeded in actually compiling it to dim1=10000
and dim2=10000 without any problem

Depending on where your definition appears, it could be OK or it could
be a horrible toll on your program's resources. Avoid allocating large
arrays as _automatic_ variables. Static maybe OK. All depends on the
platform.
Because it should be best to dynamically memorize?

Yes, if I understood what you meant here.

V
 
C

Cristian

Depending on where your definition appears, it could be OK or it could
be a horrible toll on your program's resources. Avoid allocating large
arrays as _automatic_ variables. Static maybe OK. All depends on the
platform.

I have to conclude a matrix calculation on great matrixes
(dim1=10000,dim2=30000). Do you have an idea of as I can modify the
program that I have pointed out?

Yes, if I understood what you meant here.

I Apologize you for my non good English
I thank you for heart for the help

Cristian
 
V

Victor Bazarov

Cristian said:
I have to conclude a matrix calculation on great matrixes
(dim1=10000,dim2=30000). Do you have an idea of as I can modify the
program that I have pointed out?

There is a section on dynamic allocation of arrays in the FAQ. You
can find the FAQ here: http://www.parashift.com/c++-faq-lite/

Beyond that, I recommend looking for a ready-to-wear solution on the
Web or in a book.

V
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Cristian said:
I work on Linux system Fedora Core 5 x86_64
Why this program doesn't work?
It perfectly works if I reduce the dimension of dim1 to 1000 !!!!!!


#include <vector>
#include <iostream>
#include <cmath>

#include <string>
#include <time.h>

using namespace std;
long i, j ;
long dim1=10000, dim2=1000 ;

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

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

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



m[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;


Thanks in advance


/S
 
C

Cristian

Stefan said:
const long dim1=10000, dim2=1000;


vector<vector<double> > m(dim1, vector<double>(dim2));


/S


Thanks Thanks Thanks Stefan.!!!!!!!!...I thank you for heart for the help

When I compile with C++ OK!!!!
When I use an interpreter (CIN. Root of Cern) I receive an error:
Illegal type cast(2) _vector.h:80: Why?


Thanks in advanced
 
V

Victor Bazarov

Cristian said:
[..]
When I compile with C++ OK!!!!
When I use an interpreter (CIN. Root of Cern) I receive an error:
Illegal type cast(2) _vector.h:80: Why?

Because that interpreter is incapable of working with standard templates,
maybe? It's not really on topic here. Perhaps you should ask those who
wrote that interpreter...

V
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Cristian said:
Thanks Thanks Thanks Stefan.!!!!!!!!...I thank you for heart for the help
Welcome!

When I compile with C++ OK!!!!
When I use an interpreter (CIN. Root of Cern) I receive an error:
Illegal type cast(2) _vector.h:80: Why?

I Don't know.
What is '_vector.h' ? Does it define a Standard compliant std::vector<> ?

/S
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Cristian said:
Thanks Thanks Thanks Stefan.!!!!!!!!...I thank you for heart for the help

When I compile with C++ OK!!!!
When I use an interpreter (CIN. Root of Cern) I receive an error:
Illegal type cast(2) _vector.h:80: Why?


From http://root.cern.ch/root/Cint.phtml?limitations :

<quote>
+ STL
Cint source package comes with an old STL (HP reference implementation)
which is in stl directory. You can interpret string, vector and list
containers and several generic algorithms.
Small subset of precompiled STL containers can be built under lib/dll_stl
directory. Read lib/dll_stl/README.txt and run setup script there. You
can precompile STL containers by yourself too. Simple example is in
demo/makecint/stl directory. STL dummy headers are in lib/prec_stl
directory.
Please understand that there are many limitations for using STL on Cint.
STL is so complex that it is very difficult to document all limitations.
</quote>

/S
 
C

Cristian

Stefan said:
<quote>
+ STL
............
> Please understand that there are many limitations for using STL on Cint.
STL is so complex that it is very difficult to document all limitations.
</quote>

/S
OK Thanks,
I bow to yours help and generosity

Cristian
 
C

Cristian

I have resolved using std::vector (Stefan Nawe' advice)
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--> dim1=10^4 dim2=10^4 run-time of the calculation: 3 sec
" -> dim1=2*10^4 dim2=10^4 " : more than 100 sec

Why?

In windowsXP (VisualC++.Net) I get a doubling of the run-time instead in
reference to the said example above


Thanks to every all
Cristian
 

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
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top