sizeof()

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

sizeof()

Post by Seb McClouth » Tue Feb 19, 2008 6:40 pm

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
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Wed Feb 20, 2008 2:01 am

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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Thu Feb 21, 2008 3:35 am

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
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Thu Feb 21, 2008 3:42 pm

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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Thu Feb 21, 2008 6:15 pm

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.

User avatar
burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

I'll be jiggered!

Post by burger2227 » Thu Feb 21, 2008 7:19 pm

Never tried that. I thought it only did strings. Nice to know!

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Who cares?

Post by Mac » Thu Feb 21, 2008 9:57 pm

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...

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Fri Feb 22, 2008 2:29 am

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.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

User avatar
burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Fri Feb 22, 2008 12:45 pm

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
Last edited by burger2227 on Tue Feb 26, 2008 3:06 am, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Tue Feb 26, 2008 1:01 am

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
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph » Tue Feb 26, 2008 4:13 pm

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?
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

User avatar
burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Sure are

Post by burger2227 » Tue Feb 26, 2008 5:40 pm

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
Last edited by burger2227 on Sun Mar 02, 2008 3:03 pm, edited 2 times in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth » Tue Feb 26, 2008 6:12 pm

Ted, once again thx for the insight on this matter.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Post Reply