CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: pointer class

  1. #1
    Join Date
    Mar 2009
    Posts
    82

    pointer class

    I got one code:
    Code:
    #include <iostream>
    using namespace std;
    
    class objekat
    {
          int test;
    
          public:
    
          void definiraj_test(int a){ test=a; };
    
          void pecati_test(){ cout<<test<<endl;}
    };
    
    
    int main()
    {
        objekat *pokazuvac;
        
        pokazuvac = new objekat;
        
        for(int i=0;i<5;i++)
        {
              pokazuvac[i].definiraj_test(i);
        }
        
        for(int i=0;i<5;i++)
        {
                pokazuvac[i].pecati_test();
        }
        
        system("PAUSE");
        
    }
    There is pointer which points to class "objekat". Also there are functions which define and print the private variable "test".

    First I declare the pointer objekat *pokazuvac;. Then pokazuvac = new objekat;.

    Now the pointer points to one object. But what if I do something like:

    Code:
     for(int i=0;i<5;i++)
        {
              pokazuvac[i].definiraj_test(i);
        }
    Why it still works? The pointer points to one object, and not array of objects??


    Thanks in advance.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: pointer class

    it works, but you corrupt the heap with that.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer class

    What is heap? Is it a large pool of unused memory ?

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: pointer class

    Yes, and the place where dynamic memory allocations are made.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer class

    Ok, thank you. And you mean that by not defining how many objects should be used (pokazuvac=new objekat[20]) there could be memory overflow?? I think the compiler should prevent this stuff.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: pointer class

    Quote Originally Posted by StGuru
    And you mean that by not defining how many objects should be used (pokazuvac=new objekat[20]) there could be memory overflow?
    No, the problem is that you are treating a lone object as an array, and then accessing this "array" "out of bounds", so to speak. Consequently, you can create an array of 20 objects and still access it out of bounds.

    Quote Originally Posted by StGuru
    I think the compiler should prevent this stuff.
    But you see, if p is a pointer and n is an integer, then p[n] is equivalent to *(p + n). So, in the case where n is 0, p[0] is equivalent to *p, but it would be rather strange to use p[0] instead of *p when p points to a lone object instead of an object within an array.

    Still, it is possible for static analysis to detect such array out of bounds access in some cases, but compilers are not required to do so. As an alternative, you could use a std::vector with the at() member function, in which case std::out_of_range would be thrown on an attempt to access the dynamic array out of bounds.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Mar 2009
    Posts
    82

    Re: pointer class

    thanks for the reply laserlight. Usually when I go out of bond at some array, I got 13423432 number for the variable. In this case perfectly works, which is strange. Anyway, how to use that vector stuff. Is it like set in Pascal??

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointer class

    It's been many years since I touched Pascal, thus I can't answer that question directly. However, vector's interface is fairly straightforward:

    http://www.cplusplus.com/reference/stl/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured