On Feb 26, 2006, at 3:03 PM, Robert Purves wrote: > > Jay Reeve wrote: > >> So can different calls to malloc_size on the same pointer yield >> different results? > > No, unless the malloc'ed block has been realloc'ed larger. > >> Can you tell us how FB knows the element size when it calls the >> runtime dynamic fns? Any way we can get to it? > > The compiler knows the element size from the dim statement. > You will have to supply sizeof( gMyDynamicArray(0) ) as a > parameter to any function requiring the size of a dynamic array > element. > > Robert P. Okay, if anyone is still interested in this thread, here is a hack which works. It's not as elegant as I'd like, but it does the job and doesn't add a lot of overhead. It again involves some small additions to RP's dynamic incl. I'm not going to publish the whole thing again, but this one's a little simpler: 1. Open Subs Dynamic.incl from the headers folder and resave it into your User Libraries folder. 2. Go to the bottom of the file and paste this code after the #END IF: '------------------------ local fn DynamicElemSize( @dyn as ptr ) '~'1 dim as long r, size long if system( _sysVers ) < 1000 size = ( fn GetHandleSize( dyn.AutoXREFHndl& ) \\ dyn.AutoXREFMax& ) xelse LONG if dyn.autoXREFCurr& for r = 0 to dyn.autoXREFCurr& - 1 long if gFBDynamicSize.addr( r ) = dyn size = gFBDynamicSize.size( r ) exit fn end if next END IF size = 0 end if end fn = size local mode local fn DynamicInsertItems( @dyn as ptr, where as long, howMany as long ) '~'1 dim as long itemCount, itemSize, baseAddr if ( dyn == _nil ) or ( where < 0 ) then exit fn itemCount = dyn.autoXREFCurr& long if itemCount + howMany >= dyn.AutoXREFMax& fn FBDynamicGrowOrAllocate( itemCount + howMany, dyn, itemSize ) end if itemSize = fn dynamicElemSize( #dyn ) if ( itemSize == 0 ) then exit fn long if ( where < itemCount ) baseAddr = [dyn.AutoXREFHndl&] BlockMove baseAddr + ( itemSize * where ), baseAddr + ( itemSize * ( where + howMany ) ), ¬ ( itemCount - where ) * itemSize dyn.autoXREFCurr& = itemCount + howMany blockZero( baseAddr + where * itemSize, howMany * itemSize ) end if end fn local mode local fn DynamicRemoveItems( @dyn as ptr, first as long, howMany as long ) '~'1 dim as long itemCount, itemSize, baseAddr if ( dyn == _nil ) or ( first < 0 ) then exit fn itemSize = fn dynamicElemSize( #dyn ) if ( itemSize == 0 ) then exit fn itemCount = dyn.autoXREFCurr& if first + howMany >= itemCount then howMany = itemCount - first long if ( first < itemCount ) baseAddr = [dyn.AutoXREFHndl&] BlockMove baseAddr + ( itemSize * ( first + howMany ) ), ¬ baseAddr + ( itemSize * first ), ¬ ( itemCount - first - howMany ) * itemSize dyn.autoXREFCurr& = itemCount - howMany blockZero( baseAddr + dyn.autoXREFCurr& * itemSize, howMany * itemSize ) end if end fn '------------------------ 3. Go to line 210 (END IF in the initialize section) and paste these lines above it: '------------------------ // Store element size for programmer reference <Jay 2/26/06> if homeAddr == @gFBDynamicSize then exit fn // -- Jay 2/26/06 -- p = [ @gFBDynamicSize + _autoXREFCurr ] // -- Jay 2/26/06 -- gFBDynamicSize.addr( p ) = homeAddr // -- Jay 2/26/06 -- gFBDynamicSize.size( p ) = elementSize // -- Jay 2/26/06 -- '------------------------ 4. Move to the blank lines above that FN (~ 168), and paste these lines: '------------------------ begin globals begin record FBDynamicSize // <Jay 2/27/06> dim as long addr, size end record dynamic gFBDynamicSize(_maxInt) as FBDynamicSize // <Jay 2/27/06> end globals '------------------------ 5. Save and close the file 6. Test it out, and let me know what you find! What this does is create a separate listing of element sizes for each dynamic array, created at the time each array is initialized, and indexed by the array's header address. It's less efficient than storing it with the array header, but I don't see any way that's going to be possible. With this altered include in place, you may call these fns: FN DynamicInsertItems( theArray, where, howMany ) FN DynamicRemoveItems( theArray, first, howMany ) elemSize = FN DynamicElemSize( theArray ) 'Returns 0 if array has not been initialized e-e =J= a y "