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 7833411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:41:49+00:00 2026-06-02T12:41:49+00:00

While experimenting with C++ and the reinterpret_cast function I tried to convert a short

  • 0

While experimenting with C++ and the reinterpret_cast function I tried to convert a short to a string and got a STATUS_ACCESS_VIOLATION Exception.

   short s = 32767;

   short* s2 = &s;
   string* test4 = reinterpret_cast<string*>(s2);

   cout << "reinterpret_cast: " << *test4 << endl;

In my understanding, a there was a try to use a piece of memory, which is not allocated to my application’s process.

How can I look out for such tryings of forbidden memory accesses? How can I reinterpret only this piece, which contains my short value?

  • 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-02T12:41:52+00:00Added an answer on June 2, 2026 at 12:41 pm

    You are not converting short to string; you are taking the address of a short, and then trying to access the memory at that address as if it contained a string, which it doesn’t.

    Luckily, in your case, this resulted in some random piece of data being interpreted as an invalid pointer, and so you got an access violation error. If there happened to be something there that looked like a valid string structure, then all sorts of weirdness might happen.

    How can I look out for such tryings of forbidden memory accesses?

    Don’t use reinterpret_cast to convert between pointers to unrelated types, and expect the reinterpreted value to point to a valid object; and don’t use it at all, unless you know exactly what you’re doing with it.

    How can I reinterpret only this piece, which contains my short value?

    If you want to convert the number into a string, then you could use a string stream:

    std::ostringstream stream;
    stream << s;
    std::cout << "formatted with a stringstream: " << stream.str() << std::endl;
    

    or, if you’re using C++11, then there are standard functions to convert numbers to strings:

    std::string string = std::to_string(s);
    std::cout << "converted with to_string: " << string << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While experimenting with some stuff on the REPL, I got to a point where
I'm trying to convert .flv videos to .ogg format. After experimenting for a while,
While experimenting with pixel shaders in WPF I decided to draw some pixels onto
While running a makefile in gcc 4.1.2 (linux 5), I got the following error
While working in a project written by some one else ,there's a Function which
While experimenting a bit with C++ templates I managed to produce this simple code,
While experimenting with iPhone app development, we have several AppIDs which should be deleted
I ran into the following algorithmic problem while experimenting with classification algorithms. Elements are
A while ago, I started experimenting with WebSockets with Node.js taking care of the
i've started learning about javascript closures, and while experimenting, i realised that the following

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.