to improve programming in pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sivashankar21@gmail.com

    #1

    to improve programming in pointers

    how i can improve my programming skill in pointer using c++

  • benben

    #2
    Re: to improve programming in pointers

    by more reading and practices.

    Ben

    Comment

    • Gianni Mariani

      #3
      Re: to improve programming in pointers

      sivashankar21@g mail.com wrote:[color=blue]
      > how i can improve my programming skill in pointer using c++
      >[/color]

      Go find the answers to these questions.


      struct A { int a; };

      struct B : A { int b; };

      A xa[ 10 ];
      B xb[ 10 ];

      A * pa = xb;
      // Q1 - what is the value of pa pointing to ?

      A * pBAD = pa + 1;
      // Q2 - why is pBAD, bad ?

      B * pb = xb;
      // - same as Q1

      A * paOK = pb + 1;
      // Q3 - what does paOK point to ?


      A (&( * pf )( int * ))[ 10 ];
      // Q4 - what is pf ?

      A * paX = & pb[1];

      bool b( paX == paOK );
      // Q5 - what is the value of b

      A * paY = & pb[3];

      int iNOTGOOD( paY - paX );
      // Q6 - Why is iNOTGOOD not good ?

      A * paM = new B;

      int main()
      {

      delete paM; // line Z - NOT GOOD
      // Q7 - why is line Z a bad thing ?

      paM = 0;
      // Q8 - what is paM now ?

      delete paM;
      // Q9 - what does delete paM do ?

      bool x( & (* xb).b == & xb->b );
      // Q10 - What is the value of x ?
      }

      B * F()
      {
      B x[2];

      return x; // doh !
      // Q11 - Why is the line marked "doh" broken ?
      }

      Comment

      • loufoque

        #4
        Re: to improve programming in pointers

        sivashankar21@g mail.com a écrit :[color=blue]
        > how i can improve my programming skill in pointer using c++
        >[/color]

        You shouldn't even use pointers.
        Avoid them as much as possible and only use them when it's needed.

        Comment

        • Default User

          #5
          Re: to improve programming in pointers

          loufoque wrote:
          [color=blue]
          > sivashankar21@g mail.com a écrit :[color=green]
          > > how i can improve my programming skill in pointer using c++
          > >[/color]
          >
          > You shouldn't even use pointers.
          > Avoid them as much as possible and only use them when it's needed.[/color]

          At which point he won't know how to use them.



          Brian

          --
          If televison's a babysitter, the Internet is a drunk librarian who
          won't shut up.
          -- Dorothy Gambrell (http://catandgirl.com)

          Comment

          • Ian Collins

            #6
            Re: to improve programming in pointers

            Default User wrote:[color=blue]
            > loufoque wrote:
            >
            >[color=green]
            >>sivashankar21 @gmail.com a écrit :
            >>[color=darkred]
            >>>how i can improve my programming skill in pointer using c++
            >>>[/color]
            >>
            >>You shouldn't even use pointers.
            >>Avoid them as much as possible and only use them when it's needed.[/color]
            >
            >
            > At which point he won't know how to use them.
            >[/color]
            But he may have developed sufficient skill in in avoiding them so he
            doesn't have to!

            Are there any (real world) instances where pointers can't be avoided?

            --
            Ian Collins.

            Comment

            • Gianni Mariani

              #7
              Re: to improve programming in pointers

              Ian Collins wrote:
              ....[color=blue]
              >
              > Are there any (real world) instances where pointers can't be avoided?[/color]

              Let's see:

              a) the "this" keyword.

              b) When you may or may not want to pass an interface ... e.g.

              void DoThing( const A & a, ErrorReporter * e = 0 );

              c) Intrusive reference counted objects.

              d) When objects point to one another i.e. a<->b

              + many more

              Comment

              • Ian Collins

                #8
                Re: to improve programming in pointers

                Gianni Mariani wrote:[color=blue]
                > Ian Collins wrote:
                > ....
                >[color=green]
                >>
                >> Are there any (real world) instances where pointers can't be avoided?[/color]
                >
                >
                > Let's see:
                >
                > a) the "this" keyword.
                >
                > b) When you may or may not want to pass an interface ... e.g.
                >
                > void DoThing( const A & a, ErrorReporter * e = 0 );
                >
                > c) Intrusive reference counted objects.
                >
                > d) When objects point to one another i.e. a<->b
                >
                > + many more[/color]

                With the exception of a, all these can be replace by some form of smart
                pointer object.

                --
                Ian Collins.

                Comment

                • Gianni Mariani

                  #9
                  Re: to improve programming in pointers

                  Ian Collins wrote:
                  ....[color=blue]
                  >
                  > With the exception of a, all these can be replace by some form of smart
                  > pointer object.[/color]

                  You still need to understand pointers even if you're managing them using
                  smart pointers.




                  Comment

                  • Ian Collins

                    #10
                    Re: to improve programming in pointers

                    Gianni Mariani wrote:[color=blue]
                    > Ian Collins wrote:
                    > ....
                    >[color=green]
                    >>
                    >> With the exception of a, all these can be replace by some form of
                    >> smart pointer object.[/color]
                    >
                    >
                    > You still need to understand pointers even if you're managing them using
                    > smart pointers.
                    >[/color]
                    True enough, but I was trying to think of an example where raw pointers
                    can't be replaced with smart pointers.

                    --
                    Ian Collins.

                    Comment

                    • benben

                      #11
                      Re: to improve programming in pointers

                      > True enough, but I was trying to think of an example where raw pointers[color=blue]
                      > can't be replaced with smart pointers.
                      >[/color]

                      When you are writing your own smart pointers? Perhaps?

                      Ben

                      Comment

                      Working...