Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Base pointer Blues!!!

Base pointer Blues!!!

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestestingbeta-testinghelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    avenger_sb25
    wrote on last edited by
    #1

    Suppose i have a set up like this:

    class Base
    {
    public:
    int x;
    virtual void f()
    };
    class Derv:public Base
    {
    public:
    int y;
    void f();
    };

    void main()
    {
    Base *p;
    p=new Derv[4];

       //I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE
       for(int i=0;i<4;i++)
          (p+i)->y=10;  //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW
    
        delete\[\]p;
    

    }

    NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger


    Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

    U T 2 Replies Last reply
    0
    • A avenger_sb25

      Suppose i have a set up like this:

      class Base
      {
      public:
      int x;
      virtual void f()
      };
      class Derv:public Base
      {
      public:
      int y;
      void f();
      };

      void main()
      {
      Base *p;
      p=new Derv[4];

         //I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE
         for(int i=0;i<4;i++)
            (p+i)->y=10;  //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW
      
          delete\[\]p;
      

      }

      NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger


      Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

      U Offline
      U Offline
      User 580446
      wrote on last edited by
      #2

      Hey Avenger, Here's a funny workaround... DWORD dw = (DWORD) p; // move in the array using this Base* p2 = NULL; for(int i=0;i<4;i++) { p2 = (Base*) dw; p2->y=10; dw += sizeof(Derv); // move to the next elem } What do you think?? :-D Clinton

      A 1 Reply Last reply
      0
      • A avenger_sb25

        Suppose i have a set up like this:

        class Base
        {
        public:
        int x;
        virtual void f()
        };
        class Derv:public Base
        {
        public:
        int y;
        void f();
        };

        void main()
        {
        Base *p;
        p=new Derv[4];

           //I WANT TO ACCESS EACH ENTRY OF THE ALLOCATED ARRAY USING POINTER OF BASE
           for(int i=0;i<4;i++)
              (p+i)->y=10;  //THIS CRASHES WHEN i>0 AS I HAVE EXPLAINED BELOW
        
            delete\[\]p;
        

        }

        NOW HERE IS THE PROBLEM- I can access the first element using pointer 'p' BUT if i try to acces the second element of the array by doing p+1, MY PROGRAM CRASHES... This is the problem: Suppose the derived class array was allocated at 0x00000000, then p contains 0x11111111. I thought that p+1 will be 0x11111111+sizeof(Derv), BUT it is 0x11111111+sizeof(Base) SO WHAT I WANT TO KNOW FORM YOU FRIENDS IS - HOW TO ACCESS THE ELEMENTS OF A DERIVED CLASS ARRAY USING BASE CLASS POINTER ??? thanx for ur time ...Avenger


        Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        hey, looking your code, i cannot see any inheritence between your two classes. you must tell that Derv is derived from Base (public, protected or private as you like...) :

        class Base {
        public:
        int x;
        virtual void f();
        };

        class Derv : public Base {
        public:
        int y;
        void f();
        };

        avenger_sb25 wrote: Suppose the derived class array was allocated at 0x00000000 impossible, nothing can be allocated at the address 0x00000000 (and even the operating system don't !). you must test if the address returned by new is different from NULL ((void*)0x00000000) :

        Base* p;
        p = new Derv[4];
        if (p != NULL) {
        for (int i = 0; i < 4; i++) {
        p[i]->y = 10;
        }
        delete[] p;
        }


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        1 Reply Last reply
        0
        • U User 580446

          Hey Avenger, Here's a funny workaround... DWORD dw = (DWORD) p; // move in the array using this Base* p2 = NULL; for(int i=0;i<4;i++) { p2 = (Base*) dw; p2->y=10; dw += sizeof(Derv); // move to the next elem } What do you think?? :-D Clinton

          A Offline
          A Offline
          avenger_sb25
          wrote on last edited by
          #4

          Nice trying Clinton!:-D If you come up with a better solution, do let me know chao!:rose: ...Avenger


          Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups