|
|
Up |
|
|
  |
Author: ArjanArjan Date: Aug 21, 2008 13:03
Hi!
Is there an easy way to get the size in bytes of an intricate user-
defined type even if there is not yet a variable of this type?
Something like:
INTEGER, PARAMETER :: NMax = 10
TYPE MyType
INTEGER(2), DIMENSION(NMax) :: Values
END TYPE MyType
NBytes = SIZEOF(MyType)
which would yield NBytes = 20
My program protests that I want to allocate too much space and I want
to find out how much I was trying to allocate.
Regards,
Arjan
|
| |
|
| | 20 Comments |
|
  |
Author: dpbdpb Date: Aug 21, 2008 13:08
Arjan wrote:
...
> Is there an easy way to get the size in bytes of an intricate user-
> defined type even if there is not yet a variable of this type?
...
> NBytes = SIZEOF(MyType)
>
> which would yield NBytes = 20
>
> My program protests that I want to allocate too much space and I want
> to find out how much I was trying to allocate.
...
What about allocating one (or a small number) and extrapolating from
there as a kludge?
I'm not sure about the SIZEOF() intrinsic
-- it's an extension in the
compiler I use that has some limitations...ottomh I don't know if it
gets user-defined types right under all cases or not. (Simple ones I'm
pretty sure are ok, more complex I'm not sure about.)
--
|
| |
|
| | no comments |
|
  |
Author: nospamnospam Date: Aug 21, 2008 13:35
> Is there an easy way to get the size in bytes of an intricate user-
> defined type even if there is not yet a variable of this type?
...
> NBytes = SIZEOF(MyType)
Certainly not in the standard, and I sort of doubt even most of the
usual extensions. There are various ways to get the size of a data
object, but not just of a type itself. You don't tend to be able to use
a type name as an argument (as in your example).
As soon as you declare a variable of the type, then that's a different
story; several potential ways then, standard and extensions, but I'm
under the impression that you know about them.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
|
| |
| no comments |
|
  |
Author: ArjanArjan Date: Aug 21, 2008 16:12
> As soon as you declare a variable of the type, then that's a different
> story; several potential ways then, standard and extensions, but I'm
> under the impression that you know about them.
Apparently not, Richard, please give me some hints!
By the way: I believe that in Pascal the SIZEOF function works with
just a TYPE as arguement.
But then: fortran .NEQ. Pascal...
A.
|
| |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Aug 21, 2008 17:55
Arjan wrote:
(snip)
> By the way: I believe that in Pascal the SIZEOF function works with
> just a TYPE as arguement.
> But then: fortran .NEQ. Pascal...
In C, the sizeof operator (not function) works with either
types or variables. Often parentheses are needed, but it
still isn't a function.
-- glen
|
| |
| no comments |
|
  |
Author: James GilesJames Giles Date: Aug 21, 2008 17:23
glen herrmannsfeldt wrote:
> Arjan wrote:
> (snip)
>
>> By the way: I believe that in Pascal the SIZEOF function works with
>> just a TYPE as arguement.
>> But then: fortran .NEQ. Pascal...
>
> In C, the sizeof operator (not function) works with either
> types or variables. Often parentheses are needed, but it
> still isn't a function.
Of course, the distinction is hard to follow without formal
definitions of the terms. In most language semantics discussions,
an operator is just syntactic sugar for a function application.
I've got a copy of the C89 rationale document around here
somewhere. I'm not sure it ever stated *why* sizeof is
considered an operator. Though it may be that they wanted
to allow types as operands, and didn't want to special-case
their description of functions.
|
| Show full article (1.26Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Aug 21, 2008 18:35
James Giles wrote:
(snip on sizeof function or operator)
> Of course, the distinction is hard to follow without formal
> definitions of the terms. In most language semantics discussions,
> an operator is just syntactic sugar for a function application.
Mostly that the parenthesis needed for a function call
aren't needed for sizeof.
> I've got a copy of the C89 rationale document around here
> somewhere. I'm not sure it ever stated *why* sizeof is
> considered an operator. Though it may be that they wanted
> to allow types as operands, and didn't want to special-case
> their description of functions.
Function call is also an operator. You can apply () to
anything of the appropriate type, even the return value
from a function. (Not that I have ever tried the latter.)
> Parenthesis are optional only of the operand is a unary-expression.
> It's probably good style to use parenthesis anyway.
I usually do. sizeof(struct xyz), sizeof(long double),
sizeof(a+b).
|
| Show full article (0.99Kb) |
| no comments |
|
  |
Author: nospamnospam Date: Aug 21, 2008 17:50
>> As soon as you declare a variable of the type, then that's a different
>> story; several potential ways then, standard and extensions, but I'm
>> under the impression that you know about them.
>
> Apparently not, Richard, please give me some hints!
Oh, I thought your emphasis on using the type without declaring a
variable meant that you knew those ways. Well, lets' see...
1. In f2003, oh, hold on. That's right - that one didn't make it into
f2003. One of my pet peeves that f2003 added new features that applied
only to f77-style stuff. (You could get the size of the f77 kinds, but
that's all). Anyway, the f2008 draft has a storage_size intrinsic. Some
compilers might implement it already, as it is trivial. I think the
f2008 draft also has C_SIZEOF (or some such name), though it is
basically redundant with storage_size.
2. Lots of compilers today have extensions, often called sizeof.
3. The best you can do that is standard conforming today is to use
inquire (iolength=result_variable) data_variable
|
| Show full article (2.64Kb) |
| no comments |
|
  |
Author: Gary ScottGary Scott Date: Aug 21, 2008 18:00
Richard Maine wrote:
>>>As soon as you declare a variable of the type, then that's a different
>>>story; several potential ways then, standard and extensions, but I'm
>>>under...
|
| Show full article (1.86Kb) |
| no comments |
|
  |
|
|
  |
Author: nospamnospam Date: Aug 21, 2008 18:28
Gary Scott sbcglobal.net> wrote:
> I think that some of these C interoperability additions were done
> incorrectly. What if I want to find the Fortran sizeof something?
> Naming it C_SIZEOF implies that I might never have a need to do that
> (not implying that the result would be different (padding?)). I think I
> would have for those things that are potentially useful in Fortran,
> would have added an optional argument to a generically named
> SIZEOF...(BIND(C),...) (or BIND=C or ...). I don't like sprinkling
> unnecessary references to other languages that way just to get that type
> of new functionality.
I probably wrote it a bit messily, making it harder than it ought to be,
but you might want to reread my prior posting. If you want the Fortran
size of something, you use the intrinsic that is there for exactly that.
It is named storage_size. There are reasons why it is not named sizeof,
but that doesn't sound like what you are complaining about. Sounds like
you are complaining that it doesn't exist in the more general form. It
does. Yes, I very much can come up with non-C-related uses for
storage_size; that's why I proposed it.
|
| Show full article (3.27Kb) |
| no comments |
|
|
|
|