Pointer cast question on

Users questions: In pointer casts: ptr1 = (TYPE *) ptr2, if sizeof (ptr2 type) is greater than sizeof (ptr1 type), then use the pointer ptr1 ptr2 points to access the storage zone is safe. If siz eof (ptr2 type) is less than sizeof (ptr1 type), then use the pointer ptr1 ptr2 points to access the storage zone is unsafe why ??????*
Experts answer: because when sizeof (ptr2 type) less than siz eof (ptr1 type), that ptr1 points to the address of the element than ptr2 large, if ptr1 = (TYPE *) ptr2, because ptr1 ptr2 need more memory than the big, the extra memory is that part of the overflow by acquisition, that portion of the memory are Is unpredictable (it may be blank system resources, is also possible that your program variable storage location, etc.), so use the pointer ptr1 ptr2 points to access the storage zone is unsafe. In the use of pointers ptr1 ptr2 points to access the storage area is safe when it is due to Than the required address for the prt2 ptr1 is small, the memory pointed to by ptr1 memory area is also within the scope.
  • This information provided by the users.Thanks!