Variable String in a UDT question (SOLVED)

General FreeBASIC programming questions.
Post Reply
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Variable String in a UDT question (SOLVED)

Post by leopardpm »

here is the UDT:

Code: Select all

'given:
type thisType
    as ushort a
    as string s
    as ushort b
end type

dim as thisType Ary(1 to 3)
as far as actual memory/byte layout, it should look like this:
Array(1): 4 bytes for a, 1? byte for s, 4 bytes for b
(I don't know if a NULL string is 1 byte or how it initializes the array)

Code: Select all

Ary(1)   Ary(2)   Ary(3)
aaaasbbbbaaaasbbbbaaaasbbbb
so what happens when I do this?
Ary(2).s = "Test String" ' a string of 10 chars

Does it 'make space' in the array in memory by moving the righthand side over 10 bytes and insert the string? or is the variable string portion actually a pointer to the string somewhere else?

Code: Select all

Ary(1)   Ary(2)   Ary(3)
aaaasbbbbaaaaTest Stringsbbbbaaaasbbbb
Basically, I am wondering if arrays of variable strings are repeatedly moved around as strings are changed within them....
Last edited by leopardpm on Dec 19, 2018 3:03, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Variable String in a UDT question

Post by MrSwiss »

Generally speaking: a FB var-len String, is a Type (in and of itself),
similar to: FixLenStr (using the same layout as FB-String) so, the
allocated memory for the effective String is outside of your Type,
which only contains the Type's header (1 x Ptr + 2 x Integer).

12 bytes = FBC 32 / 24 bytes = FBC 64

testing code:

Code: Select all

type thisType
    as string s
    as ushort a, b
end type

Print "Type size: "; Str(SizeOf(thisType))
Print "FBC x"; Str(SizeOf(Any Ptr) * 8) 

Sleep
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Variable String in a UDT question

Post by leopardpm »

ok... so the array data in memory is not physically moved around, only the pointer is possibly changed, whenever a string is changed in a variable sized string array... right?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Variable String in a UDT question

Post by MrSwiss »

Yes, correct. The real string-memory is allocated to the Ptr ...
(len(String) / memory allocated, stored in the two Integers)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Variable String in a UDT question

Post by leopardpm »

Thank you, Mr. Swiss!
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Variable String in a UDT question (SOLVED)

Post by fxm »

This is the same principle for a var-len array declared as UDT field:
- Only the array descriptor is in the UDT data, with a constant length for a declared array, because only depending on the number of dimensions of the array (number of 'Any' in the declaration).
- The array data are allocated elsewhere in the heap memory, referenced by this descriptor.

For a fixed-len array declared as UDT field, the array data are directly in the UDT data (in the same way as for a UDT's fix-len string).
Post Reply