Philipe@20: /* Philipe@20: * File: Vector.h Philipe@20: * Author: Me Philipe@20: * Philipe@20: * Created on May 14, 2010, 3:08 PM Philipe@20: */ Philipe@20: Philipe@20: #ifndef _DYNARRAY_H Philipe@20: #define _DYNARRAY_H Philipe@20: Philipe@20: #include Philipe@20: Philipe@20: Philipe@20: Philipe@20: /*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray Philipe@20: * inside the function called! After adding or other operation that might Philipe@20: * change the size, must re-read the addr of the chunk of memory that is the Philipe@20: * array, via the DynArrayInfo. Philipe@20: *Here's why: An array variable is a location, either on the stack Philipe@20: * or in a field of a struct, whose contents is an addr. That addr is of the Philipe@20: * first location of a chunk of locations. The DynArray works by changing Philipe@20: * the chunk of locations, then modifying the contents of the original Philipe@20: * array variable. It overwrites the addr of the old chunk of locations Philipe@20: * with the addr of the new chunk. Philipe@20: *But when the array variable is passed as a parameter, such as Philipe@20: * in this: "foo( myDynArray )", then there are now two locations that hold Philipe@20: * the addr of the same chunk of locations. So when a call is made that Philipe@20: * adds to the DynArray, and inside the DynArray expands, it only updates Philipe@20: * the original location with the new addr. Hence, the function will begin Philipe@20: * overwriting memory past the end of the old chunk, because it still has Philipe@20: * the pointer to the old chunk of locations. Philipe@20: * Philipe@20: *A dynamic array is accessed same as any other array. However, must use Philipe@20: * dyn array calls, defined in here, in order to add or increase the size. Philipe@20: * Must re-read the original array variable after any size-changing calls. Philipe@20: *To pass a DynArray as a parameter to a function, can only pass the Philipe@20: * DynArrayInfo, then inside the function, to read the addr of the first Philipe@20: * location in the chunk of locations that is the array, do this: Philipe@20: * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can Philipe@20: * treat localArrayCopy as a normal array, as long as don't make any calls Philipe@20: * that add or otherwise could increase the size of the array. If do make Philipe@20: * such a call, then re-copy the array via the above. Can then use the Philipe@20: * copy up until another add to the array. Philipe@20: * Philipe@20: */ Philipe@20: typedef struct Philipe@20: { Philipe@20: void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple * Philipe@20: int32 numInArray; //num entries added Philipe@20: int32 sizeOfArray; //num elems alloc'd Philipe@20: int32 sizeOfElem; //num bytes in one elem of array -- used in 2nd version Philipe@20: } Philipe@20: PrivDynArrayInfo; Philipe@20: Philipe@20: PrivDynArrayInfo * Philipe@20: makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray ); Philipe@20: Philipe@20: PrivDynArrayInfo * Philipe@20: makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray ); Philipe@20: Philipe@20: PrivDynArrayInfo * Philipe@20: makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray ); Philipe@20: Philipe@20: int32 Philipe@20: addToDynArray( void *value, PrivDynArrayInfo *info ); Philipe@20: Philipe@20: void Philipe@20: makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex ); Philipe@20: Philipe@20: void Philipe@20: makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex); Philipe@20: Philipe@20: void Philipe@20: increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize ); Philipe@20: Philipe@20: typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever Philipe@20: Philipe@20: void Philipe@20: freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr ); Philipe@20: Philipe@20: void Philipe@20: freeDynArrayFlat( PrivDynArrayInfo *info ); Philipe@20: Philipe@20: Philipe@20: typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void * Philipe@20: Philipe@20: void Philipe@20: forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr ); Philipe@20: Philipe@20: #endif /* _DYNARRAY_H */ Philipe@20: