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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:18:43+00:00 2026-05-25T16:18:43+00:00

Working my way through a C tutorial #include <stdio.h> int main() { short s

  • 0

Working my way through a C tutorial

#include <stdio.h>

int main() {
  short s = 10;
  int i = *(int *)&s; // wonder about this
  printf("%i", i);
  return 0;
}

When I tell C that the address of s is an int, should it not read 4 bytes?

Starting from the left most side of 2 bytes of s. In which case is this not critically dangerous as I don’t know what it is reading since the short only assigned 2 bytes?

Should this not crash for trying to access memory that I haven’t assigned/belong-to-me?

  • 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-05-25T16:18:43+00:00Added an answer on May 25, 2026 at 4:18 pm

    First of all, never do this.

    As to why it doesn’t crash: since s is a local, it’s allocated on the stack. If short and int have different sizes in your architecture (which is not a given), then you will probably end up reading a few more bytes from memory that’s on the same memory page as the stack; so and there will be no access violation (even though you will read garbage).

    Probably.

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

Sidebar

Related Questions

I'm working my way through this tutorial here about progress dialogues but have hit
I'm starting out with wxPython and have been working my way through every tutorial
I'm working my way through the NotePad tutorial, and that's all fine - I'm
I've been working my way through the NerdDinner MVC tutorial , and I just
I'm currently working my way through the MVC Music Store tutorial. I'm stuck on
I am working my way through a make tutorial. Very simple test projects I
I'm working my way through the Rails Tutorial book by Hartl and I'm completely
I'm new to Ruby on Rails and am working my way through the tutorial
Working through the NerdDinner Tutorial , I'm trying to figure out a good way
I'm really new at this. I've been working my way through a couple of

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.