TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALEX WALKER
from: KURT KUZBA
date: 1997-12-13 23:01:00
subject: Finding the end of an arr

AW>   Just wondering, if an array is passed to a function, is
AW>   there a way to test for or find the end of the array
AW>   without having the size of the array passed to the
AW>   function?
AW>   If it's a char string one could search/test for the null
AW>   character, but what if it's an array of integers?
   You need a delimiter or a numeric limit.
   In C++, it is common to use a list or an object array class.
   In either of these methods, the array is maintained behind
   the scenes and you access the elements through the member
   functions rather than directly, and the class or template
   keeps track of the limits of the array. You don't pass the
   array, then. You pass the array object, and all the limit
   and access functionality are built into the object.
   If a rigid array is required, then a fixed limit is needed.
   A simple struct can supply the needed limit info.
typedef struct {
   long lLimit;
   int *piArray;
}  ARRAY_INTEGER;
   As an example of using such a structure, suppose you needed
   to create an array with 1000 int objects:
ARRAY_INTEGER *MakeArray(long lLimit)
{
   ARRAY_INTEGER MyArray;
   MyArray.lLimit = lLimit;
   MyArray.piArray = new int[lLimit];
   return MyArray;
}
   But now you can see how this would make a class desirable.
class IntegerArray
{
public:
   IntegerArray(long);
   ~IntegerArray();
   int Set(long, int);
   int Get(long);
   long Size(void){ return lLimit; }
private:
   long lLimit;
   int *piArray;
}
void IntegerArray::IntegerArray(long lElements = 10)
{
   lLimit = lElements;
   piArray = new int[lLimit];
}
void IntegerArray::~IntegerArray(void)
{
   delete []piArray;
}
int IntegerArray::Set(long lElement, int iValue)
{
   if(lElement = 0)
   {
      *(piArray + lElement) = iValue;
      return lElement;
   }
   return 0;
}
int IntegerArray::Get(long lElement)
{
   if(lElement = 0)
      return *(piArray + lElement);
   return 0;
}
   Now you have a data object which will manage the array for
   you. Since the data is encapsulated, you have ensured that
   nobody will accidentally write beyond the array bounds and
   the actual array limit is always available to any function
   using the array. There is a performance hit on speed of
   access to the data, however, so, where speed is critical,
   the structure with limit info bound to the array and trusting
   to the users of the data to observe your boundaries would be
   the better method.
> ] It's around somewhere. I put it where I wouldn't lose it....
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.