为什么不能将int分配给struct成员的union成员?

时间:2018-06-21 21:33:55

标签: c++

我正在尝试为作为结构成员的并集成员分配值,但似乎未分配该值。 我已经完成了程序,在分配之后,len_param.data值似乎是某种指针(请参阅文章末尾的GDB输出)

此外,如果我printf,它可以正常运行:

len_param.data.v_int = 4;

// If I uncomment this line, IT RUNS FINE! WHY?
// printf("len_param.data.v_int: %i \n", len_param.data.v_int);

len_param的类型如下:

struct Parameter {
    enum {
        NORMAL, ARRAY, SKIP,
    } type;

    GIDirection direction;

    GIArgument data;
};

GIArgument定义:

union _GIArgument
{
  gboolean v_boolean;
  gint8    v_int8;
  guint8   v_uint8;
  gint16   v_int16;
  guint16  v_uint16;
  gint32   v_int32;
  guint32  v_uint32;
  gint64   v_int64;
  guint64  v_uint64;
  gfloat   v_float;
  gdouble  v_double;
  gshort   v_short;
  gushort  v_ushort;
  gint     v_int;
  guint    v_uint;
  glong    v_long;
  gulong   v_ulong;
  gssize   v_ssize;
  gsize    v_size;
  gchar *  v_string;
  gpointer v_pointer;
};
typedef _GIArgument GIArgument;

完整的文件可以在这里找到:https://gist.github.com/romgrk/642388914a9ff412eb5683fca44009d7#file-function-cc-L255

我在那一步走的时候的GDB输出是:

Thread 1 "node" hit Breakpoint 1, GNodeJS::FunctionInvoker (info=...) at ../src/function.cc:256
256                     len_param.data.v_int = GetV8ArrayLength(info[in_arg]);
(gdb) step
GNodeJS::GetV8ArrayLength (value=...) at ../src/function.cc:25
25  static int GetV8ArrayLength (Local<Value> value) {
(gdb) finish
Run till exit from #0  GNodeJS::GetV8ArrayLength (value=...) at ../src/function.cc:25
GNodeJS::FunctionInvoker (info=...) at ../src/function.cc:260
260                     callable_arg_values[length_i].v_pointer = &len_param.data;
Value returned is $1 = 4
(gdb) p len_param.data.v_int
$2 = -17928
(gdb) p len_param
$3 = {
  type = GNodeJS::Parameter::SKIP, 
  direction = GI_DIRECTION_INOUT, 
  data = {
    v_boolean = -17928, 
    v_int8 = -8 '\370', 
    v_uint8 = 248 '\370', 
    v_int16 = -17928, 
    v_uint16 = 47608, 
    v_int32 = -17928, 
    v_uint32 = 4294949368, 
    v_int64 = 140737488337400, 
    v_uint64 = 140737488337400, 
    v_float = -nan(0x7fb9f8), 
    v_double = 6.9533558069492434e-310, 
    v_short = -17928, 
    v_ushort = 47608, 
    v_int = -17928, 
    v_uint = 4294949368, 
    v_long = 140737488337400, 
    v_ulong = 140737488337400, 
    v_ssize = 140737488337400, 
    v_size = 140737488337400, 
    v_string = 0x7fffffffb9f8 "\t\357\304\303J\a", 
    v_pointer = 0x7fffffffb9f8
  }
}

1 个答案:

答案 0 :(得分:1)

问题可能是,len_param.data除了在范围之外和len_param的生存期到期之前立即获取地址之外,什么也没做。因此,编译器认为没有必要在其中存储任何内容。

下面是定义,使用和死亡len_param的代码段(省略了不必要的代码,并添加了一些注释注释):

if (param.type == Parameter::ARRAY) {

    // ... 

    Parameter len_param = call_parameters[length_i];    // len_param is defined

    if (len_param.direction == GI_DIRECTION_IN) {
        // ...
    }
    else if (len_param.direction == GI_DIRECTION_INOUT) {

        len_param.data.v_int = GetV8ArrayLength(info[in_arg]);

        callable_arg_values[length_i].v_pointer = &len_param.data;
    }
}       // len_param goes out of scope, so it's no longer alive
        // and the pointer that got placed in `callable_arg_values[length_i].v_pointer`
        // is pointing to garbage

根本问题是callable_arg_values[length_i].v_pointer中的指针在存储之后是无效的。