I want know that exist to like "sizeof()" function in "visual basic"
|
|
Thread rating:  |
TZ - 06 Dec 2007 05:07 GMT ^^ ? I waitting your answer...^^
T.Z nohtaeho78@hotmail.com
Scott Seligman - 06 Dec 2007 05:13 GMT >^^ ? >I waitting your answer...^^ Len()
 Signature --------- Scott Seligman <scott at <firstname> and michelle dot net> --------- Money often costs too much. -- Ralph Waldo Emerson
TZ - 06 Dec 2007 05:38 GMT I think that method(Len) is only use for string type
I want know that have to like "sizeof()" of VC
thank you for your answer^^
TZ
>>^^ ? >>I waitting your answer...^^ > > Len() Scott Seligman - 06 Dec 2007 05:45 GMT >I think that method(Len) is only use for string type > >I want know that have to like "sizeof()" of VC Use Len(). For Strings it returns the length of the string, for other variables, it'll return the number of bytes used.
 Signature --------- Scott Seligman <scott at <firstname> and michelle dot net> --------- The universe is driven by the complex interaction between three ingredients: matter, energy, and enlightened self-interest. -- G'Kar in Babylon 5:"Survivors"
TZ - 06 Dec 2007 06:09 GMT Thank you for your help to me
very good quality infrmation for me.
TZ
>>I think that method(Len) is only use for string type >> >>I want know that have to like "sizeof()" of VC > > Use Len(). For Strings it returns the length of the string, for other > variables, it'll return the number of bytes used. Tony Proctor - 07 Dec 2007 15:52 GMT Not really true. For UDT's it returns the byte size when persisted on disk. Use LenB(), as Mike suggested, to get the in-memory byte size
Tony Proctor
> >I think that method(Len) is only use for string type > > > >I want know that have to like "sizeof()" of VC > > Use Len(). For Strings it returns the length of the string, for other > variables, it'll return the number of bytes used. Jim Mack - 07 Dec 2007 18:13 GMT > Not really true. For UDT's it returns the byte size when persisted > on disk. Use LenB(), as Mike suggested, to get the in-memory byte > size Yes, and be aware that the implementation of LenB() for UDTs changed at some point -- either from VB4-32 to VB5, or from 5 to 6 (don't have them to test). It used to return the true byte size, now it returns the 'alignment' size -- the value that would give you the true usage for an array of UDT if you multiplied it by the number of elements.
IOW, if a UDT is 13 bytes long, LenB used to report 13 -- now it reports 16, because that's how elements of that UDT would be aligned in an array.
 Signature Jim
Thorsten Albers - 07 Dec 2007 21:23 GMT Jim Mack <jmack@mdxi.nospam.com> schrieb im Beitrag <#2guizPOIHA.5720@TK2MSFTNGP04.phx.gbl>...
> Yes, and be aware that the implementation of LenB() for UDTs changed > at some point -- either from VB4-32 to VB5, or from 5 to 6 (don't have [quoted text clipped - 4 lines] > reports 16, because that's how elements of that UDT would be aligned > in an array. LenB() returns the actual amount of bytes allocated for a given symbol. And you can't allocate 13 bytes of memory on a 32 bit platform but only a multiple of 32 bits = 4 bytes. LenB() only returns a value which isn't a multiple of 32 bits if the given symbol is of a native VB data type (Byte, Integer, Boolean) since VB manages the storage of such data types internally. AFAIK LenB() never behaved other then this since it was introduced (VB 4).
 Signature ---------------------------------------------------------------------- THORSTEN ALBERS Universität Freiburg albers@ uni-freiburg.de ----------------------------------------------------------------------
Bob Butler - 07 Dec 2007 23:10 GMT > Jim Mack <jmack@mdxi.nospam.com> schrieb im Beitrag > <#2guizPOIHA.5720@TK2MSFTNGP04.phx.gbl>... [quoted text clipped - 15 lines] > internally. AFAIK LenB() never behaved other then this since it was > introduced (VB 4). That's what I remember as well. LenB was added to supplement Len because of the need to distinguish between padded and non-padded versions. Len works as it always did and LenB works as it did since it was added.
Thorsten Albers - 08 Dec 2007 01:20 GMT Bob Butler <noway@nospam.ever> schrieb im Beitrag <O22cdaSOIHA.1212@TK2MSFTNGP05.phx.gbl>...
> That's what I remember as well. LenB was added to supplement Len because of > the need to distinguish between padded and non-padded versions. Len works > as it always did and LenB works as it did since it was added. I think it mainly was added to VB 4 since this version introduced OLE (/UNICODE) strings. With ANSI strings the return value of Len() is the count of characters >and< bytes. With UNICODE strings it is the count of characters >only<, the count of bytes has to be retrieved with LenB(). For UNICODE strings other "B" functions where added together with LenB(): AscB(), MidB(), InstrB(), LeftB(), RightB(), ChrB(). With the 'standard' versions of the functions working with (UNICODE) string character data is supported, with the "B" versions working with string binary data. (cmp. also AscW() and ChrW()!)
 Signature ---------------------------------------------------------------------- THORSTEN ALBERS Universität Freiburg albers@ uni-freiburg.de ----------------------------------------------------------------------
Schmidt - 08 Dec 2007 07:04 GMT > you can't allocate 13 bytes of memory on a 32 bit platform > but only a multiple of 32 bits = 4 bytes. That behaviour of LenB is only because of the Compiler- Directive wich forces 4Byte-Alignment, not because of an 32Bit-OS-restriction - e.g. if you define your Types inside a Typelib, you can override VBs 4Byte-Alignment if you want that.
And it is possible as well, to allocate 13 Bytes of memory from the system ("under the hood implementation" of the Memory-Manager aside for the moment):
Option Explicit
Private Declare Function HeapCreate Lib "kernel32" _ (ByVal Opt&, ByVal InitSize&, ByVal MaxSize&) As Long Private Declare Function HeapDestroy Lib "kernel32" _ (ByVal hHeap As Long) As Long Private Declare Function HeapAlloc Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal dwBytes&) As Long Private Declare Function HeapSize Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&) As Long Private Declare Function HeapReAlloc Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&, ByVal dwBytes&) As Long Private Declare Function HeapFree Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&) As Long
Private Sub Form_Load() Dim hHeap&, lpMem& hHeap = HeapCreate(0, 0, 0)
lpMem = HeapAlloc(hHeap, 0, 13) If lpMem Then MsgBox HeapSize(hHeap, 0, lpMem)
If hHeap Then HeapDestroy hHeap End Sub
Olaf
Thorsten Albers - 08 Dec 2007 10:29 GMT Schmidt <sss@online.de> schrieb im Beitrag <O5T5nmWOIHA.3852@TK2MSFTNGP06.phx.gbl>...
> And it is possible as well, to allocate 13 Bytes of memory > from the system ("under the hood implementation" of the [quoted text clipped - 4 lines] > lpMem = HeapAlloc(hHeap, 0, 13) > If lpMem Then MsgBox HeapSize(hHeap, 0, lpMem) This doesn't really allocate 13 bytes of memory, it allocates 13 bytes from the heap object created with HeapCreate(). For the created heap in any case at least one and always a multiple of a memory page is allocated by the heap managment functions.
 Signature ---------------------------------------------------------------------- THORSTEN ALBERS Universität Freiburg albers@ uni-freiburg.de ----------------------------------------------------------------------
Schmidt - 08 Dec 2007 21:53 GMT > Schmidt <sss@online.de> schrieb im Beitrag > <O5T5nmWOIHA.3852@TK2MSFTNGP06.phx.gbl>... [quoted text clipped - 10 lines] > it allocates 13 bytes from the heap object created > with HeapCreate(). Yes of course, but in either case you will have some sort of memory-management "in between". The most memory-managers allow you, to allocate ByteCounts, wich don't have to be multiples of 4. How efficient their internal workings are (how they use the internally allocated mem-pages) is of no interest regarding the "LenB-Topic". The reported Values of LenB do *not* depend on the characteristics of the underlying OS (be it 16Bit, 32Bit or 64Bit). Regarding VB-TypeDefs they only depend on the Byte-Packing-directive of the underlying compiler.
And VB has no problems with working against memory- addresses that start on "odd Memory-Slots", wich are not multiples of 4.
Option Explicit
Private Declare Sub BindArray Lib "kernel32" _ Alias "RtlMoveMemory" (pArr() As Any, pSrc As Long, _ Optional ByVal cb As Long = 4) Private Declare Sub ReleaseArray Lib "kernel32" _ Alias "RtlMoveMemory" (pArr() As Any, _ Optional pSrc As Long = 0, _ Optional ByVal cb As Long = 4) Private Type SAFEARRAY1D cDims As Integer fFeatures As Integer cbElements As Long cLocks As Long pvData As Long cElements As Long lLbound As Long End Type
Private Const HEAP_ZERO_MEMORY& = 8 Private Declare Function HeapCreate Lib "kernel32" _ (ByVal Opt&, ByVal InitSize&, ByVal MaxSize&) As Long Private Declare Function HeapDestroy Lib "kernel32" _ (ByVal hHeap As Long) As Long Private Declare Function HeapAlloc Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal dwBytes&) As Long Private Declare Function HeapSize Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&) As Long Private Declare Function HeapReAlloc Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&, ByVal dwBytes&) As Long Private Declare Function HeapFree Lib "kernel32" _ (ByVal hHeap&, ByVal dwFlags&, ByVal lpMem&) As Long
Private Type Test b As Byte i As Integer l As Long End Type
Private Sub Form_Load() Dim i&, Dummy As Test, hHeap&, lpMem& Dim TestArr() As Test, saTestArr As SAFEARRAY1D Const ArrElmts& = 10 hHeap = HeapCreate(0, 0, 0) If hHeap = 0 Then Exit Sub
lpMem = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, LenB(Dummy) * ArrElmts + 1) saTestArr.pvData = lpMem + 1 'Array-Content starts on a "weird" address saTestArr.cbElements = LenB(Dummy) saTestArr.cElements = ArrElmts saTestArr.cDims = 1 BindArray TestArr, VarPtr(saTestArr)
For i = 0 To UBound(TestArr) With TestArr(i) .b = i: .i = i: .l = i Debug.Print .b, .i, .l Debug.Print VarPtr(.b), VarPtr(.i), VarPtr(.l) End With Next i
ReleaseArray TestArr HeapDestroy hHeap End Sub
Olaf
Thorsten Albers - 08 Dec 2007 23:40 GMT Schmidt <sss@online.de> schrieb im Beitrag <OBRSrXeOIHA.4656@TK2MSFTNGP03.phx.gbl>...
> Yes of course, but in either case you will have some > sort of memory-management "in between". [quoted text clipped - 12 lines] > addresses that start on "odd Memory-Slots", wich are > not multiples of 4. I don't want to extend this discussion, so just one final statement:
Assuming you have 5 bytes of memory allocated at memory address x. x+0 x+1 x+2 x+3 --- x+4 (x+5) (x+6) (x+7)
With a 32 bit processor you can't read byte x+4 without also reading bytes x+5-x+7. Therefore bytes x+5-x+7 must have been allocated previously within the same context. It is a completely different question if e.g. only bytes x+0-x+4 of this allocated memory are referenced by symbol ABC, while byte x+5 is referenced by symbol DEF and bytes x+6-x+7 are referenced by symbol GHI (this is a matter of the manager used to manage this allocated memory).
 Signature ---------------------------------------------------------------------- THORSTEN ALBERS Universität Freiburg albers@ uni-freiburg.de ----------------------------------------------------------------------
MikeD - 06 Dec 2007 05:46 GMT >I think that method(Len) is only use for string type > > I want know that have to like "sizeof()" of VC > > thank you for your answer^^ LenB()...look it up in VB's Help.
 Signature Mike Microsoft MVP Visual Basic
TZ - 06 Dec 2007 06:09 GMT Thank you for your help to me
very good quality infrmation for me too
TZ
>>I think that method(Len) is only use for string type >> [quoted text clipped - 3 lines] > > LenB()...look it up in VB's Help.
|
|
|