Altera_Forum
Honored Contributor
11 years agoC++ pass by referenece not working as expected.
I am upgrading NIOS toolsets from 8.1 to 13.1.
In 8.1 this code compiled and functioned correctly. In 13.1 this code compiles and mis functions. There are no warnings associated with the code. Three code snippets to follow which declare and use a Timer class. ************************************** Timers.hpp contains // Timers available enum TimerHandle { TMR_0 = 0, TMR_1 = 1, TMR_INV = 2 }; class Timers { public: // Initialisation operations static Timers* Instance(); // Interval timer functions bool GetIntervalTimer( const TimerHandle & p_rHandle ); protected: Timers(); // Default constructor. ~Timers(); // Destructor. private: // Timers member variables static Timers* m_pThis; // Instance pointer. public: private: }; ************************************** Timers.cpp contains ************************************** # include "Timers.hpp" // initialise static members Timers* Timers::m_pThis = 0; Timers* Timers::Instance() { if(0 == m_pThis) { m_pThis = new Timers(); if(!m_pThis->Init()) { m_pThis->~Timers(); } } return m_pThis; } Timers::Timers() { // Record this for use by interrupt service routine m_pThis = this; } bool Timers::GetIntervalTimer( const TimerHandle &p_rHandle ) { assert( p_rHandle < TMR_INV ); // other stuff if p_rHandle is OK return false; } ************************************** "MainRoutine.cpp" contains ************************************** # include "subroutine.hpp" void main { TimerHandle m_hStatusTimer; // Status timer handle. m_hStatusTimer = TMR_0; // Instantiate the Timers object and initialise Timers* pTimers = Timers::Instance(); pTimers->GetIntervalTimer( m_hStatusTimer ); // } ************************************** so when I debug my code I can see :- m_hStatusTimer gets set to 0
- Then jumps to Timers::GetIntervalTimer.
- Inside Timers::GetIntervalTimer, p_rHandle = 0xffffdfee, so the assert comes into play - and the code stalls - as it should if the input parameter is out of bounds.
- I assume 0xfffdfee is the address of m_hStatusTimer