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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:37+00:00 2026-05-25T21:45:37+00:00

With my compiler at least, creating a reference implies no dereferencing. Therefore, code like

  • 0

With my compiler at least, creating a reference implies no dereferencing. Therefore, code like the following works:

int trivialExample(char* array, int length)
{
    char& ref = array[6];
    if (length > 6)
    {
        std::cout << ref << std::endl;
    }
}

That is, given a char array and its length (and assuming a bunch of trivialities like the array elements are all initialized and the passed length is correct), it will print the seventh character only if it actually exists.

Is this relying on undefined behavior?

  • 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-25T21:45:38+00:00Added an answer on May 25, 2026 at 9:45 pm

    Actually, this is conceptually (and practically) not different than the following:

    int trivialExample(char* array, int length)
    {
        char *ptr = &array[6];
        if (length > 6)
        {
            std::cout << (*ptr) << std::endl;
        }
    }
    

    My educated guess is that you intend to call it this way:

    char buffer[4];
    trivialExample(buffer, sizeof(buffer));
    

    And in C++, as in C, just obtaining a pointer to outside of the declared array (other than the next-to-last) invokes undefined behaviour, even if not dereferenced.

    The rationale is that there may be (are?) architectures that faults just by loading an invalid address in a CPU register.

    UPDATE: After some research, and hints from other SO users, I’ve becomed convinced that C++ does not allow to take a reference outside of the declaring object, not even to the next-to-last element. In this particular case, the results are the same, except for the element number 6, that would be allowed in the pointer version and not in the reference one.

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

Sidebar

Related Questions

Shouldn't the compiler automatically cast to a double in the following? At least according
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
The problem I am trying to avoid code that looks like the following: If(object
Compiler Error Keyword 'this' is not available in the current context delegate void CallBack(int
I am creating a few pictureboxes dynamically, then assigning the following: // class variable
Creating a Lisp syntax like DSL - very small specific application - but very
I would like to pass some options to a compiler. The option would have
I'm trying to introduce Hoopl into some compiler and faced some problem: creating a
Is there a way to re-compile or at least 'check compile' stored procedures en
Everyone (at least everyone who uses a compiled language) has faced compilation errors but

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.