Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

c++ - Is it ever safe to reinterpret_cast the address of a variable to the address of a union?

Is the following well-defined (i.e. does not reply on undefined or implementation defined behaviour)?

header.hpp:

#pragma once

union union_type { 
  char   c; 
  // many member types...
  double d[4]; 
}; 

static_assert(alignof(union_type) != alignof(char));

int func1(char & arg1);
int func2(union_type & arg2);
int func3(void * arg3, bool arg3_is_char);

func1.cpp

int func1(char & arg1) {
  // perform some operation with arg1, might modify it
}

func2.cpp

#include “header.hpp”
int func2(union_type & arg2) {
  return func1(arg2.c);
} 

func3.cpp

#include “header.hpp”
int func3(void * arg3, bool arg3_is_a_char) {
  if (arg3_is_a_char && arg3) {

    // does the following cast rely on undefined 
    // or implementation defined behaviour?
    auto * ptr = reinterpret_cast<union_type *>(arg3);

    return func2(*ptr);
  }
  return 0;
}

Empirically, the above code seems to work as expected (regardless of the underlying alignment of the memory pointed to by arg3). That is, even when the address pointed to by arg3 does not have the appropriate alignment.

My concern is that this is relying on underlined behaviour and some future optimisation might make an assumption about what the valid address of arg2 can be and then use a different address of union_type::c

EDIT: For those interested in the background to this question, union_type and void* represent output parameters from public API which does not expose any STL types. There are then several accessors for the two types which continue to grow and diverge. Being able to cast the void** to the union_type would have allowed half to the wrappers to be removed.

question from:https://stackoverflow.com/questions/66062364/is-it-ever-safe-to-reinterpret-cast-the-address-of-a-variable-to-the-address-of

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...