If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] How to create (0 to -1) bounds byte array?

In some cases I would like my function will return Byte(0 to -1), like 'vb Split' do, so array will be always initialized and further I can safely use Ubound() for checking or directly in 'for each' e.t.c., no matter, but I don't know how correctly create such "empty" byte array in a way to be sure not to get crash surprises when runtime decides to free memory of such "empty" array.

It turns out that it is "sort of" dimensioned. In other words, SAFEARRAY.cDims has a non-zero value. However, when we look at the SAFEARRAYBOUND values, we see that the lLBound = 0 but the cElements also = 0. Obviously, the VB6 UBound function just does something like: UBound = sab.lLBound + (sab.cElements - 1)

And that all makes perfect sense to me.

And then, that got me thinking some more. We can certainly place any type of an array in a Variant. So why can't we place one of these funny "sort of" dimensioned array into a Variant and then typecast it? Along these lines, I wrote this code:

Given that no actual memory is allocated for the array data, I see no foul in doing this typecasting.

It would appear that this is a workable method for creating one of these "funny" arrays for any type. It could probably be improved a bit with GetMem2, but the above certainly seems to work.

Regards,
Elroy

Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Given that no actual memory is allocated for the array data, I see no foul in doing this typecasting.
It would appear that this is a workable method for creating one of these "funny" arrays for any type. It could probably be improved a bit with GetMem2, but the above certainly seems to work.

Nope, that's not going to fly, since you neglected to change the .cbElements Member
of the (underlying) ByteArray you've choosen as a "template".

One can remedy (and add/overwrite) that too, of course - but then the code would become longer -
so why not use the OleAut-function which was desigend for that purpose instead (to save some lines of code)...

Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Re: How to create (0 to -1) bounds byte array?

Ahhh, I didn't see your post. Ok, it looks like it does matter. I'll admit that I was a bit nervous about it. But it's all fixed now, either way.

EDIT1: Truth be told, I'm a bit surprised that a Redim Preserve works on these things, but I suppose that does make sense.

EDIT2: Yeah, I went ahead and used vbArray.

Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Re: [RESOLVED] How to create (0 to -1) bounds byte array?

@Dragokas: My sample `EmptyBiff12PartArray` was intended for `cBiff12Part` being a VB6 class. For an empty UDTs array as in your sample code, you have to tweak the signature to use `Optional ByVal VarType As VbVarType = vbUserDefinedType` to prevent crashes. Yes, mismatch on `VarType` param default value and API declare's retval type can certainly lead to catastrophic failures.

The funny thing in your mismatched snippet is that `ReDim` does not AV but calling `Erase` on this franken-array actually does.