|
-
Aug 31st, 2001, 10:53 AM
#1
Thread Starter
Member
Getting the size of a dynamic string array in memory
I have a dynamic array of strings.
Dim DynArray() As String
Redim DynArray(x,y)
How can I find the size of the array in memory?
I have searched for information on the String data type and how it is stored in memory, but couldn't find much.
If I had defined the size of the strings, this question would be easy. (x * y * SizeOfString) But my strings are of varying length.
Is there any way to do this easily? Or do I have to keep track of the size of every string in the array using the Len() function?
Any help greatly appreciated.
Cedric
-
Aug 31st, 2001, 10:54 AM
#2
Frenzied Member
-
Aug 31st, 2001, 10:57 AM
#3
Frenzied Member
Agh.. mybad. Too used to C++
That doesn't exist in VB.
I think you can use len(arrayname)
-
Aug 31st, 2001, 10:58 AM
#4
Thread Starter
Member
R
sizeof() is a C function. It didn't work when I put it in my VB code.
Is there some way to use it in VB?
Cedric
-
Aug 31st, 2001, 10:59 AM
#5
Thread Starter
Member
-
Aug 31st, 2001, 11:02 AM
#6
Frenzied Member
Hmm... there may be an API lemme check.
-
Aug 31st, 2001, 11:09 AM
#7
Fanatic Member
A quick way to do it?
VB Code:
Dim Temp As String
Temp = Join([i]mydynamicarray[/i])
MsgBox Len(Temp)
-
Aug 31st, 2001, 11:11 AM
#8
Frenzied Member
This is far from optimal; its a bit of a cludge but it should work:
If Dir("c:\temp.dat") <> "" Then Kill "c:\temp.dat"
Open "c:\temp.dat" For Binary As #1
Put #1, , arr
Close #1
sizeofarray = FileLen("c:\temp.dat")
-
Aug 31st, 2001, 11:14 AM
#9
Thread Starter
Member
interesting workaround with the temp file.
I will try both methods.
Thanks for your help.
Cedric
-
Aug 31st, 2001, 11:32 AM
#10
Frenzied Member
Originally posted by ExcalibursZone
A quick way to do it?
VB Code:
Dim Temp As String
Temp = Join([i]mydynamicarray[/i])
MsgBox Len(Temp)
I say take out the middle man!
VB Code:
MsgBox Len(Join(MyDynamicArray))
You just proved that sig advertisements work.
-
Aug 31st, 2001, 11:38 AM
#11
Thread Starter
Member
I guess it wasn't clear that my array is two dimensional.
Join() doesn't work with multidimensional arrays.
I tried splitting up the array, but that's more work than it's worth.
The temp file thing worked though.
Thanks mlewis.
Cedric
-
Aug 31st, 2001, 12:22 PM
#12
Fanatic Member
Just to add a little extra on that: if you use the temp file and put, since the strings are variable length VB will first write an integer to the file that represents the length of the next string in characters, and then it will write the string itself. It will repeat that for every string, so the actual size of just the strings themselves will be filelength - (2 * total#ofstrings). That's not really much of a concern if you're only going for an estimate though.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Aug 31st, 2001, 12:24 PM
#13
Frenzied Member
This is true; but there is also some overhead for storing the array information, so the info written to the file counts as stuff used in RAM. Its just maybe 10-20 bytes off for huge arrays (40,000 elements or so)
Note: don't quote me on this; just some quick & dirty estimates in my mind.
-
Aug 31st, 2001, 12:51 PM
#14
Fanatic Member
I just noticed that the original subject was the in memory size. I was thinking only of the string part itself. There's only a paltry amount . 20 bytes for just the array, plus 4 bytes per dimension. A 2D var length string array would be 28 + data size then. Also, in memory, the size of a string is 2x the number of characters, since VB strings are unicode (2 bytes per char). The file however is just 1 byte per char. I should have mentioned that before but it slipped my mind.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|