sizeof(ptr) = ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Dollin

    #16
    Re: sizeof(ptr) = ?

    Richard wrote:
    Barry Schwarz <schwarz45@yaho o.comwrites:
    >>Pointers of any type will have the same size because they hold the address
    >>value of memory that they point to, thus they always hold the same type of
    >>data. On standard PC's this is a 32bit address (4 bytes), but it depends on
    >>the system what the size is.
    >>
    >While this may be true in your experience, the C standard imposes no
    >such restriction. Furthermore, there are systems for which it is not
    >true.
    >
    Could you explain how malloc works for such systems please.
    It returns a `void*`.

    (If that's not enough of an answer, you'll have to unpack your question.)

    --
    Chris "`essen`, vb, 'to buy many games'" Dollin

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

    Comment

    • santosh

      #17
      Re: sizeof(ptr) = ?

      Richard wrote:
      Barry Schwarz <schwarz45@yaho o.comwrites:
      >
      >On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
      >>I test it with char, short, int, long, float, double and struct
      >pointers, all return 4 bytes. Does the standard say anything about
      >sizeof a pointer? (I didn't find it in the standard...)
      >>>
      >--
      >William
      >>>
      >>Pointers of any type will have the same size because they hold the
      >>address value of memory that they point to, thus they always hold
      >>the same type of data. On standard PC's this is a 32bit address (4
      >>bytes), but it depends on the system what the size is.
      >>
      >While this may be true in your experience, the C standard imposes no
      >such restriction. Furthermore, there are systems for which it is not
      >true.
      >
      Could you explain how malloc works for such systems please.
      Because void * is guaranteed to be able to store values of all object
      pointer types.

      Comment

      • Nick Keighley

        #18
        Re: sizeof(ptr) = ?

        On 22 Oct, 12:51, Barry Schwarz <schwar...@yaho o.comwrote:

        <snip>
        (I wonder if there is enough memory in the entire
        world to fully populate a system with a 64 BYTE pointer.)
        64 x 8 = 512 bits

        a 512 bit address can address 2^512 locations

        2^512 ~ 10^154

        the number of atoms in the observable universe is estimated to be
        ~10^80

        so no you couldn't populate your system if you confine yourself to
        the
        observable universe


        --
        Nick Keighley


        Comment

        • Eric Sosman

          #19
          Re: sizeof(ptr) = ?

          William Xu wrote:
          I test it with char, short, int, long, float, double and struct
          pointers, all return 4 bytes. Does the standard say anything about
          sizeof a pointer? (I didn't find it in the standard...)
          (Some of these points have been made in other responses,
          but a few don't seem to have appeared yet.)

          The Standard guarantees that

          - Any kind of data pointer can be converted to `void*'
          and back without changing its value (this implies that
          no data pointer has more value bits than `void*')

          - Any function pointer can be converted to any other type
          of function pointer and back without changing value
          (hence, all function pointers have the same number of
          value bits)

          - The value 0 (the "null pointer constant") is valid for
          all pointer types, both data pointers and function pointers

          - `void*' and pointers to all three kinds of `char'
          have the same size and representation (whatever it is)

          - All pointers to all struct types have the same size
          and representation (whatever it is)

          - All pointers to all union types have the same size
          and representation (whatever it is)

          - All non-bit-field objects (hence, all pointers) occupy
          at least one byte each

          - A qualified type (`const', etc.) has the same size and
          representation as its unqualified cousin

          The Standard does *not* guarantee that all pointers have
          the same representation or even the same size, and does *not*
          guarantee that function pointers can be converted to data
          pointers or vice versa.

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

          Comment

          • Mark Bluemel

            #20
            Re: sizeof(ptr) = ?

            santosh wrote:
            Richard wrote:
            >
            >Barry Schwarz <schwarz45@yaho o.comwrites:
            >>
            >>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
            >>>Pointers of any type will have the same size because they hold the
            >>>address value of memory that they point to, thus they always hold
            >>>the same type of data. On standard PC's this is a 32bit address (4
            >>>bytes), but it depends on the system what the size is.
            >>While this may be true in your experience, the C standard imposes no
            >>such restriction. Furthermore, there are systems for which it is not
            >>true.
            >Could you explain how malloc works for such systems please.
            Because void * is guaranteed to be able to store values of all object
            pointer types.
            People do seem to get very worried when they find that some of what they
            thought they knew is in fact false, don't they?

            The point here is that all the standard requires is that

            a) there must be a pointer representation (void *) which is capable of
            pointing to any category of data. malloc() returns its result in such a
            pointer.

            b) there must be a valid way of converting from void * to any other
            pointer type.

            c) malloc() must ensure that the memory it allocates is suitably aligned
            for any data type.

            Nothing requires that the same representation should be used for both
            the generic pointer type (void *) and all other (data) pointer types.

            --
            Mark "And ASCII isn't required by the standard either" Bluemel

            Comment

            • Richard

              #21
              Re: sizeof(ptr) = ?

              santosh <santosh.k83@gm ail.comwrites:
              Richard wrote:
              >
              >Barry Schwarz <schwarz45@yaho o.comwrites:
              >>
              >>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
              >>>I test it with char, short, int, long, float, double and struct
              >>pointers, all return 4 bytes. Does the standard say anything about
              >>sizeof a pointer? (I didn't find it in the standard...)
              >>>>
              >>--
              >>William
              >>>>
              >>>Pointers of any type will have the same size because they hold the
              >>>address value of memory that they point to, thus they always hold
              >>>the same type of data. On standard PC's this is a 32bit address (4
              >>>bytes), but it depends on the system what the size is.
              >>>
              >>While this may be true in your experience, the C standard imposes no
              >>such restriction. Furthermore, there are systems for which it is not
              >>true.
              >>
              >Could you explain how malloc works for such systems please.
              >
              Because void * is guaranteed to be able to store values of all object
              pointer types.
              But we are talking about values of "special memory blocks" where the
              pointers are different. How does malloc know which "memory type" to
              allocate?

              Comment

              • santosh

                #22
                Re: sizeof(ptr) = ?

                Richard wrote:
                santosh <santosh.k83@gm ail.comwrites:
                >
                >Richard wrote:
                >>
                >>Barry Schwarz <schwarz45@yaho o.comwrites:
                >>>
                >>>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                >>>>I test it with char, short, int, long, float, double and struct
                >>>pointers, all return 4 bytes. Does the standard say anything
                >>>about sizeof a pointer? (I didn't find it in the standard...)
                >>>>>
                >>>--
                >>>William
                >>>>>
                >>>>Pointers of any type will have the same size because they hold the
                >>>>address value of memory that they point to, thus they always hold
                >>>>the same type of data. On standard PC's this is a 32bit address (4
                >>>>bytes), but it depends on the system what the size is.
                >>>>
                >>>While this may be true in your experience, the C standard imposes
                >>>no
                >>>such restriction. Furthermore, there are systems for which it is
                >>>not true.
                >>>
                >>Could you explain how malloc works for such systems please.
                >>
                >Because void * is guaranteed to be able to store values of all object
                >pointer types.
                >
                But we are talking about values of "special memory blocks" where the
                pointers are different. How does malloc know which "memory type" to
                allocate?
                It doesn't. It allocates memory suitable for all types. After the
                conversion to an appropriate pointer type, the memory block is accessed
                as an array of that type.

                Comment

                • Richard

                  #23
                  Re: sizeof(ptr) = ?

                  santosh <santosh.k83@gm ail.comwrites:
                  Richard wrote:
                  >
                  >santosh <santosh.k83@gm ail.comwrites:
                  >>
                  >>Richard wrote:
                  >>>
                  >>>Barry Schwarz <schwarz45@yaho o.comwrites:
                  >>>>
                  >>>>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                  >>>>>I test it with char, short, int, long, float, double and struct
                  >>>>pointers, all return 4 bytes. Does the standard say anything
                  >>>>about sizeof a pointer? (I didn't find it in the standard...)
                  >>>>>>
                  >>>>--
                  >>>>William
                  >>>>>>
                  >>>>>Pointers of any type will have the same size because they hold the
                  >>>>>address value of memory that they point to, thus they always hold
                  >>>>>the same type of data. On standard PC's this is a 32bit address (4
                  >>>>>bytes), but it depends on the system what the size is.
                  >>>>>
                  >>>>While this may be true in your experience, the C standard imposes
                  >>>>no
                  >>>>such restriction. Furthermore, there are systems for which it is
                  >>>>not true.
                  >>>>
                  >>>Could you explain how malloc works for such systems please.
                  >>>
                  >>Because void * is guaranteed to be able to store values of all object
                  >>pointer types.
                  >>
                  >But we are talking about values of "special memory blocks" where the
                  >pointers are different. How does malloc know which "memory type" to
                  >allocate?
                  >
                  It doesn't. It allocates memory suitable for all types. After the
                  conversion to an appropriate pointer type, the memory block is accessed
                  as an array of that type.
                  How can you allocate memory for "all types"? Where is this "special
                  block of memory with extra bits for padding" case?

                  Comment

                  • Richard Bos

                    #24
                    Re: sizeof(ptr) = ?

                    Richard <rgrdev@gmail.c omwrote:
                    santosh <santosh.k83@gm ail.comwrites:
                    >
                    Richard wrote:
                    Barry Schwarz <schwarz45@yaho o.comwrites:
                    >
                    >On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                    >>I test it with char, short, int, long, float, double and struct
                    >pointers, all return 4 bytes. Does the standard say anything about
                    >sizeof a pointer? (I didn't find it in the standard...)
                    >>>
                    >>Pointers of any type will have the same size because they hold the
                    >>address value of memory that they point to, thus they always hold
                    >>the same type of data. On standard PC's this is a 32bit address (4
                    >>bytes), but it depends on the system what the size is.
                    >>
                    >While this may be true in your experience, the C standard imposes no
                    >such restriction. Furthermore, there are systems for which it is not
                    >true.
                    >
                    Could you explain how malloc works for such systems please.
                    Because void * is guaranteed to be able to store values of all object
                    pointer types.
                    >
                    But we are talking about values of "special memory blocks" where the
                    pointers are different.
                    No, we're not. We weren't the last time you spouted this nonsensical
                    misinterpretati on of C's pointer system, and we still aren't. Nor will
                    we next time you will stubbornly refuse to learn.

                    Richard

                    Comment

                    • Richard

                      #25
                      Re: sizeof(ptr) = ?

                      rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                      Richard <rgrdev@gmail.c omwrote:
                      >
                      >santosh <santosh.k83@gm ail.comwrites:
                      >>
                      Richard wrote:
                      >
                      >Barry Schwarz <schwarz45@yaho o.comwrites:
                      >>
                      >>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                      >>>I test it with char, short, int, long, float, double and struct
                      >>pointers, all return 4 bytes. Does the standard say anything about
                      >>sizeof a pointer? (I didn't find it in the standard...)
                      >>>>
                      >>>Pointers of any type will have the same size because they hold the
                      >>>address value of memory that they point to, thus they always hold
                      >>>the same type of data. On standard PC's this is a 32bit address (4
                      >>>bytes), but it depends on the system what the size is.
                      >>>
                      >>While this may be true in your experience, the C standard imposes no
                      >>such restriction. Furthermore, there are systems for which it is not
                      >>true.
                      >>
                      >Could you explain how malloc works for such systems please.
                      >
                      Because void * is guaranteed to be able to store values of all object
                      pointer types.
                      >>
                      >But we are talking about values of "special memory blocks" where the
                      >pointers are different.
                      >
                      No, we're not. We weren't the last time you spouted this nonsensical
                      misinterpretati on of C's pointer system, and we still aren't. Nor will
                      we next time you will stubbornly refuse to learn.
                      >
                      Richard
                      "there are systems for which it is not true"

                      I ask again : how does malloc know what kind of "special" memory to
                      allocate? Special memory might have these special pointers.

                      And kindly don't accuse me of refusing to learn. If you want to bully
                      someone try someone else.

                      Comment

                      • Richard Bos

                        #26
                        Re: sizeof(ptr) = ?

                        Richard <rgrdev@gmail.c omwrote:
                        rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                        >
                        Richard <rgrdev@gmail.c omwrote:
                        santosh <santosh.k83@gm ail.comwrites:
                        >
                        Richard wrote:

                        Barry Schwarz <schwarz45@yaho o.comwrites:
                        >
                        >On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                        >>I test it with char, short, int, long, float, double and struct
                        >pointers, all return 4 bytes. Does the standard say anything about
                        >sizeof a pointer? (I didn't find it in the standard...)
                        >>>
                        >>Pointers of any type will have the same size because they hold the
                        >>address value of memory that they point to, thus they always hold
                        >>the same type of data. On standard PC's this is a 32bit address (4
                        >>bytes), but it depends on the system what the size is.
                        >>
                        >While this may be true in your experience, the C standard imposes no
                        >such restriction. Furthermore, there are systems for which it is not
                        >true.
                        >
                        Could you explain how malloc works for such systems please.

                        Because void * is guaranteed to be able to store values of all object
                        pointer types.
                        >
                        But we are talking about values of "special memory blocks" where the
                        pointers are different.
                        No, we're not. We weren't the last time you spouted this nonsensical
                        misinterpretati on of C's pointer system, and we still aren't. Nor will
                        we next time you will stubbornly refuse to learn.
                        >
                        "there are systems for which it is not true"
                        >
                        I ask again : how does malloc know what kind of "special" memory to
                        allocate? Special memory might have these special pointers.
                        ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
                        That assumption was not true last time, either...
                        And kindly don't accuse me of refusing to learn.
                        ....and this was explained to you last time, as well.

                        Richard

                        Comment

                        • santosh

                          #27
                          Re: sizeof(ptr) = ?

                          Richard wrote:
                          rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                          >
                          >Richard <rgrdev@gmail.c omwrote:
                          >>
                          >>santosh <santosh.k83@gm ail.comwrites:
                          >>>
                          >Richard wrote:
                          >>
                          >>Barry Schwarz <schwarz45@yaho o.comwrites:
                          >>>
                          >>>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                          >>>>I test it with char, short, int, long, float, double and
                          >>>>struct
                          >>>pointers, all return 4 bytes. Does the standard say anything
                          >>>about sizeof a pointer? (I didn't find it in the standard...)
                          >>>>>
                          >>>>Pointers of any type will have the same size because they hold
                          >>>>the address value of memory that they point to, thus they
                          >>>>always hold the same type of data. On standard PC's this is a
                          >>>>32bit address (4 bytes), but it depends on the system what the
                          >>>>size is.
                          >>>>
                          >>>While this may be true in your experience, the C standard
                          >>>imposes no
                          >>>such restriction. Furthermore, there are systems for which it
                          >>>is not true.
                          >>>
                          >>Could you explain how malloc works for such systems please.
                          >>
                          >Because void * is guaranteed to be able to store values of all
                          >object pointer types.
                          >>>
                          >>But we are talking about values of "special memory blocks" where the
                          >>pointers are different.
                          >>
                          >No, we're not. We weren't the last time you spouted this nonsensical
                          >misinterpretat ion of C's pointer system, and we still aren't. Nor
                          >will we next time you will stubbornly refuse to learn.
                          >>
                          >Richard
                          >
                          "there are systems for which it is not true"
                          >
                          I ask again : how does malloc know what kind of "special" memory to
                          allocate? Special memory might have these special pointers.
                          The point was that different pointer types might differ in size. That
                          doesn't mean that the memory pool from which malloc gets space has to
                          be different for each differing pointer types.

                          It may be that the implementation, for example, decides to append extra
                          information with pointers to floating types, but not to other types. In
                          this case such pointers might conceivably be larger than others.

                          Also an implementation might decide to allocate objects of a certain
                          type in lower address spaces than others. In such a case, pointers to
                          that type may be of smaller width, though the memory for the entire
                          gamut is the same.

                          I confess all the above to be hypothetical speculations on my part.
                          Maybe others more experienced can actually point to implementations
                          with heterogeneous pointer types.

                          Comment

                          • J. J. Farrell

                            #28
                            Re: sizeof(ptr) = ?

                            On Oct 22, 3:31 pm, Richard <rgr...@gmail.c omwrote:
                            santosh <santosh....@gm ail.comwrites:
                            Richard wrote:
                            >
                            santosh <santosh....@gm ail.comwrites:
                            >
                            >Richard wrote:
                            >
                            >>Barry Schwarz <schwar...@yaho o.comwrites:
                            >
                            >>>On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                            >>>>I test it with char, short, int, long, float, double and struct
                            >>>pointers, all return 4 bytes. Does the standard say anything
                            >>>about sizeof a pointer? (I didn't find it in the standard...)
                            >
                            >>>--
                            >>>William
                            >
                            >>>>Pointers of any type will have the same size because they hold the
                            >>>>address value of memory that they point to, thus they always hold
                            >>>>the same type of data. On standard PC's this is a 32bit address (4
                            >>>>bytes), but it depends on the system what the size is.
                            >
                            >>>While this may be true in your experience, the C standard imposes
                            >>>no
                            >>>such restriction. Furthermore, there are systems for which it is
                            >>>not true.
                            >
                            >>Could you explain how malloc works for such systems please.
                            >
                            >Because void * is guaranteed to be able to store values of all object
                            >pointer types.
                            >
                            But we are talking about values of "special memory blocks" where the
                            pointers are different. How does malloc know which "memory type" to
                            allocate?
                            >
                            It doesn't. It allocates memory suitable for all types. After the
                            conversion to an appropriate pointer type, the memory block is accessed
                            as an array of that type.
                            >
                            How can you allocate memory for "all types"? Where is this "special
                            block of memory with extra bits for padding" case?
                            Where does "padding" come into this?

                            Malloc's interface, and hence its job, is well-defined. It's the
                            implementation' s job to make sure malloc implements its defined
                            interface, by whatever means are required. On an architecture with a
                            complicated memory addressing architecture, malloc's job is likely to
                            be more difficult than on one with a simple architecture. The details
                            of what's required depend on each individual architecture.

                            Comment

                            • J. J. Farrell

                              #29
                              Re: sizeof(ptr) = ?

                              On Oct 22, 4:31 pm, Richard <rgr...@gmail.c omwrote:
                              r...@hoekstra-uitgeverij.nl (Richard Bos) writes:
                              Richard <rgr...@gmail.c omwrote:
                              >
                              santosh <santosh....@gm ail.comwrites:
                              >
                              Richard wrote:
                              >
                              Barry Schwarz <schwar...@yaho o.comwrites:
                              >
                              >On Oct 22, 2:01 am, "MisterE" <Mist...@nimga. comwrote:
                              >>I test it with char, short, int, long, float, double and struct
                              >pointers, all return 4 bytes. Does the standard say anything about
                              >sizeof a pointer? (I didn't find it in the standard...)
                              >
                              >>Pointers of any type will have the same size because they hold the
                              >>address value of memory that they point to, thus they always hold
                              >>the same type of data. On standard PC's this is a 32bit address (4
                              >>bytes), but it depends on the system what the size is.
                              >
                              >While this may be true in your experience, the C standard imposes no
                              >such restriction. Furthermore, there are systems for which it is not
                              >true.
                              >
                              Could you explain how malloc works for such systems please.
                              >
                              Because void * is guaranteed to be able to store values of all object
                              pointer types.
                              >
                              But we are talking about values of "special memory blocks" where the
                              pointers are different.
                              >
                              No, we're not. We weren't the last time you spouted this nonsensical
                              misinterpretati on of C's pointer system, and we still aren't. Nor will
                              we next time you will stubbornly refuse to learn.
                              >
                              Richard
                              >
                              "there are systems for which it is not true"
                              >
                              I ask again : how does malloc know what kind of "special" memory to
                              allocate? Special memory might have these special pointers.
                              >
                              And kindly don't accuse me of refusing to learn. If you want to bully
                              someone try someone else.
                              We'd be better able to answer your questions if you were to ask them
                              in less obscure ways. You have introduced the concepts of "padding"
                              and "special memory blocks" into this discussion; what exactly are you
                              talking about? What do you mean by these concepts, and how are they
                              relevant to this discussion? What exactly are you trying to ask?


                              Comment

                              • Mark Bluemel

                                #30
                                Re: sizeof(ptr) = ?

                                santosh wrote:
                                Richard wrote:
                                >
                                >>
                                >I ask again : how does malloc know what kind of "special" memory to
                                >allocate? Special memory might have these special pointers.
                                >
                                The point was that different pointer types might differ in size. That
                                doesn't mean that the memory pool from which malloc gets space has to
                                be different for each differing pointer types.
                                >
                                It may be that the implementation, for example, decides to append extra
                                information with pointers to floating types, but not to other types. In
                                this case such pointers might conceivably be larger than others.
                                I think I've explained before about Pr1me 50-Series systems...

                                Their "natural" (for some meaning of "natural") addressable unit was the
                                16-bit "half-word", so the standard 32-bit (2 half-words) pointers
                                addressed at this level.

                                On the occasions that (8-bit) characters needed to be addressed, an
                                alternative "long" pointer format was used. This was 48-bits long (3
                                half-words) - adding 16-bits to distinguish between the lefthand and the
                                righthand byte within a half-word.

                                The (conforming) C implementation therefore had 48-bit pointers for
                                "char *" and "void *", AFAIR, and 32-bit pointers for other data types.
                                (Naturally casting an appropriately aligned long pointer to a short
                                pointer was trivial).

                                It's perhaps worth mentioning that the 32-bit pointers were not simple
                                integers giving an offset into a flat memory space - oh no! They
                                contained 3 distinct pieces of data: a fault bit (used mainly for
                                function pointers to support a rather nifty dynamic lookup technique -
                                or at least it seemed nifty at the time), a "ring number" (relating to
                                access privileges), a segment number and an offset within the segment.

                                Comment

                                Working...