Advanced pointer?

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

    Advanced pointer?

    I have a question about advanced poiters. I know the basics of memory
    managment and pointers. That a pointer points to a memory location. I
    also have read that dynamic memory new is used for creating an array
    that you don't know the size of the array at compile time. Still I see
    pointers used in other ways. I can create object o; or object * o =
    new object; What is the different from creating an object one way or
    the other. I have used pointers:

    obj{
    member * pmemberr;

    };

    obj::obj(){
    pmember = new member;
    }

    I have some books that deal with pointers but they give me are
    simplistic and don't cover examples I see in other programs. If a good
    explanition is dificult to give where can I look this up to get a
    better idea how to use pointers.

  • JoeC

    #2
    Re: Advanced pointer?


    JoeC wrote:
    I have a question about advanced poiters. I know the basics of memory
    managment and pointers. That a pointer points to a memory location. I
    also have read that dynamic memory new is used for creating an array
    that you don't know the size of the array at compile time. Still I see
    pointers used in other ways. I can create object o; or object * o =
    new object; What is the different from creating an object one way or
    the other. I have used pointers:
    >
    obj{
    member * pmemberr;
    >
    };
    >
    obj::obj(){
    pmember = new member;
    }
    >
    I have some books that deal with pointers but they give me are
    simplistic and don't cover examples I see in other programs. If a good
    explanition is dificult to give where can I look this up to get a
    better idea how to use pointers.
    BTW I also know that pointers are needed for polymorphism.

    Comment

    • Steve Pope

      #3
      Re: Advanced pointer?

      JoeC <enki034@yahoo. comwrote:
      >BTW I also know that pointers are needed for polymorphism.
      An interesting claim. I'm guessing it's false, and that
      a language could (as a language feature) provide many types of
      polymorphism without the language providing pointers (as
      a language feature).

      Probably some people here could cite examples of such languages.

      Steve

      Comment

      • Gavin Deane

        #4
        Re: Advanced pointer?


        JoeC wrote:
        I have a question about advanced poiters. I know the basics of memory
        managment and pointers. That a pointer points to a memory location. I
        also have read that dynamic memory new is used for creating an array
        that you don't know the size of the array at compile time.
        std::vector is a better way of creating an array if you don't know the
        size until run time.
        Still I see
        pointers used in other ways. I can create object o; or object * o =
        new object; What is the different from creating an object one way or
        the other.
        object o;
        o has either static storage duration (the object lasts for the entire
        life of the program) if this statement appears at file scope, outside
        any function, or o has automatic storage duration (the object lasts
        until the end of the block of code it is declared in) if this statement
        appears inside a function or inside a block of code within a function.
        In either case, the compiler is responsible for managing the lifetime
        of the object.

        object* o = new object;
        The object pointed to by o has dynamic storage duration. It exists
        until you, the programmer, gets rid of it with delete o; which you must
        do to avoid a memeory leak. You, and you alone, are responsible for
        managing the lifetime of the object and avoiding a memory. The compiler
        will not help you.

        Do not give yourself this extra responsibility unless you absolutely
        need to. And then, do not use new on its own. Always, always, always
        use smart objects to encapsulate your memory management. So for dynamic
        arrays use std::vector and for dynamic single objects use a smart
        pointer like std::auto_ptr or std::tr1::share d_ptr (or whatever the
        boost::shared_p tr is called now it or something like it has been
        incorporated into C++).

        Gavin Deane

        Comment

        • Jim Langston

          #5
          Re: Advanced pointer?

          "JoeC" <enki034@yahoo. comwrote in message
          news:1159228893 .775582.246140@ d34g2000cwd.goo glegroups.com.. .
          >I have a question about advanced poiters. I know the basics of memory
          managment and pointers. That a pointer points to a memory location. I
          also have read that dynamic memory new is used for creating an array
          that you don't know the size of the array at compile time. Still I see
          pointers used in other ways. I can create object o; or object * o =
          new object; What is the different from creating an object one way or
          the other. I have used pointers:
          >
          obj{
          member * pmemberr;
          >
          };
          >
          obj::obj(){
          pmember = new member;
          }
          >
          I have some books that deal with pointers but they give me are
          simplistic and don't cover examples I see in other programs. If a good
          explanition is dificult to give where can I look this up to get a
          better idea how to use pointers.
          Your question seems to be what's the difference between using new to
          allocate one instance of an object and allocating an array of objects.

          If I have a pointer such as:
          MyClass* MyPointer;
          All I know is that it can be used to point to at least one instance of
          MyClass. So I can say:

          MyPointer = new MyClass;
          or
          MyPointer = new MyClass[10];

          The first example allocates memory and calls constructor for one MyClass and
          has MyPointer point to it. The second allocates memory for 10 MyClasses and
          calls constructor and has MyPointer point to the first one.

          There is little (but some) difference between declaring MyPointer as:
          MyClass* MyPointer;
          MyClass MyPointer[];

          It is up to me to determine how many instances of MyClass MyPointer points
          to.

          Also, if only one is instatized with new, then you use delete. If more than
          one you use delete[]. If you use delete and more than one is declared, this
          can cause undefined behavior. The most likely thing that will happen is the
          destructors will not be called but on one instance.


          Comment

          • JoeC

            #6
            Re: Advanced pointer?


            Steve Pope wrote:
            JoeC <enki034@yahoo. comwrote:
            >
            BTW I also know that pointers are needed for polymorphism.
            >
            An interesting claim. I'm guessing it's false, and that
            a language could (as a language feature) provide many types of
            polymorphism without the language providing pointers (as
            a language feature).
            >
            Probably some people here could cite examples of such languages.
            >
            Steve
            shape * sh1 = new triangle();
            shape * sh2 = new square();

            or from a program that I wrote:

            hunit::hunit(ch ar n){

            switch(n){
            case 'u':
            p = new unit;
            break;
            case 'a':
            p = new air;
            break;
            case 's':
            p = new sea;
            break;
            }

            cnt = new int(1);

            }

            Comment

            • JoeC

              #7
              Re: Advanced pointer?


              Gavin Deane wrote:
              JoeC wrote:
              I have a question about advanced poiters. I know the basics of memory
              managment and pointers. That a pointer points to a memory location. I
              also have read that dynamic memory new is used for creating an array
              that you don't know the size of the array at compile time.
              >
              std::vector is a better way of creating an array if you don't know the
              size until run time.
              >
              Still I see
              pointers used in other ways. I can create object o; or object * o =
              new object; What is the different from creating an object one way or
              the other.
              >
              object o;
              o has either static storage duration (the object lasts for the entire
              life of the program) if this statement appears at file scope, outside
              any function, or o has automatic storage duration (the object lasts
              until the end of the block of code it is declared in) if this statement
              appears inside a function or inside a block of code within a function.
              In either case, the compiler is responsible for managing the lifetime
              of the object.
              >
              object* o = new object;
              The object pointed to by o has dynamic storage duration. It exists
              until you, the programmer, gets rid of it with delete o; which you must
              do to avoid a memeory leak. You, and you alone, are responsible for
              managing the lifetime of the object and avoiding a memory. The compiler
              will not help you.
              >
              Do not give yourself this extra responsibility unless you absolutely
              need to. And then, do not use new on its own. Always, always, always
              use smart objects to encapsulate your memory management. So for dynamic
              arrays use std::vector and for dynamic single objects use a smart
              pointer like std::auto_ptr or std::tr1::share d_ptr (or whatever the
              boost::shared_p tr is called now it or something like it has been
              incorporated into C++).
              >
              Gavin Deane
              I like your advice and that is usually the way I design my programs.
              Recently, I have been writing some simple games but complex programs
              and have been advised to use pointer for some aspects of the game.
              There are some times, I realy had to use pointers but other times I
              have gotten by with out them. So pointers are good for global
              variables or larger objects that may have to get passed to different
              functions. Here is an example of my hearder for my map game:

              #include<window s.h>
              #include<ctime>
              #include<vector >

              #include "unit.h"
              #include "libs.h"
              #include "board.h"
              #include "terrain.h"
              #include "graphic.h"
              #include "tbox.h"
              #include "cfight.h"
              #include "mapmgt.h"

              #ifndef GAME_H
              #define GAME_H

              LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); //winproc
              function
              void kill(std::vecto r<unit>&); //unit killer

              #endif


              static const int TeamSize = 5; //size of each team

              static board * b; //the board

              static terrain trn[5]; //terrain array
              static char * tn[5]; //terrain name array

              static mapmgt m(trn, b->GetSizeX(), b->GetSizeY()); //map manager

              static vector<unitrtea m(TeamSize); //read side
              static vector<unitytea m(TeamSize); //purple side originally was going
              to
              //yellow but didn't look good on
              the screen

              static bool red; //red's turn
              static bool ppl; //purple's turn

              static int t1m; //red's current mover
              static int t2m; //purple's current mover

              Comment

              • Frederick Gotham

                #8
                Re: Advanced pointer?

                JoeC posted:
                I have a question about advanced poiters.

                What are "advanced pointers"? Are you talking about plain old run-of-the-
                mill pointers.
                I know the basics of memory managment and pointers. That a pointer
                points to a memory location. I also have read that dynamic memory new
                is used for creating an array that you don't know the size of the array
                at compile time. Still I see pointers used in other ways. I can create
                object o; or object * o = new object; What is the different from
                creating an object one way or the other. I have used pointers:

                The former creates an "automatic object" which comes into being at its
                definition, and is destroyed when it goes out of scope.

                The latter creates a "dynamicall ya allocated object" which come into being
                at its creation via new, and is destroyed when it is "delete"'d.

                obj{
                member * pmemberr;
                >
                };

                Perhaps you meant "struct obj" rather than simply "obj"?

                If a good explanition is dificult to give where can I look this up to
                get a better idea how to use pointers.

                Here's a recent post of mine which trys to teach about them:



                --

                Frederick Gotham

                Comment

                • Frederick Gotham

                  #9
                  Re: Advanced pointer?

                  JoeC posted:
                  BTW I also know that pointers are needed for polymorphism.

                  It can be achieved with a reference:

                  class Base : { virtual int Func()...

                  class Derived1 : public Base { virtual int Func()...

                  void MyFunc(Base &obj)
                  {
                  obj.Func();
                  }

                  --

                  Frederick Gotham

                  Comment

                  • JoeC

                    #10
                    Re: Advanced pointer?


                    Frederick Gotham wrote:
                    JoeC posted:
                    >
                    BTW I also know that pointers are needed for polymorphism.
                    >
                    >
                    It can be achieved with a reference:
                    >
                    class Base : { virtual int Func()...
                    >
                    class Derived1 : public Base { virtual int Func()...
                    >
                    void MyFunc(Base &obj)
                    {
                    obj.Func();
                    }
                    >
                    --
                    >
                    Frederick Gotham
                    didn't know that all my books say that you have to obj * prt = new
                    thing;

                    Comment

                    Working...