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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:04:12+00:00 2026-05-28T16:04:12+00:00

I was just wondering how disastrous integer overflow really is. Take the following example

  • 0

I was just wondering how disastrous integer overflow really is. Take the following example program:

#include <iostream>

int main()
{
    int a = 46341;
    int b = a * a;
    std::cout << "hello world\n";
}

Since a * a overflows on 32 bit platforms, and integer overflow triggers undefined behavior, do I have any guarantees at all that hello world will actually appear on my screen?


I removed the "signed" part from my question based on the following standard quotes:

(§5/5 C++03, §5/4 C++11) If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

(§3.9.1/4) Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer. This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

  • 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-28T16:04:13+00:00Added an answer on May 28, 2026 at 4:04 pm

    As pointed out by @Xeo in the comments (I actually brought it up in the C++ chat first):

    Undefined behavior really means it and it can hit you when you least expect it.

    The best example of this is here: Why does integer overflow on x86 with GCC cause an infinite loop?

    On x86, signed integer overflow is just a simple wrap-around. So normally, you’d expect the same thing to happen in C or C++. However, the compiler can intervene – and use undefined behavior as an opportunity to optimize.

    In the example taken from that question:

    #include <iostream>
    using namespace std;
    
    int main(){
        int i = 0x10000000;
    
        int c = 0;
        do{
            c++;
            i += i;
            cout << i << endl;
        }while (i > 0);
    
        cout << c << endl;
        return 0;
    }
    

    When compiled with GCC, GCC optimizes out the loop test and makes this an infinite loop.

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

Sidebar

Related Questions

Just wondering what the difference between MFC control messages prefixed with the following is:
Just wondering what the best (fastest execution time) method for translating the following SQL
Just wondering if theres a program you can paste a JSON response into and
Just wondering how difficult it would be to convert the following library to Objective-C
Just wondering if any know of an elegant solution for the following. If I
just wondering if I have a webpage that generates a pdf, but could take
Just wondering if its possible to 301 redirect an existing Rewriterule? For example if
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview
Just wondering if a .NET app can be compiled down to native machine code
Just wondering if there is an easy way to add the functionality to duplicate

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.