No way to allocate memory in VB?
How do I allocate memory in Visual Basic? Something such as malloc() as in C/C++
BYTE *myByte;
myByte = malloc(10);
Uh, what do you need this for? I mean, VB has automatic memory management!?
- JQ, PWC Software
"programming is all about wasting time" -me
- JQ, PWC Software
"programming is all about wasting time" -me
~phil
I''m pretty sure you can''t... but you can use arrays defined like:
...and then dynamically allocate how big you want them to be...
Note that if you use Preserve, all you can change is the upper bound of the last dimension... so...
--Tr][aD--
Dim MyData() as Byte
...and then dynamically allocate how big you want them to be...
''allocate 512 bytesRedim MyData(0 to 511)''allocate 1024 bytes w/o losing the data in bytes 0-511Redim Preserve MyData(0 to 1023)''allocate a 64x64 textureRedim MyData(0 to 63, 0 to 63)
Note that if you use Preserve, all you can change is the upper bound of the last dimension... so...
Redim MyData(0 to 63, 0 to 63)Redim MyData(0 to 127, 0 t0 63) ''this won''t workRedim MyData(0 to 63, 32 to 63) ''nor will thisRedim MyData(0 to 63, 0 to 511) ''but this will
--Tr][aD--
--Tr][aD--
Well the problem with that is the array must be declared with constants.
Dim someVar as Integer
someVar = 50
Dim myArray(0 to someVar) as Byte ''won''t work
where as the malloc() takes anything
What i''m trying to do is open a file and read it into another file. So after a open one file I want to read it into memory and the write it to another from that memory block.
Dim someVar as Integer
someVar = 50
Dim myArray(0 to someVar) as Byte ''won''t work
where as the malloc() takes anything
What i''m trying to do is open a file and read it into another file. So after a open one file I want to read it into memory and the write it to another from that memory block.
Try this:
Dim someVar As Integer
Dim myArray() As Byte
someVar = 50
ReDim myArray(0 To someVar)
André Luiz Silva
Dim someVar As Integer
Dim myArray() As Byte
someVar = 50
ReDim myArray(0 To someVar)
André Luiz Silva
"- To begin with, said the Cat, a dog's not mad. You grant that? - I suppose so, said Alice. - Well, then, - the Cat went on - you see, a dog growls when it's angry, and wags its tail when it's pleased. Now I growl when I'm pleased, and wag my tail when I'm angry. Therefore I'm mad."
As said above, use a dynamic array, by Diming the variable with nothing inside the brackets:
Dim Whatever() As Integer
Then you can ReDim it to however large you want:
ReDim Whatever(i)
And if you want to keep the data that is already there in it (so if you make it one thing bigger, you still keep the old info) then use this:
ReDim Preserve Whatever(i)
The other option is to use a collection, either the collection which is given to you, or you make your own using classes. Even using a linked list would work as well.
hth
Trying is the first step towards failure.
Dim Whatever() As Integer
Then you can ReDim it to however large you want:
ReDim Whatever(i)
And if you want to keep the data that is already there in it (so if you make it one thing bigger, you still keep the old info) then use this:
ReDim Preserve Whatever(i)
The other option is to use a collection, either the collection which is given to you, or you make your own using classes. Even using a linked list would work as well.
hth
Trying is the first step towards failure.
Trying is the first step towards failure.
"What i''m trying to do is open a file and read it into another file. So after a open one file I want to read it into memory and the write it to another from that memory block"
If this is all you want to do (ie you don''t want to manipulate the data before writing it out to the second file) then there is no need to use an array. Open both files at the same time, one for input, one for output/append, and read/write the bytes one at a time.
If this is all you want to do (ie you don''t want to manipulate the data before writing it out to the second file) then there is no need to use an array. Open both files at the same time, one for input, one for output/append, and read/write the bytes one at a time.
vbisme,
I''ve actually done this before in an Archive manager for Descent using the Windows API. It was in VB3 using 16bit APIs though. I used GlobalAlloc and GlobalFree for memory management, and hread and hwrite for the file I/O. In the 32bit world, you should still be able to use GlobalAlloc / GlobalFree, but use the ReadFile and WriteFile APIs for I/O.
Overall, I''d suggest using one of the methods mentioned earlier. If, however, they do not work, you can try this one, but don''t attempt it unless you have a good API reference for VB. I can''t really give specific exmpamples as my code is out of date, and I haven''t written any VB code in years.
Later,
Micah
I''ve actually done this before in an Archive manager for Descent using the Windows API. It was in VB3 using 16bit APIs though. I used GlobalAlloc and GlobalFree for memory management, and hread and hwrite for the file I/O. In the 32bit world, you should still be able to use GlobalAlloc / GlobalFree, but use the ReadFile and WriteFile APIs for I/O.
Overall, I''d suggest using one of the methods mentioned earlier. If, however, they do not work, you can try this one, but don''t attempt it unless you have a good API reference for VB. I can''t really give specific exmpamples as my code is out of date, and I haven''t written any VB code in years.
Later,
Micah
Moot,
I''ve found copying files byte-by-byte is painfully slow, so I came up with this work-around using an array as a buffer;
Dim f_buffer(511) As Byte
rec_length = 512
Open Text1.Text For Binary Access Write As #1
Open afilename$ For Binary Access Read As #2 Len = rec_length
bytes_to_read = LOF(2)
While (bytes_to_read >= rec_length)
Get #2, , f_buffer
Put #1, , f_buffer
bytes_to_read = bytes_to_read - rec_length
Wend
If (bytes_to_read > 0) Then
left_over$ = String(bytes_to_read, " ")
Get #2, , left_over$
Put #1, , left_over$
End If
Close #1
Close #2
Cheers
Matt
I''ve found copying files byte-by-byte is painfully slow, so I came up with this work-around using an array as a buffer;
Dim f_buffer(511) As Byte
rec_length = 512
Open Text1.Text For Binary Access Write As #1
Open afilename$ For Binary Access Read As #2 Len = rec_length
bytes_to_read = LOF(2)
While (bytes_to_read >= rec_length)
Get #2, , f_buffer
Put #1, , f_buffer
bytes_to_read = bytes_to_read - rec_length
Wend
If (bytes_to_read > 0) Then
left_over$ = String(bytes_to_read, " ")
Get #2, , left_over$
Put #1, , left_over$
End If
Close #1
Close #2
Cheers
Matt
Is this a file that is updated over time? If not just copy the file using the API or the FileSystemObject in the Scripting runtime. Even if you need to copy the contents of the file you can still use the FileSystemObject:
Dim fs As New FileSystemObject
Dim ts As TextStream
Dim ts2 As TextStream
Dim sText As String
Set ts = fs.OpenTextFile("somefile.txt", ForReading)
sText = ts.ReadAll
ts.Close
Set ts2 = fs.OpenTextFile("someotherfile.txt", ForWriting)
ts2.Write sText
ts2.Close
Done.
Breakaway Games
Dim fs As New FileSystemObject
Dim ts As TextStream
Dim ts2 As TextStream
Dim sText As String
Set ts = fs.OpenTextFile("somefile.txt", ForReading)
sText = ts.ReadAll
ts.Close
Set ts2 = fs.OpenTextFile("someotherfile.txt", ForWriting)
ts2.Write sText
ts2.Close
Done.
Breakaway Games
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement