Sign in to follow this  
Muzlack

Pointer arithmetic

Recommended Posts

Does pointer arithmetic work with objects that have dynamically allocated memory? Is this memory stored somewhere completely different from the object itself so that pointer arithemetic can still work? Just curious, Bryce

Share this post


Link to post
Share on other sites
Quote:
Original post by Muzlack
Is this memory stored somewhere completely different from the object itself so that pointer arithemetic can still work?
I'm not sure what you're refering to, why would it need to be stored somewhere else?
But yes, pointer arithmetic works just fine on heap allocated objects.

Share this post


Link to post
Share on other sites
The pointer itself (which is 4 bytes [32-bits] on a 32-bit CPU), is on the stack. It points to the first byte of the object. The object can be stored anywhere, but with new it's stored on the heap. A double is 8 bytes long. So, if you have a pointer to a double, declared as double* pDouble, then double d = pDouble[1] means "Goto where pDouble is pointing, and move forward 1 object. Then move that object into 'd'". The compiler knows how far to move forward, because the type of pDouble is pointer to a double, and the compiler knows that a double is 8 bytes long. So the compiler moves 8 bytes forwards.

Share this post


Link to post
Share on other sites
That answers my question then.

But if you did something like this:

class example
{
private:
int *p;
public:
example(int i);
}


example::example(int i)
{
//reserve i amount of integers
*p = (int *) malloc (sizeof(int)*i);
}

int main()
{
example *test1= new example(50);
example *test2= new example(100);

example *testArithmetic = test1;

testArithmetic++;
}


(I just coded this right here, so it may be incorrect)

testArithmetic wouldn't point to test2, because the way I see it on the heap you would then have:

test1's integer pointer
test1's data x 50
test2's integer pointer
test2's data x 100

and since the pointer only knows the size of the pointer of the class, it would advance to the data, right?

Anyways, I'm just curious, thanks!

Bryce

Share this post


Link to post
Share on other sites
It would point to garbage after that, since you delcare the two class variables
with two DISTINCT calls to new. Each Distinct call will grab a random slot from
memory that matches the requirements of the requested call, there is NO garateee
that they two calls right next to eachother in the code will give memory blocks
that are next to eachother.

Share this post


Link to post
Share on other sites
The pointer knows the size of class objects; here, that size is the size of an int*, since that's the only thing inside the class. So "testArithmetic" now points at the memory immediately following test1 on the heap, but as said, there isn't necessarily anything there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this