LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2007, 10:39 AM   #1
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Rep: Reputation: 15
C++ segmentation fault


Hi all:

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.

Any help would be appreciated!!!

Thanks in advance
 
Old 12-05-2007, 10:47 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 12-05-2007, 11:17 AM   #3
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
Can you post the relevant code?
 
Old 12-05-2007, 12:42 PM   #4
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
Yeah, here is the code:


Code:
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);
 
Old 12-05-2007, 12:48 PM   #5
Asuralm
LQ Newbie
 
Registered: Nov 2007
Posts: 26

Original Poster
Rep: Reputation: 15
Code:
template<class T>
NRVec<T>::NRVec() : nn(0), v(0) {}

template<class T>
NRVec<T>::NRVec(int n) : nn(n), v(new T[n]){}

template <class T>
NRVec<T>::NRVec(const T& a, int n) : nn(n), v(new T[n])
{
    for(int i =0; i < n; i++)
        v[i] = a;
}

template <class T>
NRVec<T>::NRVec(const T *a, int n) : nn(n), v(new T[n])
{
    for(int i =0; i<n; i++)
        v[i] = *a++;
}

template <class T>
NRVec<T>::NRVec(const NRVec<T> &rhs) : nn(rhs.nn), v(new T[nn])
{
    for(int i =0; i<nn; i++)
        v[i] = rhs[i];
}
Here is the definition of the NRVec class. I got this from the numerical recipes.
 
Old 12-05-2007, 05:09 PM   #6
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
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

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault vm_devadas Linux - Hardware 1 12-06-2006 09:52 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault tejas15_10 Linux - Newbie 1 06-14-2005 04:44 AM
Segmentation fault? JiggaJerry Fedora 10 01-16-2005 04:01 PM
segmentation fault sakkie Linux - Security 7 03-26-2004 11:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration