Page 1 of 1

sizeof()

Posted: Tue Feb 19, 2008 6:40 pm
by Seb McClouth
I'm working on the sizeof() FUNCTION and I was wondering if you could do a FUNCTION <name> (x AS ANY) where x can really be anything, an integer, a string, etc.

grtz

Posted: Wed Feb 20, 2008 2:01 am
by burger2227
Why couldn't you?

Let me know how it works out Seb.

All ya gotta do is try it! It probably won't break your machine.

Ted

Posted: Thu Feb 21, 2008 3:35 am
by Seb McClouth
thx for the reply.

Okay basically sizeof() is suppose to return the bytes of the certain input.

Now here's the tricky part, the input should either be a defined TYPE, an INTEGER, a LONG, a SINGLE, a DOUBLE, or a STRING. Nicest thing is, sizeof() is a build in OPERATOR/FUNCTION in C, so I can't get an exact location of its source code. Atleast the STRING-part, it simple because we just us LEN already for that.

I've tried to DECLARE FUNCTION sizeof(x AS ANY) but that only seems to work for TYPE when the FUNCTION sizof(x AS DeclareType) is. So it doesn't work just like that.

I had the idea of making several sizeof() functions:
  • FUNCTION sizeofdouble(x AS DOUBLE)
    FUNCTION sizeofint(x AS INTEGER)
    FUNCTION sizeoflong(x AS LONG)
    FUNCTION sizeofsingle(x AS SINGLE)
    FUNCTION sizeofstring(x AS STRING)
    FUNCTION sizeoftype(x AS ANY) <- still tricky
So the only working code I have is for a sizeof() a string.

Grtz

Posted: Thu Feb 21, 2008 3:42 pm
by burger2227
Here are the byte sizes in QB:

Integer = 2 bytes = 16 bits 'thus you can store 2 values in AH and AL
Long = 4 bytes = 32 bits
Single = 4 bytes = 32 bits
Double = 8 bytes = 64 bits

That is how you determine the size of a TYPE declaration for file records or headers.

Ted

Posted: Thu Feb 21, 2008 6:15 pm
by Patz QuickBASIC Creations
LEN() also shows the number of bytes needed by the variable.

Code: Select all

LEN(Int%) = 2
LEN(Lng&) = 4
LEN(Sgl!) = 4
LEN(Dbl#) = 8
LEN(Strng$) = Number of characters in the variable, each weighing in at 1 byte each.

I'll be jiggered!

Posted: Thu Feb 21, 2008 7:19 pm
by burger2227
Never tried that. I thought it only did strings. Nice to know!

Ted

Who cares?

Posted: Thu Feb 21, 2008 9:57 pm
by Mac
I just wonder what coding problem you have that requires to know the length of a variable (except string)

If I create a TYPE, for example, and put a bunch of variables, I never needed to need the size. QBasic worries about that.

At the end, I create a record, say XXX as that TYPE.

Fine, now I might finally need the size, so I say LEN(XXX).

I never knew how long a single was, or cared.

Just wondering...

Posted: Fri Feb 22, 2008 2:29 am
by Seb McClouth
Thx for the replies guys!

Burger, that's really handy! Pats, also handy, and I'll try that one for sure!. Mac could you explain that type thing more? Basicially I do:

Code: Select all

size=sizeof(type.partype):size=size+sizeof(type.partype2)... etc
I'll keep you guys updated on the progress.

Posted: Fri Feb 22, 2008 12:45 pm
by burger2227
Here's an example of a type:

Code: Select all

TYPE DTAData     'used by DOS services
  Reserved  AS STRING * 21           'reserved for use by DOS
  Attr AS STRING * 1                 'file's attribute
  Time AS INTEGER                    'file's last change time
  Date AS INTEGER                    'file's last change date
  Size  AS LONG                      'file's byte size
  Named  AS STRING * 13              'file's name
END TYPE
DIM SHARED DTA AS DTAData
The Type definition is used to get the file information back from DOS. There are 43 bytes of information you can get with an Interrupt Routine. To find out how many bytes are in the TYPE DTA variable use LEN(DTA).

Type is similar to an array that holds different types of data. For example the filename is held in the DTA.Named variable. It uses 13 bytes because DOS adds a CHR$(0) to the end of the file name.

Types are often used to define a record in Random files also. Random file records would be the LEN of the Type and the number of records is:

NumberOfRecords = LEN(DTA) \ LOF(#)

In the OPEN RANDOM Statement. LEN = LEN(DTA) 'That includes number lengths also.

The length of a TYPE declaration can also come in handy as when working on bitmaps. The offset tells you where the palette and picture information is located AFTER the Header information.

Ted

Posted: Tue Feb 26, 2008 1:01 am
by Seb McClouth
Sorry for not replying before. I meanted to ask how Mac calculated the LEN of his TYPE's.

Nevertheless is your info, Ted, very handy though.

Thx

Posted: Tue Feb 26, 2008 4:13 pm
by Ralph
Patz:

I could not get QB 4.5 to accept any of your code, as in:

Code: Select all

PRINT LEN(Int%)
I was able to use:

Code: Select all

CLS
a% = 12345       'int
PRINT LEN(a%)
a& = 12345678    'long int
PRINT LEN(a&)
a! = 1234.5      'single
PRINT LEN(a!)
a# = 12345678.9# 'double
PRINT LEN(a#)
Am I doing something wrong?

Sure are

Posted: Tue Feb 26, 2008 5:40 pm
by burger2227
A variable name is used with LEN. It needs a value NOT 0. PRINT LEN(num) displays nothing! Also INT is a QB function and should be used as such. PATZ just used that as an example.

You have to use a variable when getting lengths of numbers! LEN(200) will not work!

Code: Select all

Number = 200
bytes = LEN(Number)
QB requires variables with LEN when checking numbers.

Ted

Posted: Tue Feb 26, 2008 6:12 pm
by Seb McClouth
Ted, once again thx for the insight on this matter.