Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150080

Re: is there a way to ask a variable what it is?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: is there a way to ask a variable what it is?
Date 2020-02-19 12:45 +0000
Organization A noiseless patient Spider
Message-ID <87pneabvaj.fsf@bsb.me.uk> (permalink)
References (1 earlier) <h9cfvbF25s3U14@mid.individual.net> <r0r37k$r04$1@dont-email.me> <87zhe5pzqf.fsf@bsb.me.uk> <86lfozeswm.fsf@linuxsc.com> <75f1fb82-5e5d-40d5-88e6-a701c17d619f@googlegroups.com>

Show all headers | View raw


already5chosen@yahoo.com writes:

> On Tuesday, February 18, 2020 at 6:56:18 PM UTC+2, Tim Rentsch wrote:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>> 
>> > [...] C is a statically typed language so all type information,
>> > and everything easily derived from type information, is known at
>> > compile time.
>> 
>> I don't think that's right exactly.  For one example, some type
>> information may be known in one translation unit but not known
>> in another.  A declaration like
>> 
>>     extern int special_values[];
>> 
>> offers partial but not complete type information in this TU, and so
>> is not known at compile time.
>
> What is "a real type", if there is such thing, is unknown, but what
> compiler think about the type when it deals with this specific
> translation unit is know.

I was not going to reply because I was deliberately simplifying for the
OP and hoping that no one would ask me to justify what I'd wrote!.

I am with you here.  The type of special_values is known in this TU --
it's an incomplete array type.  The same identifier may have a different
type in another TU.

>> A second example is variable length
>> arrays.  Here the type information is not known until run time, and
>> usually is not fixed but varies with different invocations. 
>
> Here we are at philosophical question of "what is considered a type?".
> You appear to come from school of original Pascal, where length of
> array is considered part of its type. That's not how traditional 'C'
> sees it.

No, the size of an array, when known, has always been part of the type,
right back to K&R C.  You don't often get to see this fact because of
the array to pointer conversion, but try:

  int a[4];
  int (*ap)[3] = &a;

and you'll see.

> Anyway, VLA are already optional in the latest Standard and
> hopefully will be marked deprecated in the next one. I am of opinion
> that their addition in c99 was a mistake.

Certainly debatable, but I think the committee has thrown the baby out
with the bath water.  Variably modified types are very useful and have
none of the issue that so rile people up about variable length arrays,
but I think both have been made optional together.

>> A third
>> example pertains to structures and unions in dynamically allocated
>> memory, where code inspects a tag using the "one special guarantee"
>> for structure member access, to select a specific struct type (using
>> a cast) at run time.  
>
> That is similar to the first case: the "real" type is unknown, but the
> type, assumed by compiler at any given point/statement is known.

Hmm... What C always knows is the type of any expression used to access
an object.  This is probably what you mean by the "real" type (but do
correct me if that's not what you mean).  C adds to this the notion of
the effective type of an object, and makes certain accesses undefined
when mismatches occur between these two types.  The effective type of an
allocated object is genuine run-time type information but the standard
makes sure no implementation needs to track it.

<cut>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 19:19 -0800
  Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 03:47 +0000
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:20 -0800
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 19:18 +0000
        Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 20:58 +0000
          Re: is there a way to ask a variable what it is? nobody@example.org (Scott) - 2020-01-30 15:59 +0000
            Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-30 16:35 +0000
    Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:55 -0800
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:24 +0000
        solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
          Re: solved: Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-30 03:16 -0800
  Re: is there a way to ask a variable what it is? Ian Collins <ian-news@hotmail.com> - 2020-01-29 17:28 +1300
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
      Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 06:49 +0000
      Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 08:08 +0100
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:14 +0000
        solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
        Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 08:56 -0800
          Re: is there a way to ask a variable what it is? already5chosen@yahoo.com - 2020-02-19 01:19 -0800
            Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 02:53 -0800
            Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-19 12:45 +0000
            Re: is there a way to ask a variable what it is? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-19 07:55 -0500
            Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-02-19 13:02 +0000
              Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 07:27 -0800
            Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-22 09:42 -0800
  Re: is there a way to ask a variable what it is? Robert Wessel <robertwessel2@yahoo.com> - 2020-01-28 22:36 -0600
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
      Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 13:22 +0100
        Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 13:57 +0100
          Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:47 +0100
            Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-30 12:06 +0100
    Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-29 15:10 +0000
      Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:07 +0100
  Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:52 -0800
  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:11 +0000
    Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 13:30 +0100
      Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 12:44 +0000
        Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 14:03 +0100
          Re: is there a way to ask a variable what it is? gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-29 14:03 +0000
          Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 14:30 +0000
            Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 15:45 +0100
              Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 16:28 +0100
                Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:35 -0800
                Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 17:34 +0000
                Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 18:48 +0100
    Re: is there a way to ask a variable what it is? Thiago Adams <thiago.adams@gmail.com> - 2020-01-29 04:43 -0800
      Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 14:46 +0100
        Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 15:15 +0000
          Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-29 08:11 -0800
          Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:38 -0800
  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:23 +0000
  Re: is there a way to ask a variable what it is? John Bode <jfbode1029@gmail.com> - 2020-01-29 11:55 -0800
  Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100
  Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100

csiph-web