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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:54:53+00:00 2026-06-10T18:54:53+00:00

From this article . Another use for declaring a variable as register and const

  • 0

From this article.

Another use for declaring a variable as register and const is to inhibit any non-local change of that variable, even trough taking its address and then casting the pointer. Even if you think that you yourself would never do this, once you pass a pointer (even with a const attribute) to some other function, you can never be sure that this might be malicious and change the variable under your feet.

I don’t understand how we can modify the value of a const variable by a pointer. Isn’t it undefined behavior?

const int a = 81;
int *p = (int *)&a;
*p = 42; /* not allowed */
  • 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-06-10T18:54:54+00:00Added an answer on June 10, 2026 at 6:54 pm

    The author’s point is that declaring a variable with register storage class prevents you from taking its address, so it can not be passed to a function that might change its value by casting away const.

    void bad_func(const int *p) {
        int *q = (int *) p;            // casting away const
        *q = 42;                       // potential undefined behaviour
    }
    
    void my_func() {
        int i = 4;
        const int j = 5;
        register const int k = 6;
        bad_func(&i);                  // ugly but allowed
        bad_func(&j);                  // oops - undefined behaviour invoked
        bad_func(&k);                  // constraint violation; diagnostic required
    }
    

    By changing potential UB into a constraint violation, a diagnostic becomes required and the error is (required to be) diagnosed at compile time:

    c11

    5.1.1.3 Diagnostics

    1 – A conforming implementation shall produce at least one diagnostic message […] if a preprocessing translation unit or translation unit
    contains a violation of any syntax rule or constraint, even if the behavior is also explicitly
    specified as undefined or implementation-defined.

    6.5.3.2 Address and indirection operators

    Constraints

    1 – The operand of the unary & operator shall be […] an lvalue that designates an object that […] is
    not declared with the register storage-class specifier.

    Note that array-to-pointer decay on a register array object is undefined behaviour that is not required to be diagnosed (6.3.2.1:3).

    Note also that taking the address of a register lvalue is allowed in C++, where register is just an optimiser hint (and a deprecated one at that).

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

Sidebar

Related Questions

i have read this article from dev.mysql . in that page is a example
From this article I see I can use msbuild to apply configuration transformations but
I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
I was reading about data driven testing using mbunit from this article. http://blog.benhall.me.uk/2007/04/mbunit-datafixture-data-driven-unit.html I
I'm struggling to find how to call the equivalent of (taken from this article
This article from Microsoft details how to implement transport security with an anonymous client
I have read this article from High Scalability about Stack Overflow and other large
After reading this article, it makes sense to rebase to gather changes from the
Firstly, sorry for my English (I'm from China). By reading this article( how to
I have previously read Spolsky's article on character-encoding, as well as this from dive

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.