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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:03:46+00:00 2026-05-27T04:03:46+00:00

What, if anything, is theoretically wrong with this c/c++ statement: *memory++ = BIT_MASK &

  • 0

What, if anything, is theoretically wrong with this c/c++ statement:

*memory++ = BIT_MASK & *memory;

Where BIT_MASK is an arbitrary bitwise AND mask, and memory is a pointer.

The intent was to read a memory location, AND the value with the mask, store the result at the original location, then finally increment the pointer to point to the next memory location.

  • 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-27T04:03:47+00:00Added an answer on May 27, 2026 at 4:03 am

    You are invoking undefined behaviour because you reference memory twice (once for reading, once for writing) in a single statement without an intervening sequence point, and the language standards do not specify when the increment will occur. (You can read the same memory multiple times; the troubles occur when you try to mix some writing in with the reading – as in your example.)

    You can use:

    *memory++ &= BIT_MASK;
    

    to achieve what you want to achieve without incurring undefined behaviour.


    In the C standard (ISO/IEC 9899:1999 aka C99), §6.5 ‘Expressions’, ¶2 says

    Between the previous and next sequence point an object shall have its stored value
    modified at most once by the evaluation of an expression. Furthermore, the prior value
    shall be read only to determine the value to be stored.70)

    That’s the primary source in the C standard. The footnote says:

    This paragraph renders undefined statement expressions such as

    i = ++i + 1;
    a[i++] = i;
    

    while allowing

    i = i + 1;
    a[i] = i;
    

    In addition, ‘Annex C (informative) Sequence Points’ has an extensive discussion of all this.

    You would find similar wording in the C++ standard, though I’m not sure it has an analogue to ‘Annex C’.

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

Sidebar

Related Questions

Has anything useful come out of Intentional or Language Oriented Programming? This isn't a
Is there anything wrong with the optimization of overloading the global operator new to
I have read McCarthy's 1960 paper on LISP and found no reference to anything
I've not seen anything in this area I would recommend to a client. If
Before actually asking anything, I'll go ahead and say this is a theoretical question;
This is a theoretical question more than anything else. In the case of C#
Anything thats as good and as stable and as feature-rich as gigaspaces?
Anything would be good. Exemples, best practices, samples, etc.
Anything one should watch out for when using CF8's CFFEED tag? Oh, here's a
Anything I have tried didn't work. Currenly I have following code to change asp.net

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.