Pointers In C++

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

    Pointers In C++

    A pointer is a variable which stores the address of another
    variable. There are two important operators when working with pointers
    in C++: the address of (&) operator and the value of (*) operator.
    They have been overloaded in C++ so they may have different uses in
    different contexts.

    How much storage space does a pointer consume?
    Use sizeof(ptr) without the '*' operator to determine the memory
    utilised on your system. Irrespective of datatype or whether the
    pointer points to a single variable or array, as a general rule,
    pointers must use the same amount of memory space.

    The & operator gives us the address of a variable and * gives us the
    value of a variable at a specified address.

    Please tell me something more about Pointer in C++.

  • James Kanze

    #2
    Re: Pointers In C++

    On Jun 26, 9:23 am, "Alf P. Steinbach" <al...@start.no wrote:
    * Stefan Ram:
    Alisha <alisha0...@gma il.comwrites:
    A pointer is a variable which stores the address of another
    variable.
    IIRC, both »pointer« and »variable« are not defined explicitly
    by ISO/IEC 14882:2003(E).
    [...]
    The definition of "variable" in §3/4 is succinct and complete,
    and I don't understand how you can refer to it above, "3p4",
    and still maintain that there's no definition of the term.
    Ditto, the term "pointer" is defined by §3.9.2/1 with
    reference §8.3.1.
    Independantly of the clarity (I have no problem with it, but
    clarity is subjective), the terms are in italics in the cited
    text. And (§1.3) "Terms that are used only in a small portion
    of this International Standard are defined where they are used
    and italicized where they are defined." Of course, I'm not
    sure that either variable or pointer would qualify as "used only
    in a small portion" of the standard. But the intent is clear:
    the cited text is a definition, in the sense of the standard.

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    • Kai-Uwe Bux

      #3
      Re: Pointers In C++

      Alf P. Steinbach wrote:
      * Stefan Ram:
      >"Alf P. Steinbach" <alfps@start.no writes:
      >>>»An object is a region of storage that has a type.«
      >>Not in C++.
      >>
      > »An object is a region of storage. [...] An object has a type«
      >>
      > ISO/IEC 14882:2003(E), 1.8
      >
      Sorry, a region of storage does not have a type.
      The proposed definition of "object" does not entail the claim that any
      region of storage has a type. It says that an object is a region of storage
      that has a type. If a region of storage does not have a type, it would not
      qualify as an object according to the proposed definition.

      The standard, on the other hand, is very clear that some regions of storage
      have types (namely those regions of storage that happen to be objects).

      A region of storage may be part of many objects, with different types.
      Yes, and nothing in the proposed definition of "object" would contradict
      that.



      Best

      Kai-Uwe Bux

      Comment

      • Kai-Uwe Bux

        #4
        Re: Pointers In C++

        Alf P. Steinbach wrote:
        * Kai-Uwe Bux:
        >Alf P. Steinbach wrote:
        >>
        >>* Stefan Ram:
        >>>"Alf P. Steinbach" <alfps@start.no writes:
        >>>>>»An object is a region of storage that has a type.«
        >>>>Not in C++.
        >>> »An object is a region of storage. [...] An object has a type«
        >>>>
        >>> ISO/IEC 14882:2003(E), 1.8
        >>Sorry, a region of storage does not have a type.
        >>
        >The proposed definition of "object" does not entail the claim that any
        >region of storage has a type. It says that an object is a region of
        >storage that has a type.
        >
        You're contradicting yourself.
        >
        >
        >If a region of storage does not have a type, it would not
        >qualify as an object according to the proposed definition.
        >
        I think that's true.
        >
        So it's not compatible with C++.
        That sentence only talks about those regions of storage that are not object.
        But if x is an object and objects are regions of storage, then some region
        of storage _is_ the object x. In that case, that region of storage has a
        type since the object x has a type.

        >The standard, on the other hand, is very clear that some regions of
        >storage have types (namely those regions of storage that happen to be
        >objects).
        >
        Chapter & verse please.
        You have already been given the necessary quotes:

        [1.8/1]: An object is a region of storage. ["object" in italics]

        Note that this is a conceptual stipulation that identifies objects with
        regions of storage. Consequently, all attributes of an object become
        attributes of the region of storage that this object happens to be (there
        is no conceptual distinction between the object and the region of storage
        that it _is_).

        Therefore, when the standard goes [in the same clause] to say that objects
        have storage duration, lifetime, and type it _follows_ that certain regions
        of storage (namely those that happen to be objects) have storage duration,
        lifetime, and type.

        Should be corrected if anything so bad has managed to sneak into the
        standard! :-)
        Maybe. The standard could say "An object occupies a region of storage" or
        "An object has a region of storage". But the standard chose to use "is" and
        thereby chose to identify objects and their regions of storage on a
        conceptual level. Whether that decision is _wise_ is a different matter.

        BTW: This is very much like the differences of materialist and
        non-materialist accounts of what a human is. Materialist would say a human
        _is_ a body and non-materialists would say a human _has_ a body. The former
        would then go on to say that if humans had immortal souls, it would follow
        that certain bodies had immortal souls. (This is one of the reasons that
        materialists tend to be critical of religion and that religious
        philosophers tend to be critical of materialism. Both agree that the
        inference is valid:-)

        >>A region of storage may be part of many objects, with different types.
        >>
        >Yes, and nothing in the proposed definition of "object" would contradict
        >that.
        >
        It does.
        How so? Let's say you have an object x of type T which has a member x.a of
        type S. Also, suppose that x_ptr is a char* so that

        [x_ptr, x_ptr+sizeof(T) )

        _is_ the object X and a_ptr is a char* so that

        [a_ptr, a_ptr + sizeof(S) )

        _is_ the object x.a. Then the region [a_ptr, a_ptr+sizeof(S) ) has type S
        and the region [x_ptr, x_ptr + sizeof(T) ) has the type T. Of course, that
        even goes when the two regions of storage coincide. BTW: in that case, x
        and x.a _are_ the same(!) object according to the standard (which makes
        more sense that one might think at first: after all any change to x will
        affect a).
        Although there exists a silly convoluted interpretation (reading
        "has" as "is associated with" and "a" as "at least one") where it doesn't.
        There is nothing silly about it. It just follows straight from the
        identification made by the standard.
        However, it doesn't matter, for it goes only to the practical reason why
        regions of storage are not regarded as having types.
        >
        Understanding the difference between storage (untyped) and object (typed)
        is
        fundamental. For example, a void* doesn't point to storage of type void.
        But it's no big deal to take that untyped storage and make an object out
        of it via e.g. placement new.
        >
        I think it's wrong of you to try to sow confusion about this distinction.
        You may not like the conceptual identification of objects with regions of
        storage, but the italics in the standard [1.8/1] make it very clear that
        the statement "An object is a region of storage" is meant as a definition
        of the term object. Since it is supposed to be a definition, it makes an
        identification on the conceptual level (for better or worse).

        Maybe, what one would want is something like "an object is a typed region of
        storage". So, two objects would be the same if and only if they occupy the
        same region in memory _and_ have the same type. Creation of an object could
        then be regarded as the act of typing a certain region of memory.


        BTW: I don't think the identification is actually all that harmful. The
        standard is not a book to teach C++ but just a specification that should
        allow one to deduce the observable behavior of C++ programs. I do not (yet)
        see how that [1.8/1] or my interpretation of it negatively impacts on that.



        Best

        Kai-Uwe Bux

        Comment

        • Matthias Buelow

          #5
          Re: Pointers In C++

          Kai-Uwe Bux wrote:
          Maybe. The standard could say "An object occupies a region of storage" or
          "An object has a region of storage". But the standard chose to use "is" and
          thereby chose to identify objects and their regions of storage on a
          conceptual level. Whether that decision is _wise_ is a different matter.
          Well, the question would be: does the object still exist if it had been
          written to tape when the computer has been melted and turned into cola
          cans? The above wording avoids such (purely philosophical, of course)
          questions, because they're meaningless in the discussion of a running
          C++ program.

          Comment

          Working...