Should I use an array in a class?

Started by
11 comments, last by ace_muncher 22 years, 6 months ago
I am making a class with about 20 integers. If I make it into an array, I know it would be more complicated code, but wouldn''t it save more space and speed? Do big-name games use arrays? -Ace
-Ace
Advertisement
well it really depends ace, if you ONLY need 20 intergers then an array is fine in my opinion, but a linked list might be more appropriate.
Live by the code, die by the code
Arrays are one of the most powerful things in C++ and there is nothing wrong in using them in classes. Without arrays we would have to name everything Item1, Item2, ect. which would make trying to control anything with more than 6 or so elements a real mess. Also, putting an array inside your class shouldn''t make messier code.

This:

int Array[20];

for(int x=0; x<20; x++)
Array[x] = x;

Looks alot better than this:

int Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num10, Num11, Num12, Num13, Num14, Num15, Num16, Num17, Num18, Num19, Num20;

Num1 = 0;
Num2 = 1;
Num3 = 2;
Num4 = 3;
Num5 = 4;
Num6 = 5;
Num7 = 6;
Num8 = 7;
Num9 = 8;
Num10 = 9;
Num11 = 10;
Num12 = 11;
Num13 = 12;
Num14 = 13;
Num15 = 14;
Num16 = 15;
Num17 = 16;
Num18 = 17;
Num19 = 18;
Num20 = 19;

Hope this help

Gandolf
I think there's to much blood in my caffeine system.
Let''s look at three scenarios.

Scenario #1: You don''t know how many elements of the array will actually be used, but even a fully used array only requires say, 1k or less, and you know that you will only need a few instances of this class. In that case, I say, use the array.

Scenario #2: You know exactly how many elements of the array will be used: full capacity. And you also know that all the size of the array is not huge, and you have decided that all the classes will be using the array data consistently and often. In that case, use the array.

Scenario #3: The array may be huge, it might not be fully utilized, or you need many instances of the classes, and each class may only need the array during certain times. In that case, I would look at dynamic memory allocation and linked lists.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Acctually, you''d probably do better to use an array than a linked list, unless you might need a huge amount of numbers. IMO, Linked lists are better to use with large classes that take alot of memory, no with small types such as int''s. The main reason for this is link lists can get pretty ugly and will take up about 3 times the amount of memory as an array of int''s would (at least the way i do them). Either way is good, just depends on what you need and what you feel comfortable with i guess.
I think there's to much blood in my caffeine system.
quote:If I make it into an array, I know it would be more complicated code,
but wouldn''t it save more space and speed?


No, in fact, a class is something like an array. Classes can be called typedef struct, some compilers allow this, and a struct access is being translated to asm just like

for(i=0; i < xy; ++i)
// ...
mov somewhere, arrays'' base address + i*sizeof(single element)

There is no real difference. Sometimes, its more nice to use arrays, for instance:

  class CClass{   void AnyFunction()   {      memset(m, 0, sizeof(int)*2);   }   int m[2];}  


However, you can replace the memset-call above with:

  memset((int*)this, 0, sizeof(this));  


That is dangerous, if you change the the order of the elements, things could screw up, because the compiler has no idea of what you are doing, and perhaps you are deleting things you don''t want to delete.

I think in this case, the important thing is what kind of integers these are. Is it a player''s flags, his IP address, his ID, etc., or is it just some 20 integers? I mean, if they have something logically associated with it, such as size, health, etc., use elements. If the class has to store 20 integers it doesn''t know anything about, use an array to keep things clear.

Cheers,
syn
My class is dealing with Units, I''m going to be making an RTS.
I''m fairly new at this, so...
But thank you anyway.


-Ace
-Ace
try this. my arraylist. kinda like java
but c++

    #define SAFE_DELETE(lp) { if(lp) { delete lp;lp = NULL; }}template <class D>class CArrayList{public:	CArrayList(): m_pData(NULL) , m_nCapacity(0), m_nSize(0) { }	virtual ~CArrayList() { SAFE_DELETE(m_pData); }	void capacity(int n)	{		if(m_nSize == 0)		{			SAFE_DELETE(m_pData);			m_pData = new D[n];			memset(m_pData,0,sizeof(D) * n);			m_nCapacity = n;		} else			if(n > m_nCapacity)			{				D * t_pData;				t_pData = new D[n];				memset(t_pData,0,sizeof(D) * n);				m_nCapacity = n;				memcpy(t_pData,m_pData,m_nSize * sizeof(D));				SAFE_DELETE(m_pData);				m_pData = t_pData;			}	}	int capacity() { return m_nCapacity; }	int size() { return m_nSize; }	void push_back(D * lp) 	{		if(m_nCapacity == 0)			capacity(10);		memcpy(m_pData + m_nSize,lp,sizeof(D));		m_nSize++;		if(m_nSize == m_nCapacity / 2)			 capacity(m_nCapacity * 2);	}	void pop_back()	{		m_nSize--;	}	typedef D * iterator;	iterator begin() { return m_pData; }	iterator end() { return m_pData + m_nSize; }	void erase(iterator it)	{		swap(it,m_pData + m_nSize);		pop_back();	}	void swap(iterator it1,iterator it2)	{		D t;		memcpy(&t,it1,sizeof(D));		memcpy(it1,it2,sizeof(D));		memcpy(it2,&t,sizeof(D));	}private:	D * m_pData;	int m_nCapacity;	int m_nSize;};    


its like java arraylist.

{ Stating the obvious never helped any situation !! }

Edited by - jwalker on October 15, 2001 7:25:14 PM
Hmmmmmm what do you guys think about vectors then?
Acts just like an array, but if space runs out it just doubles in size?
Live by the code, die by the code
I''d go for vectors...

Jack

This topic is closed to new replies.

Advertisement