Allocating memory for an object twice

Discussion in 'C++' started by prasoonthegreat@gmail.com, Mar 24, 2009.

  1. Guest

    #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
     
    , Mar 24, 2009
    #1
    1. Advertising

  2. wrote:
    > #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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 24, 2009
    #2
    1. Advertising

  3. Guest

    On Mar 24, 8:07 pm, Victor Bazarov <> wrote:
    > wrote:
    > > #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
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    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
     
    , Mar 24, 2009
    #3
  4. wrote:
    > [..]
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 24, 2009
    #4
  5. Arne Mertz Guest

    Victor Bazarov schrieb:
    > wrote:
    >> #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
     
    Arne Mertz, Mar 24, 2009
    #5
  6. Guest

    well i have done as you said

    v=new T[size]();

    but i am not getting the array zero initialized......
    any solution for that
     
    , Mar 25, 2009
    #6
  7. Guest

    i am using g++ (Dev C++ 4.9.9.2)......
    any better compiler???
     
    , Mar 25, 2009
    #7
  8. Arne Mertz Guest

    schrieb:
    > 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
     
    Arne Mertz, Mar 25, 2009
    #8
  9. * Arne Mertz:
    > schrieb:
    >> 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


    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)

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Mar 25, 2009
    #9
  10. Alf P. Steinbach wrote:
    > * Arne Mertz:
    >> schrieb:
    >>> 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

    >
    >
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 25, 2009
    #10
  11. * Victor Bazarov:
    > Alf P. Steinbach wrote:
    >> * Arne Mertz:
    >>> schrieb:
    >>>> 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

    >>
    >>
    >>
    >> 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).


    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

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Mar 25, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    452
  2. soni29
    Replies:
    6
    Views:
    432
    Kevin Goodsell
    Sep 5, 2003
  3. Axel
    Replies:
    1
    Views:
    756
    stephan beal
    Oct 27, 2003
  4. sfs
    Replies:
    3
    Views:
    307
  5. Rakesh Kumar
    Replies:
    5
    Views:
    691
    James Kanze
    Dec 21, 2007
Loading...

Share This Page