This is mostly out of curiosity, and hopefully someone more knowledgeable will enlighten me.
Let's say we have the following :
1 2 3 4 5 6 7 8 9 10 11
class Foo {
public :
Foo() {//just some init stuff.
for(unsignedint i=0; i<10; i++) {
numbers[i]=0;
}
iPtr=numbers;
}
int numbers[10];
int* iPtr;
};
and we instantiate on the heap like so :
Foo* fooPtr = new Foo();
I've read that in this particular case, Foo::numbers would also be allocated on the heap. My question is, will the elements of Foo::numbers be allocated sequentially? Would it be wise to perform pointer arithmetic on iPtr?
I've read that in this particular case, Foo::numbers would also be allocated on the heap.
This is true. When an object is put on the heap, that means all its memebers are put on the heap.
My question is, will the elements of Foo::numbers be allocated sequentially?
Yes.
Would it be wise to perform pointer arithmetic on iPtr?
It'd be fine, yes.
Although there are other problems with iPtr here that make it unsafe and a bad idea (lack of proper copy/assignment is one -- pointlessness is another... what's the purpose of iPtr if it just points to numbers? Why not just use numbers directly?)
Thanks for your reply Disch! It's certainly cleared things up.
Yes, iPtr has no real purpose. This is just a silly example I slapped together for the sake of demonstration. :)