1

I have seen that in the C++ abstract machines (if the are different), the mere act of forming a point to an invalid point in memory is undefined behavior.

For example

int* arr = new int[10];
int* last = arr + 9;  // last element
int* end = arr + 10; // past last element, still ok according to the rules
int* another = arr + 11;  // invalid, UB, supposedly, the rest of the program can be invalid

Now suppose, this other code

int* arr;  // arr can be invalid already here, UB?
int* another = arr;  // forming a pointer to possibly invalid memory, UB?

How is assigning an uninitialized pointer different from forming an invalid pointer by an operation? is it as bad?

10
  • int *arr; by itself is legal, int *another = arr; I think stops being UB in C++26? Commented Apr 27, 2024 at 5:24
  • 1
    C/C++ doesn't exist its either "C" or "C++" and in C++ using raw pointers is NOT recommended (specifically to avoid dereferencing nullptr's). In C++ you should use std::vector<int> in cases like this. std::vector<int> values; and then first and last will be values.begin() and values.end(). So be very very clear if you want a "C" or a "C++" answer ;) Commented Apr 27, 2024 at 5:28
  • 1
    Assigning a nullptr to another pointers isn't UB, dereferencing a nullptr is! So all of your code is fine (as long as you don't do anything with the pointers you created) Commented Apr 27, 2024 at 5:29
  • @PepijnKramer, yes, I meant two different abstract machines. Are they different in concrete ways? (as much as anything abstract can be concrete). I think for the question the same rules apply to both. fixed. Commented Apr 27, 2024 at 5:30
  • 1
    @alfC Fair enough, I meant uninitialized pointers Commented Apr 27, 2024 at 5:31

2 Answers 2

1
int* arr;  // arr can be invalid already here, UB?

Not UB, since here you are not using/accessing the value of arr.

int* another = arr;  // forming a pointer to possibly invalid memory, UB?

UB, since you are accessing an indeterminate value to assign/initialize to another.

How is assigning an uninitialized pointer different from forming an invalid pointer by an operation? is it as bad?

They are both technically undefined behavior, so from a language lawyer perspective, they are equally bad, as they permit arbitrary behavior on behalf of the implementation.

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting! that means that any class containing a pointer (e.g., a pointer and a size) will have to have the pointer initialized (e.g., nullptr) for the class to have the assignment well-defined after default trivial construction.
@alfC Not necessarily, as long as you do not access the pointer member directly. If I understand correctly, assignment of a structure (or class) to another is well defined, even if one of the members is indeterminate, i.e., accessing the member directly would have UB. Though admittedly, that Q&A is in C, not C++.
No member pointers do NOT have to be initialized (they aren't different in any way to normal pointers). Just as long as you have them initialized before first use (first dereference).
@CPlus, I think your linked question is a completely different beast since it doesn't involve a pointer
1

How is assigning an uninitialized pointer different from forming an invalid pointer by an operation?

In terms of initialization the definition int *arr; is no different than int i;. Both leave the variable uninitialized(assuming these appear in local/block scope and not in global scope).

The important thing is these definitions don't odr-use the variables arr and i.

On the other hand, when you do int *another = arr; you're using unitialized arr to initialize another. This constitutes an odr-use of uninitialized arr for something other than assignment or taking its address. And this is undefined behavior.

4 Comments

odr-use instead of lvalue-to-rvalue conversion?
@JeffGarrett Both I think.
@JeffGarrett, what is "lvalue-to-rvalue conversion" in this context?
@JeffGarrett As alfC mentioned/asked and I said, in int *arr; is no odr-use or lvalue-to-rvalue conversion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.