Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:36:02+00:00 2026-06-11T17:36:02+00:00

I had always thought that checking the pointer after casting a void* to a

  • 0

I had always thought that checking the pointer after casting a void* to a struct* was a valid way to avoid invalid casts. Something like

MyStructOne* pStructOne = (MyStructOne*)someVoidPointer;
if(!pStructOne)
   return 0;

It appears that this is not the case as I can cast the same data to two different structs and get the same valid address. The program is then happy to populate my struct fields with whatever random data is in there.

What is a safe way of casting struct pointers?

I can’t use dynamic_cast<> as it’s not a class.

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:36:03+00:00Added an answer on June 11, 2026 at 5:36 pm

    If you have any control over the struct layout you can put your own type enumeration at the front of every struct to verify the type. This works in both C and C++.

    If you can’t use an enumeration because not all types are known ahead of time, you can use a GUID. Or a pointer to static variable or member that is unique per struct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Up until today, I had always thought that decent compilers automatically convert struct pass-by-value
I have modified a working Windows service that had always been starting beforehand. After
I've always thought that the concept of Marshalling had a bit of a funny
I have a ccarray and I thought that I always had to know the
I had always thought that $HOME and ~ were exactly the same and thus
I've always thought that F# had two different ways to pass arguments, curry style
I always thought elements that had display:none CSS style returned 0 for height() and
For example, if somestruct has three integer members, I had always thought that it
Generally, I had thought it was always better to store images in the filesystem
During the last years I always thought that in Java, Reflection is widely used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.