Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with UDTs and Strings / PSA
#1
I've run across a bug(s) with variable length strings nested within a UDT.

It manifested itself when I had some variables that originate from a UDT that should have been initialized to zero, but were instead a random value.

This is a stripped down example of the issue(s).

Its seems that if you nest a variable length string in a UDT It can causes crashes.

Variable length strings in this case is convenient for me because the LEN function reports the actual length of string, not the allocated block of memory. This makes the string parsing and manipulation easier.

The workaround would to use fixed length strings and roll my own LEN function.

I'm using Linux and QB v3.13.0

Code: (Select All)
'Nested String UDT Issues

TYPE tUDT_NEST_DEEPER
'If I comment out the 'a' element, and the element 's' is not
' a fixed length, the elements do not initialize to zero properly.
a AS LONG
's AS STRING * 10 ' Works with no issue
s AS STRING ' broken
b AS LONG
END TYPE

TYPE tUDTNEST
s AS STRING
n AS LONG
u AS tUDT_NEST_DEEPER
END TYPE

TYPE tUDT
a AS LONG
b AS tUDTNEST
c AS LONG
END TYPE

DIM test AS tUDT

'The following should initialize to 0 or blank
PRINT " These should all be zero or blank."
PRINT "test.a = "; test.a
PRINT "test.b.s = "; test.b.s
PRINT "test.b.n = "; test.b.n
PRINT "test.c = "; test.c

'Uncomment for crash, if you are using variable length strings
'PRINT "test.b.u.s = "; test.b.u.s
'PRINT "test.b.u.b = "; test.b.u.b
Reply
#2
I don't know how to fix this besides using fixed length strings and that is what I would use.   If you need to know the actual length of the data include a length variable in the UDT.        I can tell you a little about what is going on.   A variable length string in a UDT is actually just a pointer.   The actual string data resides elsewhere.     A udt itself MUST be fixed length or the compiler doesn't know how to allocate it.
Reply
#3
Thanks, I have opened https://github.com/QB64-Phoenix-Edition/...issues/524 to track this.
Reply
#4
Yeah variable length strings in UDTs cause trouble, known issue. If I recall correctly their presence can cause the need to initialize the numbers too??? along with the strings.

Fixed strings is excellent work around and you know what? not using UDT's can speed up executions of finished program but this solution may make it a coding nightmare for organization.
b = b + ...
Reply
#5
I ran into this issue a long time ago and swore off using variable length strings. But some of the code I've seen posted on the forum were using it with no issue, so I was under the impression that those issues were resolved.

Like I said earlier, they are convenient because they can behave like an array, and you don't have to manage the size and LEN function behaves as expected.

I found it kinda funny that depending on the order of elements in the array would cause slightly different behaviors.

Quote:not using UDT's can speed up executions of finished program but this solution may make it a coding nightmare for organization.
 
I thought about that too. But, I would end up having a bunch arrays that have identifiers approaching the 40 character limit.
Reply
#6
This has been fixed in the latest 3.14.0 release.
Reply
#7
(07-27-2024, 08:01 PM)justsomeguy Wrote: I ran into this issue a long time ago and swore off using variable length strings. But some of the code I've seen posted on the forum were using it with no issue, so I was under the impression that those issues were resolved.

Like I said earlier, they are convenient because they can behave like an array, and you don't have to manage the size and LEN function behaves as expected.

I found it kinda funny that depending on the order of elements in the array would cause slightly different behaviors.

Quote:not using UDT's can speed up executions of finished program but this solution may make it a coding nightmare for organization.
 
I thought about that too. But, I would end up having a bunch arrays that have identifiers approaching the 40 character limit.
It might be fun to have a coding style discussion, in a new thread, to compare UDT's to non-UDT variable use, or maybe just to discuss coding styles, pros and cons, in general.

Pete
Fake News + Phony Politicians = Real Problems

Reply




Users browsing this thread: 1 Guest(s)