ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I try to declare a large matrix type or vector type, say a matrix of 3000*3, or a vector 3000*1 something like that, when running the program it reported the segmentation fault. When I try a small matrix like 10*3 then it's fine.
Does anyone know why it is please? Is it because of my previlidge?
The matrix and vector type I used are from the numerical recipes.
You didn't provide much detail so I can only guess but one cause may be that you're trying to store this huge matrix on the stack, which is not big enough to hold it. Allocate it on the heap instead using new.
template <class T>
class NRVec{
private:
int nn; // size of array. upper index is nn-1
T *v;
public:
NRVec();
explicit NRVec(int n); // zero-based array
NRVec(const T &a, int n); // initialize to constant value
NRVec(const T *a, int n); // initialize to array
NRVec(const NRVec &rhs); // copy constructor
NRVec & operator=(const NRVec &rhs); //assignment
NRVec & operator=(const T &a); // assign a to every element
inline T & operator[](const int i); // i'th element
inline const T & operator[](const int i) const;
inline int size()const;
~NRVec();
};
And I have built them into a library.
In another project, I invoke the NRVec class like this.
Code:
typedef NRVec<float> Vec_SP;
.
.
.
In some function, I declare an array lie
Vec_SP myarray(3000);
Then it failed. It only work if I declare a very small array like
Vec_SP myarray(10);
Hmm, it compiles and runs fine on my amd64 (runs fine when compiled as 64bit and 32bit (with -m32 flag)). The code looks good, have you tried it on another system? If so, did it work? If not, what compiler are you using? If so, what are your specs?
Also, could you email me the entire source file? I want to try compiling your exact code. s_troz <at> yahoo <dot> ca
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.