Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > DynArray
diff DynArray.h @ 32:958dcb7754ca
PR branch -- added comments about safe usage
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Fri, 08 Mar 2013 05:37:45 -0800 |
| parents | e6b21c2350d1 |
| children | c6e1805505c9 |
line diff
1.1 --- a/DynArray.h Mon Sep 03 15:09:32 2012 -0700 1.2 +++ b/DynArray.h Fri Mar 08 05:37:45 2013 -0800 1.3 @@ -12,16 +12,44 @@ 1.4 #include "PR_impl/Services_Offered_by_PR/Memory_Handling/vmalloc.h" 1.5 1.6 1.7 - //A dynamic array is same as any other array, but add a DynArrayInfo next 1.8 - // to it. Accesses and updates of array indexes are done normally, it's 1.9 - // only when add a new element into array that use the extra info. 1.10 - // An add can cause the pointer to the normal array to change.. so must 1.11 - // be protected to single VP at a time. 1.12 + 1.13 +/*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray 1.14 + * inside the function called! After adding or other operation that might 1.15 + * change the size, must re-read the addr of the chunk of memory that is the 1.16 + * array, via the DynArrayInfo. 1.17 + *Here's why: An array variable is a location, either on the stack 1.18 + * or in a field of a struct, whose contents is an addr. That addr is of the 1.19 + * first location of a chunk of locations. The DynArray works by changing 1.20 + * the chunk of locations, then modifying the contents of the original 1.21 + * array variable. It overwrites the addr of the old chunk of locations 1.22 + * with the addr of the new chunk. 1.23 + *But when the array variable is passed as a parameter, such as 1.24 + * in this: "foo( myDynArray )", then there are now two locations that hold 1.25 + * the addr of the same chunk of locations. So when a call is made that 1.26 + * adds to the DynArray, and inside the DynArray expands, it only updates 1.27 + * the original location with the new addr. Hence, the function will begin 1.28 + * overwriting memory past the end of the old chunk, because it still has 1.29 + * the pointer to the old chunk of locations. 1.30 + * 1.31 + *A dynamic array is accessed same as any other array. However, must use 1.32 + * dyn array calls, defined in here, in order to add or increase the size. 1.33 + * Must re-read the original array variable after any size-changing calls. 1.34 + *To pass a DynArray as a parameter to a function, can only pass the 1.35 + * DynArrayInfo, then inside the function, to read the addr of the first 1.36 + * location in the chunk of locations that is the array, do this: 1.37 + * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can 1.38 + * treat localArrayCopy as a normal array, as long as don't make any calls 1.39 + * that add or otherwise could increase the size of the array. If do make 1.40 + * such a call, then re-copy the array via the above. Can then use the 1.41 + * copy up until another add to the array. 1.42 + * 1.43 + */ 1.44 typedef struct 1.45 { 1.46 - void ***addrOfPtrToArray; //addr of array of ptrs == triple * 1.47 - int32 numInArray; 1.48 - int32 sizeOfArray; 1.49 + void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple * 1.50 + int32 numInArray; //num entries added 1.51 + int32 sizeOfArray; //num elems alloc'd 1.52 + int32 sizeOfElem; //num bytes in one elem of array -- used in 2nd version 1.53 } 1.54 PrivDynArrayInfo; 1.55
