简体   繁体   中英

Passing Array of objects Composed of other objects as reference to Functions In C++?

I have a function which calls itself 10 times a second. I am using QTimer for repeat calls.

void DisplayClass::UpdateGuiWithData()

{

//miscellaneous code which is validated

SingletonObjectAsThread::instance()->UpdateFromGuiToExternalHardware(ClassOjbectArray,var1,var2);

QTimer::singleShot(100,this,SLOT(UpdateGuiWithData()));

}


Class A_ComposingClass_B_Object

{

//boolean and enum variables

B ArrayOf_B_Objects[16];

}

Class B

{

//boolean and enum vairables

}

class DisplayClass

{

//variables that are not a concern here
UpdateGuiWithData();
A ArrayOfObject_A[4];

};

Class SingletonAsThread

{

//vairables that are not a concern here

UpdateFromGui(A_ComposingClass_B_Object*,const bool&,const bool&);

};

Here is the deal when I run the code as is, there is a steady increase in memory size but when I comment out the Call to UpdateFromGui call in UpdateGuiWithData class, the memory stays at a constant level of around 51 MB. The UpdateFromGui function has no dynamic memory allocation or GUI functions. It is just a plane jane function that constructs the packet to write to serial port and is called 10 times a second since this is the refresh rate of the hardware.

The only reason I could think of the increase in memory is passing the array of objects with each call to UpdateFromGui function. I think with each call we are creating a copy of class objects and hence the increase in memory. Then I tried to use passing array of objects as reference to the function but couldn't find a suitable declaration for such a function, though I found the definition and usage of such a function. Here is what I found on the net.

// Receive Array by reference.

void GetArray( int (&Array) [10] )

{

}

// Test array by reference.

void CRabbitDlgDlg::TestArray()

{

    // Pass array by reference.

    int Array[10] = { 0 };

    GetArray( Array );

}

My question are

--->Am I thinking on the right line or is it something to do with repeated call to singleton

class object?

--->Also do I need a copy constructor here for Class A, though there is no dynamic

allocation or pointer variables in this class?

--->What else can be the source of this memory leak (if not its not about copy constructor or singleton calls) which constantly increases the memory usage

of the application?

Arrays in C and C++ don't work in the way you described. What we call an "array" is just a memory region, where objects are placed next to each other, and the pointer that points to first object. When you pass a pointer to a function, you just pass a single integer number. No object it points to is copied and it keeps pointing to the same contiguous chunk of memory.

What you encountered is a true memory leak. You allocate memory somewhere and forget to free it, when it's no longer needed. Or you enter recursion where you didn't want to, and each function allocates some memory in stack.

And your clode is badly presented--I can't find declarations, about which you're talking right after the code window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM