sizeof(udt) and more

General FreeBASIC programming questions.
Post Reply
Fabrizio_00000
Posts: 21
Joined: Mar 31, 2011 17:30
Location: Rome, Italy

sizeof(udt) and more

Post by Fabrizio_00000 »

Hi guys, please help me out because this thing is driving me crazy.

Have a look at this code fragment:

Code: Select all

type flddef field = 1
	name	as string * 16
	namelen	as ubyte
	type	as ubyte
	size	as ushort
end type

print sizeof(flddef)
I should get 20 (16 + 1 + 1 + 2) but the result is 21. Why?

But there's more:

I have a string whose content ("outstr", whose length is 100 bytes) is what should be passed into flddef with these lines of code:

Code: Select all

recdef = allocate(5 * sizeof(flddef))
for i = 0 to 4
	memcpy(@recdef[i], sadd(outstr) + i * 20, sizeof(flddef))
next
The transfer takes place successfully, in fact the verification of the content of the allocated memory area is correct:

Code: Select all

for i = 0 to flds * sizeof(flddef) - 1
	ch = *(cast(ubyte ptr, recdef) + i)
	print hex(ch, 2); " ";
	linlen += 1
	if linlen = sizeof(flddef) then
		linlen = 0
		print
	endif
next
print
Result:

Code: Select all

41 4C 50 48 41 30 30 30 30 30 30 30 30 30 30 31 D0 10 0A 00 42
42 45 54 41 30 30 30 30 30 30 30 30 30 30 30 32 10 02 02 00 47
47 41 4D 4D 41 30 30 30 30 30 30 30 30 30 30 33 90 03 04 00 44
44 45 4C 54 41 30 30 30 30 30 30 30 30 30 30 34 10 08 04 00 45
45 50 53 49 4C 4F 4E 30 30 30 30 30 30 30 30 35 10 10 14 00 00
The last byte of each record seems to be the first of the next one (do you remember? 21 instead of 20). But let's leave this issue out for now. Let's try to inspect the individual fields:

Code: Select all

for i = 0 to flds - 1
	print recdef[i].name; " "; recdef[i].namelen; " "; recdef[i].type; " "; recdef[i].size
next
The result should have been this:

Code: Select all

ALPHA00000000001 208 16 10
BETA000000000002 16 2 2
GAMMA00000000003 144 3 4
DELTA00000000004 16 8 4
EPSILON000000005 16 16 20
But instead this is what I get:

Code: Select all

ALPHA00000000001 16 10 16896
BETA000000000002 2 2 18176
GAMMA00000000003 3 4 17408
DELTA00000000004 8 4 17664
EPSILON000000005 16 20 0
it seems that the "namelen" field is completely ignored.

I'm considering getting help from a good psychiatrist but thought maybe I'm not the problem...
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: sizeof(udt) and more

Post by fxm »

From documentation:
Fixed length strings are also terminated with a NULL character, and so they use size + 1 bytes of space. This NULL terminator may be removed in future, to prevent the redundant character complicating data layout in user-defined Types.
Fabrizio_00000
Posts: 21
Joined: Mar 31, 2011 17:30
Location: Rome, Italy

Re: sizeof(udt) and more

Post by Fabrizio_00000 »

Oh, God! I'll have to rewrite 80% of my code...
Well, at least I learned something.
Thanks, fxm!
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: sizeof(udt) and more

Post by fxm »

NOTE:

'Dim As String * n ...':
- total characters = n+1
- useful characters = n

'Dim As Zstring * n ...' or 'Dim As Wstring * n ...' :
- total characters = n
- useful characters = n-1
Post Reply