int* ptr=10;.......issue.

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

    #1

    int* ptr=10;.......issue.

    hi ,

    whats the issue with the following declaration, is it correct and what
    exactly is happening here.

    int main()
    {
    //.....
    int* ptr=10; // i suppose ptr is pointing to the memory location which
    stores the value 10.
    //
    .......
    }


    on gcc it gives no error...but when i compile it using g++ it gives
    "invalid conversion of int * to int"

    please advise on this.
    regards.

  • Lars Uffmann

    #2
    Re: int* ptr=10;.......i ssue.

    "al pacino" <siddharthdave8 4@gmail.comwrot e:
    whats the issue with the following declaration, is it correct and what
    exactly is happening here.
    [...]
    int* ptr=10; // i suppose ptr is pointing to the memory location which
    stores the value 10.
    Nope, this would make the pointer point to the memory ADDRESS 10
    (0xa).
    on gcc it gives no error...but when i compile it using g++ it gives
    "invalid conversion of int * to int"
    You need to add a typecast:
    int* ptr = (int *) 10;
    or
    int *ptr = new int[1];
    *ptr = 10;
    delete [] ptr;

    Best Regards,

    Lars


    Comment

    • al pacino

      #3
      Re: int* ptr=10;.......i ssue.


      Lars Uffmann wrote:
      "al pacino" <siddharthdave8 4@gmail.comwrot e:
      whats the issue with the following declaration, is it correct and what
      exactly is happening here.
      [...]
      int* ptr=10; // i suppose ptr is pointing to the memory location which
      stores the value 10.
      >
      Nope, this would make the pointer point to the memory ADDRESS 10
      (0xa).
      >
      maybe i was confused as,
      char *s="hello";
      implies 's' pointing to the string HELLO.

      but in the original case..what was happning was
      int *ptr; and
      ptr=10; //ptr now points to address 10;

      why is it so in the case of int as in char.

      thanks and regards.



      on gcc it gives no error...but when i compile it using g++ it gives
      "invalid conversion of int * to int"
      >
      You need to add a typecast:
      int* ptr = (int *) 10;
      or
      int *ptr = new int[1];
      *ptr = 10;
      delete [] ptr;
      >
      Best Regards,
      >
      Lars
      or

      Comment

      • Salt_Peter

        #4
        Re: int* ptr=10;.......i ssue.


        al pacino wrote:
        hi ,
        >
        whats the issue with the following declaration, is it correct and what
        exactly is happening here.
        >
        int main()
        {
        //.....
        int* ptr=10; // i suppose ptr is pointing to the memory location which
        stores the value 10.
        no, thats wrong and a big no-no.
        You don't understand pointers.
        //
        ......
        }
        >
        >
        on gcc it gives no error...but when i compile it using g++ it gives
        "invalid conversion of int * to int"
        >
        please advise on this.
        regards.
        That is expected. You are inducing undefined behaviour.
        You are not trying to *set* what is *at* the pointer to the value of
        10.
        You are trying to set the memory address *in* ptr to 10.
        A very big no-no (thankfully caught by most C++ compilers).

        This too is illegal:

        int *ptr; // ptr holds some residual value, its unitialized (bad,
        no-no, danger)
        *ptr = 10;

        How do you know what is at that residual location?
        Imagine what happens when the compiler lets you overwrite any memory
        location.
        Answer: UB (format the hard drive, crash the computer, etc)
        You think thats a common joke - but its not a joke

        Pointers are always set using the address_of (&) operator or new.

        int n; // reserve integer n on the stack, although n is uninitialized
        int *ptr = &n; // reserve pointer ptr on stack & its now valid: it
        holds n's address
        *ptr = 10; // same as n = 10

        Again, ptr holds an address, *ptr is the variable at that address.
        And the term in question is "dereferenc ing the pointer".
        One way or another, its the program that sets the pointer.
        Pointers are not a toy, when you see a pointer, except for
        polymorphism, a buzzer should go off in your head. Pointers are nearly
        always bad news.

        pointers are hell
        pointers create bugs
        pointers generate headaches
        a pointer is a disease
        etc

        Stay away from pointers, use a reference instead.
        hmm, how the hell are we going to explain references to you?

        Comment

        • Gernot Frisch

          #5
          Re: int* ptr=10;.......i ssue.

          pointers are hell
          pointers create bugs
          pointers generate headaches
          a pointer is a disease
          etc
          >
          Stay away from pointers, use a reference instead.
          Do you come from Java? Don't make pointers that bad, because they do
          have their right of existance.


          hmm, how the hell are we going to explain references to you?
          It's apointer - but not really a pointer.


          Comment

          • Robert J. Hansen

            #6
            Re: int* ptr=10;.......i ssue.

            Do you come from Java? Don't make pointers that bad, because they do
            have their right of existance.
            Yes, when wrapped in a tr1::shared_ptr <>, or some other suitable
            exceptionsafe, leak-free method of handling them.

            Comment

            • Rolf Magnus

              #7
              Re: int* ptr=10;.......i ssue.

              al pacino wrote:
              >
              Lars Uffmann wrote:
              >"al pacino" <siddharthdave8 4@gmail.comwrot e:
              whats the issue with the following declaration, is it correct and what
              exactly is happening here.
              [...]
              int* ptr=10; // i suppose ptr is pointing to the memory location which
              stores the value 10.
              >>
              >Nope, this would make the pointer point to the memory ADDRESS 10
              >(0xa).
              >>
              maybe i was confused as,
              char *s="hello";
              implies 's' pointing to the string HELLO.
              Well, there is a difference here. That string literal is of array type. An
              array gets in most cases converted to a pointer to its first element.
              but in the original case..what was happning was
              int *ptr; and
              ptr=10; //ptr now points to address 10;
              >
              why is it so in the case of int as in char.
              It's not char, it's char[6].

              Comment

              • Rolf Magnus

                #8
                Re: int* ptr=10;.......i ssue.

                Gernot Frisch wrote:
                >
                >pointers are hell
                >pointers create bugs
                >pointers generate headaches
                >a pointer is a disease
                >etc
                >>
                >Stay away from pointers, use a reference instead.
                >
                Do you come from Java? Don't make pointers that bad, because they do
                have their right of existance.
                I'd say that Java references are closer to C++ pointers than to C++
                references.

                Comment

                • peter koch

                  #9
                  Re: int* ptr=10;.......i ssue.


                  Gernot Frisch wrote:
                  Saltiers(?) wrote:
                  pointers are hell
                  pointers create bugs
                  pointers generate headaches
                  a pointer is a disease
                  etc

                  Stay away from pointers, use a reference instead.
                  >
                  Do you come from Java? Don't make pointers that bad, because they do
                  have their right of existance.
                  >
                  Is it not rather the other way around - that Java people abuse pointers
                  because that is the only thing they have? We "real" C++ people normally
                  do know that Saltiers advice (avoid pointers whenever possible) is
                  sound and correct.
                  >
                  >
                  hmm, how the hell are we going to explain references to you?
                  >
                  It's apointer - but not really a pointer.
                  2% truth 98% false!

                  /Peter

                  Comment

                  • Marcus Kwok

                    #10
                    Re: int* ptr=10;.......i ssue.

                    Salt_Peter <pj_hern@yahoo. comwrote:
                    Pointers are always set using the address_of (&) operator or new.
                    Or the null pointer value (integral constant 0).

                    --
                    Marcus Kwok
                    Replace 'invalid' with 'net' to reply

                    Comment

                    • Salt_Peter

                      #11
                      Re: int* ptr=10;.......i ssue.


                      Gernot Frisch wrote:
                      pointers are hell
                      pointers create bugs
                      pointers generate headaches
                      a pointer is a disease
                      etc

                      Stay away from pointers, use a reference instead.
                      >
                      Do you come from Java? Don't make pointers that bad, because they do
                      have their right of existance.
                      >
                      I do enjoy Java. Did you know that a Java reference is the equivalent
                      of a C++ pointer? Java would love to have the equivalent of a C++
                      reference.
                      >
                      hmm, how the hell are we going to explain references to you?
                      >
                      It's apointer - but not really a pointer.
                      No, a reference is an object (even when it is implemented as a pointer
                      down under -which is often not the case). A plain pointer is nothing
                      more than an adrress to something or nothing that may or may not be
                      valid.

                      Comment

                      • Salt_Peter

                        #12
                        Re: int* ptr=10;.......i ssue.


                        Marcus Kwok wrote:
                        Salt_Peter <pj_hern@yahoo. comwrote:
                        Pointers are always set using the address_of (&) operator or new.
                        >
                        Or the null pointer value (integral constant 0).
                        Indeed.
                        >
                        --
                        Marcus Kwok
                        Replace 'invalid' with 'net' to reply

                        Comment

                        • Lars Uffmann

                          #13
                          Re: int* ptr=10;.......i ssue.

                          "Salt_Peter " <pj_hern@yahoo. comwrote:
                          pointers are hell
                          pointers create bugs
                          pointers generate headaches
                          a pointer is a disease
                          etc
                          Stay away from pointers, use a reference instead.
                          Lol - now come on - pointers are pretty useful and impossible
                          to work without in many cases.

                          Best Regards,

                          Lars


                          Comment

                          • Rolf Magnus

                            #14
                            Re: int* ptr=10;.......i ssue.

                            Salt_Peter wrote:
                            >
                            Gernot Frisch wrote:
                            pointers are hell
                            pointers create bugs
                            pointers generate headaches
                            a pointer is a disease
                            etc
                            >
                            Stay away from pointers, use a reference instead.
                            >>
                            >Do you come from Java? Don't make pointers that bad, because they do
                            >have their right of existance.
                            >>
                            >
                            I do enjoy Java. Did you know that a Java reference is the equivalent
                            of a C++ pointer? Java would love to have the equivalent of a C++
                            reference.
                            >
                            >>
                            hmm, how the hell are we going to explain references to you?
                            >>
                            >It's apointer - but not really a pointer.
                            >
                            No, a reference is an object
                            No. That's exactly what a reference is not.
                            (even when it is implemented as a pointer down under -which is often not
                            the case).
                            Really? Could you elaborate? Which compilers implement them differently? I'm
                            seriously interested in that.

                            Comment

                            • Bo Persson

                              #15
                              Re: int* ptr=10;.......i ssue.


                              "al pacino" <siddharthdave8 4@gmail.comskre v i meddelandet
                              news:1156745966 .364732.210010@ i42g2000cwa.goo glegroups.com.. .
                              >
                              Lars Uffmann wrote:
                              >"al pacino" <siddharthdave8 4@gmail.comwrot e:
                              whats the issue with the following declaration, is it correct and
                              what
                              exactly is happening here.
                              [...]
                              int* ptr=10; // i suppose ptr is pointing to the memory location
                              which
                              stores the value 10.
                              >>
                              >Nope, this would make the pointer point to the memory ADDRESS 10
                              >(0xa).
                              >>
                              maybe i was confused as,
                              char *s="hello";
                              implies 's' pointing to the string HELLO.
                              >
                              but in the original case..what was happning was
                              int *ptr; and
                              ptr=10; //ptr now points to address 10;
                              >
                              why is it so in the case of int as in char.
                              >
                              thanks and regards.
                              Sorry, but you try to draw the conclusions the wrong way.

                              Here int follows the general rules, and char* is very special. It has
                              a whole set of extra rules, to be able to cope with the strange string
                              literal "hello" (which isn't a char!).


                              Bo Persson


                              Comment

                              Working...