Down casting -- memory issues


Hi All
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Class B
{};

Class Derived : Public B
{int a[1000];
};

int main()
{   Base b;
  Derived*Dptr=static_cast<Derived*>(&b)
....
...some code
....
}


Here i typecasted Base address into derived and derived occupies more memory than base(1000*2 extra here).So how this memory is being adjusted when typecasting happens.

Earlier same address(&b) could have reserved 1 byte as Base is an empty class,Now that it is turned into Derived ,doesnt it have to reserve extra space to accomadate these 1000 ints at this address ?Wont memory get corrupted if this happens?

Pls help!!
Your program is incorrect. You can't use a Derived* to point to a Base object. What you have here is simply undefined behaviour.
So how this memory is being adjusted when typecasting happens.
It isn't.
Therefore casting (except for dynamic_cast) is dangerous. You may read/write memory that's not yours.
@Thanks all.
Peter87 (5388),coder777 (3857)
This info is very helpful.I figured out now.
Topic archived. No new replies allowed.