-
June 2nd, 2009, 03:41 AM
#1
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.
-
June 2nd, 2009, 04:08 AM
#2
Re: pointer class
it works, but you corrupt the heap with that.
-
June 2nd, 2009, 04:24 AM
#3
Re: pointer class
What is heap? Is it a large pool of unused memory ?
-
June 2nd, 2009, 07:23 AM
#4
Re: pointer class
Yes, and the place where dynamic memory allocations are made.
-
June 2nd, 2009, 07:55 AM
#5
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.
-
June 2nd, 2009, 08:04 AM
#6
Re: pointer class
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.
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.
-
June 2nd, 2009, 03:17 PM
#7
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??
-
June 2nd, 2009, 04:11 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|