size of a structure [message #7905] |
Mon, 27 January 1997 00:00  |
Phil Williams
Messages: 78 Registered: April 1996
|
Member |
|
|
A couple of questions for y'all
1) What is the "length" of a struture in the help,/st mean? It's not
the "size" which is what I thought it was. i.e.
IDL> help,/st,info
** Structure DATA_INFO, 18 tags, length=124:
TYPE STRING 'GE'
PATH STRING
'/w/GEDB/GINX/GENESIS/MRS/710294/03075/005/'
SIZE LONG 138976
OFFSET LONG 7904
DATATYPE STRING '_16BIT_SGN_INT'
TE FLOAT 15.0000
TR FLOAT 24.0000
MATRIX INT Array(3)
FOV FLOAT Array(3)
PXLSZ FLOAT Array(3)
SCANTIME FLOAT 0.00000
IMAGETIME FLOAT 56.2200
NAME STRING 'xxxxxxxxx,xxxxxxx'
STUDY_NUMBER LONG 3075
PAT_ID STRING '710294'
AGE STRING 'x xxxxx'
SEX STRING 'female'
MASS STRING '15.8760 kg'
IDL> openw,1,'test.dat'
IDL> writeu,1,info
IDL> close,1
IDL> openr,1,'test.dat'
IDL> s = fstat(1)
IDL> print,s.size
162
2) Is there a way, besides brute force, to determine the size of a
structure w/in a procedure?
Thanks for your time,
Phil.
BTW, I've updated some of my routines on my webpage. Please check them
out when you get a chance: www.irc.chmcc.org/idl/philsIDL.html
--
/*********************************************************** ********/
Phil Williams, Ph.D.
Research Instructor
Children's Hospital Medical Center "One man gathers what
Imaging Research Center another man spills..."
3333 Burnet Ave. -The Grateful Dead
Cincinnati, OH 45229
email: williams@irc.chmcc.org
URL: http://scuttle.chmcc.org/~williams/
/*********************************************************** ********/
|
|
|
Re: size of a structure [message #7972 is a reply to message #7905] |
Wed, 29 January 1997 00:00  |
rivers
Messages: 228 Registered: March 1991
|
Senior Member |
|
|
In article <Pine.SUN.3.91.970129142249.26479C-100000@demsyd.syd.dem.csiro.au>, Peter Mason <peterm@demsyd.syd.dem.csiro.au> writes:
> On Mon, 27 Jan 1997, Phil Williams wrote:
>> 1) What is the "length" of a struture in the help,/st mean? It's not
>> the "size" which is what I thought it was. i.e.
>> IDL> help,/st,info
> < ... structure with several string fields & some other fields, with
> < help,/struct showing length=124 >
> < ... code showing that structure written to disk is 162 bytes long >
>
> It seems that the structure's "length" is correct except when it comes to
> structure members which are strings. A string member contributes 16
> bytes to the structure's "length", regardless of string length.
> This is probably because strings are dynamic, while everything else about
> an IDL structure's size (#members, member datatypes and dimensions) is
> static: the 16 bytes shown for a string member is probably some sort of
> string descriptor with a pointer to the actual string.
Be careful here, you also need to worry about padding. IDL will build its
structures to obey the machine's alignment requirements. For example if you
have a structure:
s = {a: 0, b: 0L}
you might expect the size to be 6 bytes. Indeed, here is the output of IDL run
on a VAX:
IDL> s= {a: 0, b: 0L}
IDL> help, /str, s
** Structure <4767ac>, 2 tags, length=6, refs=1:
A INT 0
B LONG 0
The length is 6.
However, on a machine which requires long integers to be aligned on quadword
boundaries, then there will be 2 bytes of padding between s.a and s.b. Here is
the output of IDL run on a DEC Alpha:
IDL> s= {a: 0, b: 0L}
IDL> help, /str, s
** Structure <47cbc0>, 2 tags, length=8, refs=1:
A INT 0
B LONG 0
Note that length is 8, not 6.
____________________________________________________________
Mark Rivers (773) 702-2279 (office)
CARS (773) 702-9951 (secretary)
Univ. of Chicago (773) 702-5454 (FAX)
5640 S. Ellis Ave. (708) 922-0499 (home)
Chicago, IL 60637 rivers@cars.uchicago.edu (e-mail)
or:
Argonne National Laboratory (630) 252-0422 (office)
Building 434A (630) 252-0405 (lab)
9700 South Cass Avenue (630) 252-1713 (beamline)
Argonne, IL 60439 (630) 252-0443 (FAX)
|
|
|
Re: size of a structure [message #7977 is a reply to message #7905] |
Wed, 29 January 1997 00:00  |
Peter Mason
Messages: 145 Registered: June 1996
|
Senior Member |
|
|
On Mon, 27 Jan 1997, Phil Williams wrote:
> 1) What is the "length" of a struture in the help,/st mean? It's not
> the "size" which is what I thought it was. i.e.
> IDL> help,/st,info
< ... structure with several string fields & some other fields, with
< help,/struct showing length=124 >
< ... code showing that structure written to disk is 162 bytes long >
It seems that the structure's "length" is correct except when it comes to
structure members which are strings. A string member contributes 16
bytes to the structure's "length", regardless of string length.
This is probably because strings are dynamic, while everything else about
an IDL structure's size (#members, member datatypes and dimensions) is
static: the 16 bytes shown for a string member is probably some sort of
string descriptor with a pointer to the actual string.
> 2) Is there a way, besides brute force, to determine the size of a
> structure w/in a procedure?
Not that I know.
But brute force is often OK, especially for a computer (which is by nature
a brute :)
e.g.,
; Return the size of variable V in bytes
forward_function sizeof ;make sure our recursion will work
function sizeof,v
j=size(v) &t=j(j(0)+1) &n=j(j(0)+2) &j=0
case t of
0:rv=0L
1:rv=1L
2:rv=2L
3:rv=4L
4:rv=4L
5:rv=8L
6:rv=8L
7:begin ;string
rv=0L &for i=0L,n-1L do rv=rv+strlen(v(i)) &n=1L
end
8:begin ;structure
rv=0L &nt=n_tags(v(0))-1L
for ii=0L,n-1L do begin ;loop here because of string members
vv=v(ii)
for i=0,nt do rv=rv+sizeof(vv.(i)) ;loop thru members
endfor
n=1L
end
9:rv=16L
else:rv=0L
endcase
return,rv*n
end
Peter Mason
|
|
|