const field in structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Skybuck Flying

    const field in structure

    Hello,

    What does Const mean in this c structure ? and what is the delphi equivalent
    ?

    I think const struct just means it can't be modified... is that correct ?

    Struct {
    Type1 Field1;
    Const struct Type2 *Field2;
    } Structure1;

    So my guess would be:

    Type
    Structure1 = record
    Field1 : Type1;
    Field2 : ^Type2;
    end;

    Delphi doesn't have constant fields in structures/records ?

    Why would someone make the field const in C, is it really that important ?

    I am guessing it's just a safety precaution during coding... ?

    Bye,
    Skybuck


  • Rob Kennedy

    #2
    Re: const field in structure

    Skybuck Flying wrote:[color=blue]
    > What does Const mean in this c structure ? and what is the delphi equivalent
    > ?
    >
    > I think const struct just means it can't be modified... is that correct ?
    >
    > Struct {
    > Type1 Field1;
    > Const struct Type2 *Field2;
    > } Structure1;[/color]

    That is a pointer to a const struct. The value of the pointer field may
    change, but that value may not be used to modify the thing being pointed
    at. Delphi has no equivalent.
    [color=blue]
    > Why would someone make the field const in C, is it really that important ?[/color]

    I could try to answer, but since you've cross-posted this to a C
    newsgroup, I'll let the experts there explain that. I don't know how
    similar C and C++ are in this regard, but you may be interested in the
    C++ FAQ category on "const correctness":



    --
    Rob

    Comment

    • Emmanuel Delahaye

      #3
      Re: const field in structure

      Skybuck Flying wrote on 11/08/04 :
      [color=blue]
      > What does Const mean in this c structure ? and what is the delphi equivalent
      > ?
      >
      > Struct {
      > Type1 Field1;
      > Const struct Type2 *Field2;
      > } Structure1;
      >[/color]

      Nothing I'am aware of. 'Const' is not a C-word. If you are talking of
      'const', it's a different story.It means that the pointer Field2 is
      only allowed to make read acces to the pointee.

      A big difference between Pascal and C : C is case sensitive.

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

      "C is a sharp tool"

      Comment

      • Skybuck Flying

        #4
        Re: const field in structure


        "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote in message
        news:mn.5d2b7d4 8f3543fca.15512 @YOURBRAnoos.fr ...[color=blue]
        > Skybuck Flying wrote on 11/08/04 :
        >[color=green]
        > > What does Const mean in this c structure ? and what is the delphi[/color][/color]
        equivalent[color=blue][color=green]
        > > ?
        > >
        > > Struct {
        > > Type1 Field1;
        > > Const struct Type2 *Field2;
        > > } Structure1;
        > >[/color]
        >
        > Nothing I'am aware of. 'Const' is not a C-word. If you are talking of
        > 'const', it's a different story.It means that the pointer Field2 is
        > only allowed to make read acces to the pointee.[/color]

        Well now I'm confused.

        Does it mean

        1. The pointer is read only.
        2. The structure is read only.

        ?

        :)


        Comment

        • Arthur J. O'Dwyer

          #5
          Re: const field in structure


          On Wed, 11 Aug 2004, Skybuck Flying wrote:[color=blue]
          >
          > "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote...[color=green]
          >> Skybuck Flying wrote on 11/08/04 :
          >>[color=darkred]
          >>> What does Const mean in this c structure ? and what is the delphi[/color][/color]
          > equivalent[color=green][color=darkred]
          >>> ?
          >>>[/color][/color][/color]
          struct {[color=blue][color=green][color=darkred]
          >>> Type1 Field1;[/color][/color][/color]
          const struct Type2 *Field2;[color=blue][color=green][color=darkred]
          >>> } Structure1;[/color][/color][/color]
          [...][color=blue]
          > Well now I'm confused.
          >
          > Does it mean
          > 1. The pointer is read only.
          > 2. The structure is read only.
          > ?[/color]

          It means the structure is read-only. The structure pointed to by
          'Field2', that is, of course; not the whole 'Structure1' structure!


          For the experts: It surprised me just now to learn that 'gcc -ansi'
          actually permits

          struct foo {
          const int bar;
          };

          Does this mean what I think it means --- 'bar' can only be given a
          value during initialization --- or is this just a quirk of GCC or
          of the Standard?

          -Arthur

          Comment

          • Rob Kennedy

            #6
            Re: const field in structure

            Skybuck Flying wrote:[color=blue]
            > Does it mean
            >
            > 1. The pointer is read only.
            > 2. The structure is read only.[/color]

            The second one.

            --
            Rob

            Comment

            • Emmanuel Delahaye

              #7
              Re: const field in structure

              Skybuck Flying wrote on 11/08/04 :[color=blue][color=green][color=darkred]
              >>> Struct {
              >>> Type1 Field1;
              >>> const struct Type2 *Field2; /* -ed- qualifier fixed */
              >>> } Structure1;[/color][/color][/color]
              [color=blue]
              > Does it mean
              >
              > 1. The pointer is read only.
              > 2. The structure is read only.[/color]

              It means that the 'struct Type2' pointed by 'Field2' is read only when
              you use Field2 to access it.

              --
              Emmanuel
              The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

              "C is a sharp tool"

              Comment

              • Chris Torek

                #8
                Re: const field in structure

                In article <Pine.LNX.4.6 0-041.04081116495 50.7549@unix48. andrew.cmu.edu>
                Arthur J. O'Dwyer <ajo@nospam.and rew.cmu.edu> writes:[color=blue]
                > For the experts: It surprised me just now to learn that 'gcc -ansi'
                >actually permits
                >
                > struct foo {
                > const int bar;
                > };[/color]

                This fragment is strictly conforming. The "bar" member of a "struct
                foo" is const-qualified and thus read-only. (Note that this is
                different from the original example, which -- after spelling
                corrections -- declared one member as "pointer to const struct
                ....", i.e., a read/write pointer to a read-only object.)
                [color=blue]
                >Does this mean what I think it means --- 'bar' can only be given a
                >value during initialization --- or is this just a quirk of GCC or
                >of the Standard?[/color]

                The member named "bar" can indeed only be given a value via
                initialization (in strictly conforming code anyway -- in practice,
                "going behind the compiler's back" to write on the member tends
                to "work", for some definition of "work" anyway).

                Consider a larger structure:

                struct S {
                int a;
                const int b;
                int c;
                };

                An object of type "struct S" has three members named a, b, and c,
                with a and c being read/write and b being read-only -- but by the
                other rules about structures, the address of b has to be "between"
                that of a and c. On conventional machines with page-granular memory
                protection (e.g., 4096 or more bytes at a time are either read/write
                or read-only), a "struct S" object will have to occupy wholly-read/write
                memory. Thus, "sneaky" code such as:

                struct S s_obj = { 1, 2, 3 };
                ...
                int *p = &s_obj.a;
                p[1] = 99; /* UNDEFINED, but tends to compile and run anyway */

                will tend to overwrite s_obj.b (note that I have assumed there
                is no padding here!). The compiler is of course allowed to *assume*
                that s_obj.b has not changed (in this case) so whether you can
                *see* the change tends to be optimization-level-dependent, for
                instance. (In other words, "don't do this". :-) )

                On another note, many embedded-systems C programmers like to
                use volatile-qualified types for hardware register layout
                data structures:

                struct fooreg {
                volatile int csr;
                volatile int dar;
                /* etc */
                };

                This is also valid, Standard C (although the precise meaning of
                "volatile" is up to the compiler anyway). I do not share their
                enthusaism: I prefer to make the structure contain ordinary,
                unqualified types, and then have the pointer that points to that
                structure carry the qualifier:

                struct fooreg {
                int csr;
                int dar;
                /* etc */
                };
                ...
                volatile struct fooreg *reg = (volatile struct fooreg *)addr;

                I have two reasons for this, one of which I think is easier to
                argue for: the unqualified "struct" version allows you to copy the
                hardware registers to software data structures for debugging, and
                yet get the software-structure access optimized without having the
                optimization interfere with real hardware access. (As it turns
                out, to make device drivers portable across widely different
                architectures, it is often a good idea not to use such structures
                in the first place -- at least not directly, anyway.)
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                • Richard Pennington

                  #9
                  Re: const field in structure

                  Skybuck Flying wrote:
                  [color=blue]
                  > "Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> wrote in message
                  > news:mn.5d2b7d4 8f3543fca.15512 @YOURBRAnoos.fr ...
                  >[color=green]
                  >>Skybuck Flying wrote on 11/08/04 :
                  >>
                  >>[color=darkred]
                  >>>What does Const mean in this c structure ? and what is the delphi[/color][/color]
                  >
                  > equivalent
                  >[color=green][color=darkred]
                  >>>?
                  >>>
                  >>>Struct {
                  >>> Type1 Field1;
                  >>> Const struct Type2 *Field2;
                  >>>} Structure1;
                  >>>[/color]
                  >>
                  >>Nothing I'am aware of. 'Const' is not a C-word. If you are talking of
                  >>'const', it's a different story.It means that the pointer Field2 is
                  >>only allowed to make read acces to the pointee.[/color]
                  >
                  >
                  > Well now I'm confused.
                  >
                  > Does it mean
                  >
                  > 1. The pointer is read only.
                  > 2. The structure is read only.
                  >
                  > ?
                  >
                  > :)
                  >
                  >[/color]

                  Lot's of good answers below this, but...

                  Here is another:

                  const struct Type2 *Field2; // Field2 is a pointer to a const struct Type2.

                  struct Type2 * const Field2; // Field2 is a const pointer to a struct Type2.

                  In the first the struct is read only. In the second the pointer is readonly.

                  -Rich

                  P.S. Am I the only one who has found reading declarations from the
                  inside out helps in C? e.g:
                  int (*f)[10]; // f is a pointer to an array of 10 int.

                  You have to understand the rules of "inside out" unfortunately.

                  --
                  Richard Pennington
                  Email: rich@pennware.c om
                  http://www.pennware.com ftp://ftp.pennware.com

                  Comment

                  • Thomas L.

                    #10
                    Re: const field in structure

                    Emmanuel Delahaye <emdel@YOURBRAn oos.fr> wrote in message news:<mn.5d907d 48693d0b2c.1551 2@YOURBRAnoos.f r>...[color=blue]
                    > Skybuck Flying wrote on 11/08/04 :[color=green][color=darkred]
                    > >>> Struct {
                    > >>> Type1 Field1;
                    > >>> const struct Type2 *Field2; /* -ed- qualifier fixed */
                    > >>> } Structure1;[/color][/color]
                    >[color=green]
                    > > Does it mean
                    > >
                    > > 1. The pointer is read only.
                    > > 2. The structure is read only.[/color]
                    >
                    > It means that the 'struct Type2' pointed by 'Field2' is read only when
                    > you use Field2 to access it.[/color]

                    so:
                    imagine we define somewhere,
                    struct Type2 {
                    int foo;
                    };

                    int bar;
                    struct Structure1 s1;
                    bar = s1.Field2->foo; //Valid because read-only?
                    s1.Field2->foo = bar; //Non valid because Field2 is read-only through
                    s1.Field2

                    struct Type2 *s2_;
                    s2 = s1.Field2; // Is it valid? There is an implicit const cast
                    here
                    s2->foo = bar; // if previous line ok, it is an access to
                    s1.Field2 but via // a different pointer.

                    If the example given is valid (the structure that s1.Field2 points at
                    can be modified provided you do not use the s1.Field2 access), could
                    the restrict keyword help the programmer in having a truly read-only
                    part of Structure1?

                    Thomas

                    Comment

                    Working...