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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:21:20+00:00 2026-05-16T18:21:20+00:00

EDIT Public health warning – this question includes a false assumption about undefined behaviour.

  • 0

EDIT Public health warning – this question includes a false assumption about undefined behaviour. See accepted answer.

After a reading recent blog post, I’ve been thinking a lot about the practicality of avoiding all standards-undefined assumptions in C and C++ code. Here is a snippet cut out of C++, to do an unsigned 128-bit addition…

void c_UInt64_Pair::operator+= (const c_UInt64_Pair &p)
{
  m_Low  += p.m_Low;
  m_High += p.m_High;

  if (m_Low < p.m_Low)  m_High++;
}

This clearly relies on assumptions about overflow behaviour. Obviously most machines can support a binary integer of the right kind (though perhaps building from 32-bit chunks or whatever), but there’s apparently a growing chance that the optimiser may exploit the standards-undefined behaviour here. That is, the only way that the m_Low < p.m_Low condition can pass is if m_Low += p.m_Low overflows, which is undefined behaviour, so the optimiser can legally decide that the condition always fails. In which case, this code is simply broken.

The question is, therefore…

How can you write a reasonably efficient version of the above without relying on undefined behaviour?

Assume that you have an appropriate 64-bit binary machine integer, but that you have a malicious compiler that will always interpret your undefined behaviour in the worst possible (or impossible) way. Also, assume that you don’t have some special built-in, intrinsic, library or whatever to do it for you.

EDIT minor clarification – this isn’t just about detecting overflow, but also ensuring that both m_Low and m_High end up with the correct modulo 2^64 results, which is also standards-undefined.

  • 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-16T18:21:20+00:00Added an answer on May 16, 2026 at 6:21 pm

    From the C++ 1998 standard, 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.” Note that “integer”, here, refers to any integer type rather than just int.

    Therefore, assuming that those are unsigned integers, like the “UInt64” in the type suggests, this is defined behavior in C++ and should work as expected.

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

Sidebar

Related Questions

Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
I am getting a StackOverflow Error in this code: EDIT [XmlAttribute(ID)] public string ID
I have this function: public static void Play(string FileName, bool Async = false) {
let me tell you a bit about where this question came from. I have
public ActionResult Edit(string param) { } http://localhost/Edit/5,someothervalue,etc How can i reach 'param' from a
I have the following code: [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = RoleKeys.Administrators)] public ActionResult Edit(int id, FormCollection
Edit: Added code (Exception on line 095, 5th time it's hit.) public DataTable ParseBarcodes(String[]
public void EditAndSave(String fileName) { Bitmap b = new Bitmap(fileName); /** * Edit bitmap
I have following function in one Activity public void AppExit() { Editor edit =
I have some CRUD routing: /news/{page} // public /news/new // private /news/{slug}/show //public /news/{slug}/edit

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.