Reputation: 6323
Can pc_TcpClientList depend on how the C_TcpSocketAccess is declared to determine if it is on the stack or heap?
Or is pc_TcpClientList an array(that is on the stack) with a bunch of pointers basically if you use pRef = &pc_TcpClientList[idx]
.
class C_TcpSocketAccess : public SAP::C_TcpSocket
{
public:
C_TcpSocket pc_TcpClientList[65]; //this is on the stack? or it depends where C_TcpSocketAccess is put?
};
{
C_TcpSocketAccess pcat;// pcat is on the stack
pcat.pc_TcpClientList; // pc_TcpClientListis on the stack because pcat is on the stack
C_TcpSocketAccess *pdog = new C_TcpSocketAccess(); // pdog is on the heap
pdog->pc_TcpClientList; // pc_TcpClientList is on the heap because pdog is on the heap?
}
Upvotes: 1
Views: 97
Reputation: 1940
C_TcpSocketAccess pcat //object is not on the heap and so //pc_TcpClientList[65] is also not on the heap
If you allocate memory for your object on the heap then the array which is a part of the object would also be on the heap. Likewise if you use stack allocation.
C_TcpSocket C_TcpSocketAccess::pc_TcpClientList[65];
As per your declaration pc_TcpClientList
is an array of C_TcpSocket
s regardless of where C_TcpSocketAccess
is created. Remember when you allocate on the heap you need to clean up once a heap object's job is done otherwise you end up with a memory leak.
Vectors is what I would recommend instead of using statically allocated arrays:
#include <vector>
class C_TcpSocketAccess : public SAP::C_TcpSocket
{
public:
std::vector<C_TcpSocket> cTcpClientList;
};
//usage in some function
{
C_TcpSocketAccess cTcpSockAcc;
C_TcpSocket aSocket;
cTcpSockAcc.cTcpClientList.push_back(aSocket);
}
Upvotes: 1