# A simple array declaration problem

This topic is 4458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Without getting into too much detail about what i'm doing, since it will only complicate things, i'll show you how i tried to solve the problem and explain from there: Class CBoard { CBoard(int iSize); const int iBoardSize; COtherClass* pOtherClass[iBoardSize]; } CBoard::CBoard(int iSize) { iBoardSize = iSize; } This is just pseudo code but you can see i'm trying to initialize the size of the array based on the class constructor i'm not even sure this is possible i'd like to know if it is and if so how it's done. Any help is greatly appreciated. Mike!

##### Share on other sites
If you want to declare an array in a class you have to use a literal, not a variable:
COtherClass* pOtherClass[10];notCOtherClass* pOtherClass[iBoardSize];

However if you want the constructor to determine the size of the array then dynamically allocate it:

COtherClass* pOtherClass;In C'tor:pOtherClass= new COtherClass[ someSizeVariable or literal ];

Hope that helps,

Dave

##### Share on other sites
Ahhh yeah dynamic allocation i never thought of that cheers!

Emmm except that doesn't seem to work now that i've tried it.

By the way i'm using C++ and DirectX.

I'll keep trying to see if i can find a way around it any other ideas?

##### Share on other sites

CBoardPiece *pBoardPiece;

cpp File

CBoard::CBoard(LPDIRECT3DDEVICE8 pD3DDevice, int iSize)
{
CBoardPiece* pBoardStoreage[iSize];

for (int h = 0; h < iSize; h++)
{
pBoardStoreage[h] = new CBoardPiece(pD3DDevice);
}

pBoardPiece = pBoardStoreage;
}

I tried to implement it the way you said, as above, according to the syntax i know but still have the problem with iSize being a variable, any more ideas?

Mike!

##### Share on other sites
class Board{	public:		Board(unsigned int size);	private:		std::vector< OtherClass * > other_class;}Board::Board(unsigned int size)	:	other_class(size){}

• Manual memory management is error-prone and rarely more efficient than the standard container classes.

• Systems Hungarian notation has no redeeming qualities in a type-safe language like C++, as argued in one of my favourite articles. Also see the parameters of the WndProc to see the negatives in action (hint: compare the wart and the type of WPARAM).

• Since the array can never have a negative size it's safer to use an unsigned type.

Σnigma

##### Share on other sites
I've read that dynamic allocation like this can be very costly, so do you think it would be better to store and the maximum size array of the class but then only initialise the ones i need i.e.

CBoardPiece* pBoardPiece[MAXAMMOUNT]

Construtor::Constructor(unsigned int iSize)
{

for (int h = 0; h < iSize; h++)
{
pBoardPiece[h] = new CBoardPiece();
}

}

Do you think this would this be more or less efficient?

Considering the CBoardPiece class is a single triangle fan with its relevant methods.

##### Share on other sites
Don't optimize prematurely. Unless you're doing this thousands of times a second you don't need to worry about it yet. Implement your program with the simplest solution possible. If when finished it doesn't run fast enough then find out what is slowing you down using a profiler and optimize those parts.

Make it run, then make it fast.

Σnigma

##### Share on other sites
Ok then i'll look into these vector gizmos i haven't used them before but i've always thought they where something like a linked list, but like i said i know nothing about them.

##### Share on other sites
Quote:
 Original post by Marx2052I've read that dynamic allocation like this can be very costly,

Did you try it to see? Did you question what "very costly" meant in context?

Did it occur to you that in C++, the exact sizeof() types *must* be known *at compile time*?

Quote:
 so do you think it would be better to store and the maximum size array of the class but then only initialise the ones i need i.e.CBoardPiece* pBoardPiece[MAXAMMOUNT]

If you can *prove* that MAXAMOUNT is always enough AND there is a reason *imposed by the problem domain* that MAXAMOUNT makes sense as a logical upper bound AND the *minimum* space you will use is not significantly less than MAXAMOUNT, then yes. But even then, it's a question of space and of code complexity, rather than speed.

Quote:
 Considering the CBoardPiece class is a single triangle fan with its relevant methods.

The amount of time you spend dynamically allocating a chunk of memory for the board will be orders of magnitude less than the time spent to draw a triangle fan even once. Hell, it will probably be far less than the time spent drawing a single triangle. You are likely to create the board once at the beginning of the game, and draw every piece every frame. Where do you think you should be looking for optimizations?

That said, you may consider whether you really need to store a container of pointers, and dynamically allocate each piece. That does add extra code complexity, and could slow things down slightly (due to extra indirection and cache coherency issues). But the devil is in the details.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 14
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009830
• ### Who's Online (See full list)

There are no registered users currently online

×