1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Invalid member variable values after object instantiation.

Discussion in 'C++' started by gsfare, Sep 9, 2010.

  1. gsfare

    gsfare New Member

    Joined:
    Sep 9, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello everyone. I am experienceing some rather strange behaviour regarding the instantiation of an object. It's a templated custom array type object, something like this.

    Code:
    template  class Array
    {
       public:
    
          // Construction/destruction.
          Array( void );
          Array( unsigned int size );
          Array( const Array &array );
          ~Array( void );
    
          // Public members.
          unsigned int size( void ) const;
    
          // Operator overloads.
          const Array &operator = ( const Array &array );
    
       private:
    
          // Private member variables.
          T *array_;
          unsigned int size_;
    };
    
    I then have another class that is to perform a simple difference operation on an Array type object.

    The function in my difference class looks like this. I've removed the actual meat of it for clarity but the error occurs before all of that anyway.

    Code:
    Array Difference::Differentiate( const Array &array )
    {
       Array tempArray( array.size() - 1 );
    
       return( tempArray );
    }
    
    Now to my problem. After instantiation of the object tempArray it's member variables array_ and size_ are invalid. size_ in this case should be 9 as array.size_ = 10. If I use GDB to analyse tempArray.size_ from within the Differentiate() member function it's showing a value of 4253902!

    Now, the strange thing is, I know my constructor for Array is working correctly because if I step into the creation of tempArray the member variables are setup correctly, but upon return from the constructor the member variables are no longer the same.

    Has anybody seen behaviour like this? I'm suspecting a stack corruption maybe. I have been running with libDUMA though so it should hopefully rule that out.

    Any help is greatly appreciated!
     

Share This Page