pointer dereference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Heathfield

    #16
    Re: pointer dereference

    santosh said:
    Richard Heathfield wrote:
    >santosh said:
    >>
    <snip>
    >
    * A pointer of type void should be able to hold the highest
    possible
    address of the program's address space.
    >>
    >A pointer of type void *, actually. [...]
    >
    Maybe this is a language issue. Would I be correct when I say "A
    pointer to type void"? I don't think so.
    Better: "a pointer of type void *"
    So I suppose instead of
    saying, for example, "a pointer to type int", I should say "a pointer
    of type int *" or, better yet, "an int pointer"...?
    Either works. Personally I prefer the "a pointer of type <type>" style,
    but "a <typepointer" is also correct.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Keith Thompson

      #17
      Re: pointer dereference

      santosh <santosh.k83@gm ail.comwrites:
      [...]
      Maybe this is a language issue. Would I be correct when I say "A
      pointer to type void"? I don't think so. So I suppose instead of
      saying, for example, "a pointer to type int", I should say "a pointer
      of type int *" or, better yet, "an int pointer"...?
      In my opinion, "a pointer to type void" or "a pointer to type int" is
      ok, but "a pointer of type void*" or "a pointer of type int*" is
      better. Pointer objects don't point to types, they point to objects.
      Pointer types don't point to anything, but objects and values of
      pointer types may point to objects. But we can colloquially say that
      type int* points to type int; it's not 100% accurate, but it's not
      ambiguous, and I wouldn't complain about the usage unless I was
      invited to.

      I wouldn't use the phrase "an int pointer" because it could imply
      something that's both an int and a pointer, rather than something that
      points to an int.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Keith Thompson

        #18
        Re: pointer dereference

        Chris Dollin <chris.dollin@h p.comwrites:
        Christopher Benson-Manica wrote:
        >Chris Dollin <chris.dollin@h p.comwrote:
        >>
        >>I think it's more reasonable to claim that there's exactly one
        >>value of type void -- we could call it, oh I dunno, sponk or flolo
        >>or `unit` -- which is conveniently represented with zero
        >>bits. (Not bits that are zero, of course.)
        >>
        >It seems like that value could be called "the null value" without too
        >much confusion (no more, or less, than with the common term "the
        >null character"). That said, I read n869 6.2.5 p18 - "The void type
        >comprises an empty set of values..." - as explicitly contradicting the
        >notion of there being any values of type void.
        >
        I think it's more reasonable to claim that there's exactly one value of
        type void -- ie, I think that the Standard's statement otherwise is
        a less-optimal choice. But this is discussion about the Standard rather
        than the language it defines ...
        I disagree; I think it would be unreasonable to say that there's a
        value of type void.

        Why can't you write this?

        void *p = /* whatever */
        void obj = *p;
        void func(void);
        obj = func();

        If we say that type void has no values, the answer is obvious. If we
        say it has one value, we need special-case rules to say that we can't
        use that value, and that sizeof(void) is not 0 but a constraint
        violation.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Mark McIntyre

          #19
          Re: pointer dereference

          On Thu, 12 Jul 2007 01:55:13 -0700, in comp.lang.c , somenath
          <somenathpal@gm ail.comwrote:
          >Hi All,
          >I have one doubt regarding pointer .I have one small code as
          >mentioned bellow .
          >
          >#include<stdio .h>
          >#include<stdli b.h>
          >int main (void)
          >{
          int *p;
          int *ptr= malloc(sizeof(i nt *));
          return 0;
          >}
          >My doubt is if (sizeof(int *)); is undefined ?
          sizeof(int*) can't be undefined. It is a constant, available at
          compile-time to your compiler from its knowledge of the size of types
          on your system.
          >Because "p" is point to any where .
          p isn't used in your code. You don't need it.



          --
          Mark McIntyre

          "Debugging is twice as hard as writing the code in the first place.
          Therefore, if you write the code as cleverly as possible, you are,
          by definition, not smart enough to debug it."
          --Brian Kernighan

          Comment

          • Richard Bos

            #20
            Re: pointer dereference

            Chris Dollin <chris.dollin@h p.comwrote:
            Christopher Benson-Manica wrote:
            >
            Chris Dollin <chris.dollin@h p.comwrote:
            I think it's more reasonable to claim that there's exactly one value of
            type void -- we could call it, oh I dunno, sponk or flolo or `unit` -- which
            is conveniently represented with zero bits. (Not bits that are zero, of
            course.)
            It seems like that value could be called "the null value" without too
            much confusion (no more, or less, than with the common term "the
            null character"). That said, I read n869 6.2.5 p18 - "The void type
            comprises an empty set of values..." - as explicitly contradicting the
            notion of there being any values of type void.
            >
            I think it's more reasonable to claim that there's exactly one value of
            type void -- ie, I think that the Standard's statement otherwise is
            a less-optimal choice.
            I think that's not reasonable at all. void is the empty set, and the
            empty set has no members, not even a single, unique, empty member.

            Richard

            Comment

            • Chris Dollin

              #21
              Re: pointer dereference

              Richard Bos wrote:
              Chris Dollin <chris.dollin@h p.comwrote:
              >I think it's more reasonable to claim that there's exactly one value of
              >type void -- ie, I think that the Standard's statement otherwise is
              >a less-optimal choice.
              >
              I think that's not reasonable at all. void is the empty set, and the
              empty set has no members, not even a single, unique, empty member.
              `void` isn't the empty set. It's a type (name). Types with no elements
              are a pain. You may wish to identify the type with the empty set, but
              I'd bet money that dollin@hp wouldn't make that identification.

              There is, after all, a difference between a function [1] that has
              no result, and a function that has a trivial result.

              [1] Not necessarily a /C/ function.

              --
              Far-Fetched Hedgehog
              Notmuchhere: http://www.electrichedgehog.net/

              Comment

              • Keith Thompson

                #22
                Re: pointer dereference

                rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                Chris Dollin <chris.dollin@h p.comwrote:
                >Christopher Benson-Manica wrote:
                Chris Dollin <chris.dollin@h p.comwrote:
                >
                >I think it's more reasonable to claim that there's exactly one
                >value of type void -- we could call it, oh I dunno, sponk or
                >flolo or `unit` -- which is conveniently represented with zero
                >bits. (Not bits that are zero, of course.)
                >
                It seems like that value could be called "the null value" without too
                much confusion (no more, or less, than with the common term "the
                null character"). That said, I read n869 6.2.5 p18 - "The void type
                comprises an empty set of values..." - as explicitly contradicting the
                notion of there being any values of type void.
                >>
                >I think it's more reasonable to claim that there's exactly one value of
                >type void -- ie, I think that the Standard's statement otherwise is
                >a less-optimal choice.
                >
                I think that's not reasonable at all. void is the empty set, and the
                empty set has no members, not even a single, unique, empty member.
                And if you want a type with a single value:

                enum { zero };

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • Chris Dollin

                  #23
                  Re: pointer dereference

                  Keith Thompson wrote:
                  Chris Dollin <chris.dollin@h p.comwrites:
                  >I think it's more reasonable to claim that there's exactly one value of
                  >type void -- ie, I think that the Standard's statement otherwise is
                  >a less-optimal choice. But this is discussion about the Standard rather
                  >than the language it defines ...
                  >
                  I disagree; I think it would be unreasonable to say that there's a
                  value of type void.
                  >
                  Why can't you write this?
                  >
                  void *p = /* whatever */
                  void obj = *p;
                  void func(void);
                  obj = func();
                  >
                  If we say that type void has no values, the answer is obvious. If we
                  say it has one value, we need special-case rules to say that we can't
                  use that value, and that sizeof(void) is not 0 but a constraint
                  violation.
                  Your argument is of the form "if there was a value of type void, then
                  the following incorrect-C-code would need special-case rules to
                  explain its incorrectness".

                  My response is "if there were a value of type void, then that C code
                  wouldn't need to be illegal, so no special explanation of its illegality
                  would be required".

                  When I said:
                  >I think that the Standard's statement otherwise is a less-optimal choice
                  I meant that had the Standard chosen void-has-one-value then the Standard
                  as a whole would have been better (and the language no worse).

                  --
                  RIP Donald Michie 11 November 1923 - 7 July 2007

                  Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                  registered office: Berks RG12 1HN 690597 England

                  Comment

                  • Chris Dollin

                    #24
                    Re: pointer dereference

                    Chris Dollin wrote:
                    Keith Thompson wrote:
                    >
                    >Chris Dollin <chris.dollin@h p.comwrites:
                    >
                    >>I think it's more reasonable to claim that there's exactly one value of
                    >>type void -- ie, I think that the Standard's statement otherwise is
                    >>a less-optimal choice. But this is discussion about the Standard rather
                    >>than the language it defines ...
                    (fx:snip)
                    When I said:
                    >
                    >>I think that the Standard's statement otherwise is a less-optimal choice
                    >
                    I meant that had the Standard chosen void-has-one-value then the Standard
                    as a whole would have been better (and the language no worse).
                    And when I said
                    >>But this is discussion about the Standard rather
                    >>than the language it defines ...
                    by "the language" I meant to mean C-as-is-currently-defined, ie, that
                    there was wandering on the edges of, if not outside, topicality. I
                    didn't mean to imply that the-Standard-with-singleton-void would
                    define exactly the same language as the-real-Standard.

                    --
                    RIP Donald Michie 11 November 1923 - 7 July 2007

                    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                    registered no: 690597 England Berks RG12 1HN

                    Comment

                    • Army1987

                      #25
                      [OT] Re: pointer dereference

                      On Fri, 13 Jul 2007 06:39:49 +0000, Chris Dollin wrote:
                      Richard Bos wrote:
                      There is, after all, a difference between a function [1] that has
                      no result, and a function that has a trivial result.
                      >
                      [1] Not necessarily a /C/ function.
                      In the non-C meaning of the word, there is no such thing as a
                      function with no result, unless it is the empty function (and so
                      its domain, as well as its codomain, is empty). There is no
                      function with empty codomain and nonempty domain.
                      --
                      Army1987 (Replace "NOSPAM" with "email")
                      "Never attribute to malice that which can be adequately explained
                      by stupidity." -- R. J. Hanlon (?)

                      Comment

                      • Christopher Benson-Manica

                        #26
                        Re: pointer dereference

                        Chris Dollin <chris.dollin@h p.comwrote:
                        Keith Thompson wrote:
                        Chris Dollin <chris.dollin@h p.comwrites:
                        void *p = /* whatever */
                        void obj = *p;
                        Your argument is of the form "if there was a value of type void, then
                        the following incorrect-C-code would need special-case rules to
                        explain its incorrectness".
                        My response is "if there were a value of type void, then that C code
                        wouldn't need to be illegal, so no special explanation of its illegality
                        would be required".
                        I meant that had the Standard chosen void-has-one-value then the Standard
                        as a whole would have been better (and the language no worse).
                        I think if you follow Keith's example a bit further, the "language no
                        worse" argument begins to break down:

                        void *p=&foo;
                        void *q=&bar;
                        void r=*p; /* legal given your arguments */
                        void s=*q; /* also legal */
                        if( r == s ) {
                        /* MUST be true, unless there is more than one value of type void,
                        but what's the point? */
                        }

                        Unless I'm mistaken, void r=*p would require the language to define
                        conversion from values of all types to this magical single value of
                        void. Obviously, restoring an object of foo's type from r would be
                        impossible. The conversion of values of other types to this void type
                        would also (modulo grains of salt) allow such silly statements as

                        void t=3; /* ridiculous */

                        A language that disallows such inherently meaningless constructions
                        would seem to be better than one that permits them, yes? I certainly
                        don't see any value gain from introducing a special value of type
                        void.

                        --
                        C. Benson Manica | I *should* know what I'm talking about - if I
                        cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

                        Comment

                        • Chris Dollin

                          #27
                          Re: pointer dereference

                          Christopher Benson-Manica wrote:

                          (responding to dollin@hp)
                          I think if you follow Keith's example a bit further, the "language no
                          worse" argument begins to break down:
                          >
                          void *p=&foo;
                          void *q=&bar;
                          void r=*p; /* legal given your arguments */
                          void s=*q; /* also legal */
                          if( r == s ) {
                          /* MUST be true, unless there is more than one value of type void,
                          but what's the point? */
                          }
                          What's the point of `if(1) ...`? Of `x = x;`? Of `printf("");`?
                          It's all the same thing -- trivial cases of general rules.
                          Unless I'm mistaken, void r=*p would require the language to define
                          conversion from values of all types to this magical single value of
                          void.
                          If it were allowed (it need not be, just as you can't assign a
                          struct to an int -- that's a separate decision). Yes. So?
                          The conversion is one of the easier ones to understand ...
                          Obviously, restoring an object of foo's type from r would be
                          impossible.
                          That's right. Just as, after `int x = 12345; char y = x;` you
                          can't restore the value of `x` from the value of `y`.
                          The conversion of values of other types to this void type
                          would also (modulo grains of salt) allow such silly statements as
                          >
                          void t=3; /* ridiculous */
                          It's not /ridiculous/, or /silly/, any more than adding `0` or
                          multiplying by `1`. It's /trivial/.
                          A language that disallows such inherently meaningless constructions
                          It's /not meaningless/. It has a perfectly well-defined and implementable
                          semantics -- which one, depends on whether conversion to void is
                          implicit or not.
                          would seem to be better than one that permits them, yes? I certainly
                          don't see any value gain from introducing a special value of type
                          void.
                          Simplification of the description and use of the language. Most
                          of the special cases of use of void disappear, or are replaced
                          by obviously-degenerate cases.

                          --
                          Singular Hedgehog
                          "I just wonder when we're going to have to sit down and re-evaluate
                          our decision-making paradigm." /Sahara/

                          Comment

                          • Chris Torek

                            #28
                            Re: pointer dereference

                            >Chris Dollin <chris.dollin@h p.comwrote:
                            >I meant that had the Standard chosen void-has-one-value then the Standard
                            >as a whole would have been better (and the language no worse).
                            I think so too, although the differene is pretty marginal (in my opinion).

                            In article <f78250$d28$1@c hessie.cirr.com >,
                            Christopher Benson-Manica <ataru@vinland. freeshell.orgwr ote:
                            >I think if you follow Keith's example a bit further, the "language no
                            >worse" argument begins to break down:
                            >
                            >void *p=&foo;
                            >void *q=&bar;
                            >void r=*p; /* legal given your arguments */
                            >void s=*q; /* also legal */
                            >if( r == s ) {
                            /* MUST be true, unless there is more than one value of type void,
                            but what's the point? */
                            >}
                            I do not see this as a problem. In a language "like C but with
                            sizeof(void)==0 and in which the one value of type void, which
                            occupies no bits, appears to be zero at all times" (which I have
                            proposed before), we get:

                            r == s
                            r == 0
                            r != 3

                            Values of type "void" can be used in any integer or boolean context
                            and simply appear to be zero. Values can be converted to "void",
                            by cast or assignment; this discards all their bits.

                            One can also do:

                            void *p = &foo;
                            ...
                            p++; /* but afterward, p==&foo */
                            >Unless I'm mistaken, void r=*p would require the language to define
                            >conversion from values of all types to this magical single value of
                            >void.
                            It is not very "magical", it is just 0. The conversion would be
                            defined for integers, at least, and maybe for pointers as well (a
                            la C99's boolean type), but probably not for struct and union.
                            >... void t=3; /* ridiculous */
                            And yet, in C as it stands today, we can write:

                            (void) 3;

                            which is just as ridiculous. Why can we *cast* to void, but not
                            *assign* to void?

                            The main weirdness with variables of type void (in this not-quite-
                            but-almost-C language) is that, because sizeof(void) is 0, given:

                            void a;
                            int b;
                            void c;
                            int d;

                            it is possible to have &a == &b, &a == &c, and/or &a == &d (but
                            &b != &d, so if &a==&b then &a!=&d). (This same situation occurs
                            today in GNUC, though, using arrays of size 0.)
                            --
                            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

                            • Army1987

                              #29
                              Re: pointer dereference

                              On Fri, 13 Jul 2007 18:10:28 +0000, Chris Torek wrote:
                              The main weirdness with variables of type void (in this not-quite-
                              but-almost-C language) is that, because sizeof(void) is 0, given:
                              >
                              void a;
                              int b;
                              void c;
                              int d;
                              >
                              it is possible to have &a == &b, &a == &c, and/or &a == &d (but
                              &b != &d, so if &a==&b then &a!=&d). (This same situation occurs
                              today in GNUC, though, using arrays of size 0.)
                              And malloc(0) would then make sense.
                              --
                              Army1987 (Replace "NOSPAM" with "email")
                              "Never attribute to malice that which can be adequately explained
                              by stupidity." -- R. J. Hanlon (?)

                              Comment

                              • Keith Thompson

                                #30
                                Re: pointer dereference

                                Chris Dollin <eh@electriched gehog.netwrites :
                                [...]
                                Simplification of the description and use of the language. Most
                                of the special cases of use of void disappear, or are replaced
                                by obviously-degenerate cases.
                                None if which, as far as I can see, would be at all useful.

                                Currently, there are a lot of things you *could* write in a C program
                                assuming there's a value of type void. Most of them are constraint
                                violations, emaning that the compiler will tell you about your error.
                                And they're not special cases; they follow straightforward ly from the
                                fact that void is an incomplete type. We need rules for incomplete
                                types anyway, to cover things like 'struct foo' before the full
                                declaration.

                                --
                                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                                "We must do something. This is something. Therefore, we must do this."
                                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                                Comment

                                Working...