pointer truncation from 'void *' to 'int'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfonso Morra

    pointer truncation from 'void *' to 'int'

    Hi,

    I'm getting a compiler error of "pointer truncation from 'void *' to
    'int'" because I'm not explicitly casting from void* to (datatype*).
    Pointers are stored as ints (is that right?), so as AFAIK, there is
    nothing to worry about is there?

    I'm using VC 7.1

    Why are such assignments being treated as errors?

  • Eric Lavigne

    #2
    Re: pointer truncation from 'void *' to 'int'

    >Hi,[color=blue]
    >
    >I'm getting a compiler error of "pointer truncation from 'void *' to
    >'int'" because I'm not explicitly casting from void* to (datatype*).
    >Pointers are stored as ints (is that right?), so as AFAIK, there is
    >nothing to worry about is there?
    >
    >I'm using VC 7.1
    >
    >Why are such assignments being treated as errors?[/color]

    You are worried about whether converting void* to int really involves
    truncation? I am new to C, but it seems that sizeof would answer this
    question for you. I imagine that the space required for pointers would
    vary from one machine to another, depending on the total amount of
    available memory, while the size of int would more likely just match
    the IEEE recommendation.

    Comment

    • Alexei A. Frounze

      #3
      Re: pointer truncation from 'void *' to 'int'

      "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
      news:dc069t$j00 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
      > Hi,
      >
      > I'm getting a compiler error of "pointer truncation from 'void *' to
      > 'int'" because I'm not explicitly casting from void* to (datatype*).
      > Pointers are stored as ints (is that right?), so as AFAIK, there is
      > nothing to worry about is there?
      >
      > I'm using VC 7.1
      >
      > Why are such assignments being treated as errors?[/color]

      Because sizeof(void*) doesn't necessarily equal to sizeof(int) nor
      sizeof(void(*)( )), it's all platform dependent (and sometimes even compiler
      dependent). And, although a pointer is an integer address of a memory cell,
      semantically its a very different type from an integer, so you shouldn't
      expect the compiler eat your code and doesn't eructate.

      Alex


      Comment

      • Flash Gordon

        #4
        Re: pointer truncation from 'void *' to 'int'

        Alfonso Morra wrote:[color=blue]
        > Hi,
        >
        > I'm getting a compiler error of "pointer truncation from 'void *' to
        > 'int'" because I'm not explicitly casting from void* to (datatype*).[/color]

        That, I believe, is a constraint violation, i.e. an error. There is also
        no guarantee that even if you cast the value it will actually fit in an int.
        [color=blue]
        > Pointers are stored as ints (is that right?),[/color]

        Wrong. Pointer are stored as pointers. These could be the same size as
        ints, larger or smaller. Pointers to consecutive locations could also,
        which converted to a suitable integer type, not be consecutive integers.
        [color=blue]
        > so as AFAIK, there is
        > nothing to worry about is there?[/color]

        There is everything to worry about. Your program is wrong, non-portable,
        and quite likely to break as soon as you try to compile for a 64 bit
        target. There you could find (depending on the model used) that pointers
        are 64 bits wide whilst int is only 32 bits wide, and there is no
        guarantee that *any* integer type is wide enough.
        [color=blue]
        > I'm using VC 7.1
        >
        > Why are such assignments being treated as errors?[/color]

        Because they are errors. Whatever you are trying to do, you are almost
        certainly doing it the wrong way.
        --
        Flash Gordon
        Living in interesting times.
        Although my email address says spam, it is real and I read it.

        Comment

        • Denis Kasak

          #5
          Re: pointer truncation from 'void *' to 'int'

          Alfonso Morra wrote:[color=blue]
          > Hi,
          >
          > I'm getting a compiler error of "pointer truncation from 'void *' to
          > 'int'" because I'm not explicitly casting from void* to (datatype*).
          > Pointers are stored as ints (is that right?), so as AFAIK, there is
          > nothing to worry about is there?
          >
          > I'm using VC 7.1
          >
          > Why are such assignments being treated as errors?[/color]

          There is no guarantee that pointers on your implementation are even
          representable as int. Pointer to int conversion is implementation
          defined and as such cannot be relied upon.

          -- Denis

          Comment

          • Emmanuel Delahaye

            #6
            Re: pointer truncation from 'void *' to 'int'

            Alfonso Morra wrote on 24/07/05 :[color=blue]
            > I'm getting a compiler error of "pointer truncation from 'void *' to 'int'"
            > because I'm not explicitly casting from void* to (datatype*). Pointers are
            > stored as ints (is that right?),[/color]

            Saiz who ? This statement is plain wrong. Pointers are stored as ...
            pointers.
            [color=blue]
            > so as AFAIK, there is nothing to worry about
            > is there?
            >
            > I'm using VC 7.1
            >
            > Why are such assignments being treated as errors?[/color]

            Because they are potential errors. In a 16 bit x386 machine, you can
            have 16-int and 32-bit pointers.


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

            "C is a sharp tool"


            Comment

            • Martin Ambuhl

              #7
              Re: pointer truncation from 'void *' to 'int'

              Alfonso Morra wrote:[color=blue]
              > Hi,
              >
              > I'm getting a compiler error of "pointer truncation from 'void *' to
              > 'int'" because I'm not explicitly casting from void* to (datatype*).
              > Pointers are stored as ints (is that right?),[/color]

              Wrong. Pointers are stored as pointers.
              [color=blue]
              > so as AFAIK, there is
              > nothing to worry about is there?[/color]

              Yes, there is.
              [color=blue]
              > I'm using VC 7.1
              >
              > Why are such assignments being treated as errors?[/color]

              Because they are erroneous.

              Comment

              • Charles F McDevitt

                #8
                Re: pointer truncation from 'void *' to 'int'


                "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
                news:dc069t$j00 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
                > Hi,
                >
                > I'm getting a compiler error of "pointer truncation from 'void *' to
                > 'int'" because I'm not explicitly casting from void* to (datatype*).
                > Pointers are stored as ints (is that right?), so as AFAIK, there is
                > nothing to worry about is there?
                >
                > I'm using VC 7.1
                >
                > Why are such assignments being treated as errors?
                >[/color]

                Altough the C standard doesn't say so, *most* (99.9%) of C implementations
                have sizeof(long)==s izeof(void *),
                so you can convert pointer to long and back again.


                Comment

                • Charles F McDevitt

                  #9
                  Re: pointer truncation from 'void *' to 'int'


                  "Charles F McDevitt" <Chuck_McDevitt @comcast.net> wrote in message
                  news:ldydnRYXAf VWfH7fRVn-1w@comcast.com. ..[color=blue]
                  >
                  > "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
                  > news:dc069t$j00 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=green]
                  >> Hi,
                  >>
                  >> I'm getting a compiler error of "pointer truncation from 'void *' to
                  >> 'int'" because I'm not explicitly casting from void* to (datatype*).
                  >> Pointers are stored as ints (is that right?), so as AFAIK, there is
                  >> nothing to worry about is there?
                  >>
                  >> I'm using VC 7.1
                  >>
                  >> Why are such assignments being treated as errors?
                  >>[/color]
                  >
                  > Altough the C standard doesn't say so, *most* (99.9%) of C implementations
                  > have sizeof(long)==s izeof(void *),
                  > so you can convert pointer to long and back again.
                  >[/color]

                  Oopss.. I means to say sizeof(long)>=s izeof(void *)


                  Comment

                  • Eric Sosman

                    #10
                    Re: pointer truncation from 'void *' to 'int'

                    Charles F McDevitt wrote:[color=blue]
                    > "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
                    > news:dc069t$j00 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
                    >[color=green]
                    >>Hi,
                    >>
                    >>I'm getting a compiler error of "pointer truncation from 'void *' to
                    >>'int'" because I'm not explicitly casting from void* to (datatype*).
                    >>Pointers are stored as ints (is that right?), so as AFAIK, there is
                    >>nothing to worry about is there?
                    >>
                    >>I'm using VC 7.1
                    >>
                    >>Why are such assignments being treated as errors?
                    >>[/color]
                    >
                    >
                    > Altough the C standard doesn't say so, *most* (99.9%) of C implementations
                    > have sizeof(long)==s izeof(void *),[/color]

                    Balderdash.

                    Besides, what has sizeof got do do with it? If sizeof(void*)
                    happens to equal sizeof(float) or sizeof(double), what conclusion
                    can you draw about interconvertibi lity?

                    --
                    Eric Sosman
                    esosman@acm-dot-org.invalid

                    Comment

                    • Barry Schwarz

                      #11
                      Re: pointer truncation from 'void *' to 'int'

                      On Sun, 24 Jul 2005 13:49:49 +0000 (UTC), Alfonso Morra
                      <sweet-science@the-ring.com> wrote:
                      [color=blue]
                      >Hi,
                      >
                      >I'm getting a compiler error of "pointer truncation from 'void *' to
                      >'int'" because I'm not explicitly casting from void* to (datatype*).[/color]

                      Converting from void* to datatype* does not involve any integer
                      conversion. Furthermore, a cast is not necessary. Your assumption
                      about the cause of the error message is suspect.
                      [color=blue]
                      >Pointers are stored as ints (is that right?), so as AFAIK, there is[/color]

                      While the representation of a pointer value may coincide with the
                      representation of an integer value, the two types are completely
                      distinct. Pointers are stored in objects with type pointer to T and
                      integers are stored in objects with type int (or long or short, etc).
                      [color=blue]
                      >nothing to worry about is there?[/color]

                      If there was nothing to worry about, the compiler would probably not
                      be bringing it to your attention (though not universally true). In
                      this case, it definitely is something to worry about.
                      [color=blue]
                      >
                      >I'm using VC 7.1[/color]

                      Probably not relevant.
                      [color=blue]
                      >
                      >Why are such assignments being treated as errors?[/color]

                      You apparently did something unintended. Show us your code.


                      <<Remove the del for email>>

                      Comment

                      • Keith Thompson

                        #12
                        Re: pointer truncation from 'void *' to 'int'

                        "Charles F McDevitt" <Chuck_McDevitt @comcast.net> writes:[color=blue]
                        > "Charles F McDevitt" <Chuck_McDevitt @comcast.net> wrote in message
                        > news:ldydnRYXAf VWfH7fRVn-1w@comcast.com. ..[/color]
                        [...][color=blue][color=green]
                        >> Altough the C standard doesn't say so, *most* (99.9%) of C implementations
                        >> have sizeof(long)==s izeof(void *),
                        >> so you can convert pointer to long and back again.
                        >>[/color]
                        >
                        > Oopss.. I means to say sizeof(long)>=s izeof(void *)[/color]

                        Which means you can write code that will fail mysteriously as soon as
                        it's ported to a system where sizeof(long) < sizeof(void*).

                        Or you can write portable code that will work on any platform.

                        The language allows conversions between pointers and integers, but
                        there's rarely any good reason to use such conversions. 99.9% of the
                        time, it's better just to treat pointers as pointers.

                        --
                        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.

                        Comment

                        • Keith Thompson

                          #13
                          Re: pointer truncation from 'void *' to 'int'

                          "Alexei A. Frounze" <alexfru@chat.r u> writes:[color=blue]
                          > "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
                          > news:dc069t$j00 $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=green]
                          >> Hi,
                          >>
                          >> I'm getting a compiler error of "pointer truncation from 'void *' to
                          >> 'int'" because I'm not explicitly casting from void* to (datatype*).
                          >> Pointers are stored as ints (is that right?), so as AFAIK, there is
                          >> nothing to worry about is there?
                          >>
                          >> I'm using VC 7.1
                          >>
                          >> Why are such assignments being treated as errors?[/color]
                          >
                          > Because sizeof(void*) doesn't necessarily equal to sizeof(int) nor
                          > sizeof(void(*)( )), it's all platform dependent (and sometimes even compiler
                          > dependent). And, although a pointer is an integer address of a memory cell,
                          > semantically its a very different type from an integer, so you shouldn't
                          > expect the compiler eat your code and doesn't eructate.[/color]

                          No, a pointer is not necessarily an integer address of a memory cell.
                          It is on many systems, but the standard makes no such guarantee, and
                          there are real-world systems where the assumption breaks down.

                          --
                          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.

                          Comment

                          • Alexei A. Frounze

                            #14
                            Re: pointer truncation from 'void *' to 'int'

                            "Keith Thompson" <kst-u@mib.org> wrote in message
                            news:ln64v0ou3h .fsf@nuthaus.mi b.org...
                            ....[color=blue]
                            > No, a pointer is not necessarily an integer address of a memory cell.
                            > It is on many systems, but the standard makes no such guarantee, and
                            > there are real-world systems where the assumption breaks down.[/color]

                            What is it then? Just give a few examples.

                            Alex


                            Comment

                            • Robert W Hand

                              #15
                              Re: pointer truncation from 'void *' to 'int'

                              On Mon, 25 Jul 2005 00:00:17 +0400, "Alexei A. Frounze"
                              <alexfru@chat.r u> wrote:
                              [color=blue]
                              >"Keith Thompson" <kst-u@mib.org> wrote in message
                              >news:ln64v0ou3 h.fsf@nuthaus.m ib.org...
                              >...[color=green]
                              >> No, a pointer is not necessarily an integer address of a memory cell.
                              >> It is on many systems, but the standard makes no such guarantee, and
                              >> there are real-world systems where the assumption breaks down.[/color]
                              >
                              >What is it then? Just give a few examples.[/color]

                              In the old DOS 16-bit world, there were near and far pointers. Far
                              pointers had an offset added in, IIRC. It was possible for different
                              "integer" values to point to the same address.
                              --

                              Best wishes,

                              Bob

                              Comment

                              Working...