Allocating memory for an object twice

P

prasoonthegreat

#include<iostream>
using std::cout;
using std::endl;

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=0;
cout<<"Hello 1";
}

vector(T *a)
{
//v=new T[size];
for(int i=0;i<size;i++)
v=a;

cout<<"Hello";
}

/*void operator=(const T *a)
{
for(int i=0;i<size;i++)
v=a;
}*/

T operator*(const vector &y)
{
T prod=0;

for(int i=0;i<size;i++)
prod += this->v * y.v;

return prod;
}
};

int main()
{
int x[3]={1,2,3};
int y[3]={4,5,6};

float a[3]={1.7,2.3,3.2};
float b[3]={1.9,2.4,3.8};



vector <int> v1;
vector <int> v2;

vector <float> v3;
vector <float> v4;

v1=x;
v2=y;

v3=a;
v4=b;

int Prod_Int = v1 * v2;

float Prod_Float = v3 * v4;

cout<<"Prod_Int = "<<Prod_Int<<endl;
cout<<"Prod_Float = "<<Prod_Float<<endl;


return 0;
}

why m i getting seg fault
 
V

Victor Bazarov

#include<iostream>
using std::cout;
using std::endl;

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=0;


It's easier just to do

v=new T[size]();

(note the parentheses), and the array will be zero-initialised.
cout<<"Hello 1";
}

vector(T *a)
{
//v=new T[size];
for(int i=0;i<size;i++)
v=a;


What if the pointer points to an array that is smaller than 'size'? You
would get *undefined behaviour*!
cout<<"Hello";
}

/*void operator=(const T *a)
{
for(int i=0;i<size;i++)
v=a;
}*/

T operator*(const vector &y)
{
T prod=0;

for(int i=0;i<size;i++)
prod += this->v * y.v;

return prod;
}
};

[..]

why m i getting seg fault


Read about "The Rule of Three".

V
 
P

prasoonthegreat

#include<iostream>
using std::cout;
using std::endl;
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=0;


It's easier just to do

         v=new T[size]();

(note the parentheses), and the array will be zero-initialised.
       cout<<"Hello 1";
   }
    vector(T *a)
    {
           //v=new T[size];
        for(int i=0;i<size;i++)
          v=a;


What if the pointer points to an array that is smaller than 'size'?  You
would get *undefined behaviour*!




   cout<<"Hello";
   }
   /*void operator=(const T *a)
   {
       for(int i=0;i<size;i++)
          v=a;
    }*/

    T operator*(const vector &y)
    {
        T prod=0;
        for(int i=0;i<size;i++)
           prod += this->v * y.v;

        return prod;
    }
};

why m i getting seg fault

Read about "The Rule of Three".

V


In the above code....although memory has been allocated to object v1
in the default constructor ......why do we need to allocate it again
in the parametrized constructor????...otherwise i am getting a seg
fault
 
V

Victor Bazarov

[..]
In the above code....although memory has been allocated to object v1
in the default constructor ......why do we need to allocate it again
in the parametrized constructor????...otherwise i am getting a seg
fault

Two different constructors construct two different objects. The object
v1 is constructed using the default c-tor. Why would another c-tor be
called for the same object? It wouldn't. So, a different object is
constructed. What constructor is used for it? The parameterized one,
most likely. That constructor looks like this:

vector(T *a)
{
//v=new T[size];
for(int i=0;i<size;i++)
v=a;

cout<<"Hello";
}

If you don't allocate memory for 'v', where do you copy the values from
the array?

V
 
A

Arne Mertz

Victor said:
#include<iostream>
using std::cout;
using std::endl;

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=0;


It's easier just to do

v=new T[size]();

(note the parentheses), and the array will be zero-initialised.
cout<<"Hello 1";
}

vector(T *a)
{
//v=new T[size];
for(int i=0;i<size;i++)
v=a;


What if the pointer points to an array that is smaller than 'size'? You
would get *undefined behaviour*!


Even IF a pointed to an array that is big enough this is the point where
the segfault occurs: we are in the constructor here, so since there is
no initialization list v might contain any unspecified value because it
is an uninitialized POD. So most likely you don't need to worry about
the size of the array a is pointing to because yet in the first
iteration the assignment tries to access the memory v is pointing to
which most likely is not yours so you get your segfault.
cout<<"Hello";
}

/*void operator=(const T *a)
{
for(int i=0;i<size;i++)
v=a;
}*/

T operator*(const vector &y)
{
T prod=0;

for(int i=0;i<size;i++)
prod += this->v * y.v;

return prod;
}
};

[..]

why m i getting seg fault


Read about "The Rule of Three".

V
 
P

prasoonthegreat

well i have done as you said

v=new T[size]();

but i am not getting the array zero initialized......
any solution for that
 
A

Arne Mertz

i am using g++ (Dev C++ 4.9.9.2)......
any better compiler???

Dev C++ is rather old and no longer in development since early 2005.
So if you still are using the gcc you got with Dev C++ it will be
quite old.
I'd recommend to update the compiler (there's nothing wrong about
gcc in general) and perhaps change the IDE. e.g. Code::Blocks is
said to be similar to Dev C++, a list of different IDEs can be found
here:
http://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments

greets
 
A

Alf P. Steinbach

* Arne Mertz:
Dev C++ is rather old and no longer in development since early 2005. So
if you still are using the gcc you got with Dev C++ it will be quite old.
I'd recommend to update the compiler (there's nothing wrong about gcc in
general) and perhaps change the IDE. e.g. Code::Blocks is said to be
similar to Dev C++, a list of different IDEs can be found here:
http://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments

Thanks for link.

But I miss one IDE there, although I haven't used it. And I can't recall the
name. But it's basically originally a GUI library, then an IDE was made.


Cheers,

- Alf (amnesiac)
 
V

Victor Bazarov

Alf said:
* Arne Mertz:


Thanks for link.

But I miss one IDE there, although I haven't used it. And I can't recall
the name. But it's basically originally a GUI library, then an IDE was
made.

CodeWarrior isn't listed (or I can't spot it).

V
 
A

Alf P. Steinbach

* Victor Bazarov:
CodeWarrior isn't listed (or I can't spot it).

Ah. It seems it's a guy called Pastrami who's out to save the world a lot of
pain by removing IDEs he particularly doesn't like, as soon as they're added.
Then, when reference to something has been removed thrice, there's some
Wikipedia guideline that makes it a Real Effort to add again.

Cheers,

- Alf
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top