I have a struct called myblock and I am given a long int for the memory address. I do (something like):
long address;
...
struct myblock block1 = (struct myblock *)address;
I need to make sure that block1 actually points to a myblock instance. static_cast, etc, are not options.
How to check validity of explicit type conversion in c?
This is one of the disadvantage when passing the address and accessing variables in C.
You can have a identification field as a last field in the structure myblock. Assign it to a constant value. After assigning, check the value if it is as expected, you can make sure that it is pointed to myblock instance.
Not only an extra field, any field validation will do.
roses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment