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

  • Home
  • SEARCH
  • 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 514335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:30:50+00:00 2026-05-13T07:30:50+00:00

struct a { public int x; } struct c { public byte x; }

  • 0
struct a { public int x; }
struct c { public byte x; }
unsafe class Program
{
    static void Main(string[] args)
    {
        a V = new a();
        V.x = 1;
        c* b = (c*)(void*)&V;
        for(int n = 0;n != 300;n++) b->x++;
        Console.WriteLine(V.x);
        Console.ReadKey(true);
    }
}

45

it really returns 45.
why doesn’t it throw any exceptions?

  • 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-13T07:30:51+00:00Added an answer on May 13, 2026 at 7:30 am

    Why would you expect it to throw an exception?

    By default, C# code is unchecked, which means that overflows and such are just silently ignored. You can find your current project settings for this setting by going to the project properties, Build-tab, and clicking on the Advanced button in the tab-contents. In the dialog that appears, you should have a “Check for arithmetic overflow/underflow”. This setting is by default unchecked.

    In this case, you’re incrementing the first byte of the 32-bit (4-byte) int-value x, by going through a pointer cast as c, and you’re incrementing it 300 times.

    This increments it 256 times, which results in it being back to 0, and then another 44 times. Since it started at 1, it is now 45.

    You can easily test this by executing the following code:

    Byte b = 255;
    b++;
    Console.Out.WriteLine("b=" + b);
    

    You’ll get:

    b=0

    not an overflow exception.

    Having said all that, you should probably not use pointers and unsafe code until you actually really need it.

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

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use .queue() to stick it on the default… May 15, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer You should read both files simultaneously line-by-line and insert the… May 15, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer Actually, you seem to have a syntax error in the… May 15, 2026 at 8:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.