0

This code gives the segmantation fault Is the constructor called when assigning array to template specifically in this program? Please explain in detail if the constructor is called why is segmantation fault? if not called isn't this the syntax error

#include <iostream>
using namespace std;
const int size=3;
template<class T>
class vector{
    T *v;
    public:
    vector(){
        v = new T[size];
        for(int i=0;i<size;i++)
        {
            v[i]=0;
        }
    }
    vector(T *a){
        for (int i=0;i<size;i++)
        {
            v[i]=a[i];
        }
    }
    T operator*(vector &v){
        T sum=0;
        for (int i=0;i<size;i++)
        {
            sum+=this->v[i]*v.v[i];
        }
        return sum;
    }
    void display(){
        for (int i=0;i<size;i++)
        {
            cout << v[i] <<"\t";
        }
    }

};
int main()
{
    int x[3]={1,3,5};
    int y[3]={2,4,6};
    vector<int> v1;
    vector<int> v2;
    v1=x;
    v2=y;
    v1.display();
    cout<<endl;
    v2.display();
    return 0;
}
  • 3
    This [doesn't even compile](https://godbolt.org/z/tJrkUQ) because of `using namespace std;`. Seems like a bad book. – Ted Lyngmo Nov 26 '19 at 11:23
  • 2
    `using namespace std;` + `class vector` is reason enough to never open the book again – 463035818_is_not_an_ai Nov 26 '19 at 11:24
  • 6
    First *terrible* name for a class, *especially* when slurping in the entire `std` namespace, [which you shouldn't do](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). By all means, tell us the name of the book and author. I need to know if I have any of that trash in my library. – WhozCraig Nov 26 '19 at 11:24
  • 6
    If this is code from a book, I suggest you re-purpose the book for something other than learning C++. – StoryTeller - Unslander Monica Nov 26 '19 at 11:24
  • 1
    The constructor from `T*` does not allocate any memory. So `v` is not a valid memory address and dereferencing it by `v[i]` and assigning to it causes the segfault. – n314159 Nov 26 '19 at 11:25
  • 2
    please post real code, this cannot cause a segfault, because it does [not compile](https://wandbox.org/permlink/c0Y3YMFJtJtbtnB7) – 463035818_is_not_an_ai Nov 26 '19 at 11:26
  • After I cleaned up the 50 errors and warnings, and added `v = new T[Xsize];` to the second constructor that wasn't initializing `v`, it worked for me. Is this one of those books that teaches by giving bad code and the student has to correct the code so that it works? I recommend turning on all warnings, and using a debugger. – Eljay Nov 26 '19 at 12:51
  • @Sandeep Here's a half-hearted attempt at salvaging what can be salvaged from that code: https://godbolt.org/z/Dm2Gwp It should give you a slightly better base for your explorations. It requires C++17 so I hope you have a C++17 compiler. – Ted Lyngmo Nov 26 '19 at 15:16

1 Answers1

2

Apart from all the awful code, there's a problem with:

int x[3]={1,3,5};
...
vector<int> v1(x);

which calls:

template<class T>
class vector{
    T *v;
    ...
    public:
    vector(T *a){
        for (int i=0;i<size;i++)
        {
            v[i]=a[i];
        }
    }
    ...

because pointer v hasn't been assigned any memory. So the first time through the for loop: v[0] = a[0]; will cause a segmentation fault.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54