在进行TAU G2编程时经常会遇到下面的错误信息:
*************************** ERROR **************************
Dereferencing of NULL pointer.
Pointer assigned new data area at address .
有必要详细说明一下。
一般出现这个问题,都是在访问一个类或结构的变量时出现的,例如下面是程序的片段。
D_attach_detach_group_identity D_attach_detach_group_identity_type;
Group_identity_downlink Group_identity_downlink_type;
Group_identity_downlinks Group_identity_downlinks_type;
CArray < Group_identity_downlink, 63 > data;
D_attach_detach_group_identity_type.pdu_type = 10 ;
D_attach_detach_group_identity_type.group_identity_report = false ;
D_attach_detach_group_identity_type.group_identity_acknowlegement = true ;
D_attach_detach_group_identity_type.group_identity_attach_detach_mode = true ;
D_attach_detach_group_identity_type.O_Bit = true ;
D_attach_detach_group_identity_type.M_Bit = true ;
Group_identity_downlink_type.Is_gIADTI_Present = true ;
Group_identity_downlink_type.gIADTI = detach;
Group_identity_downlink_type.group_id_attach_or_detach.group_id_detachment_downlink = 3 ;
Group_identity_downlink_type.Is_gIAT_Present = true ;
Group_identity_downlink_type.gIAT = gssi;
Group_identity_downlink_type.group_id_address.group_short_subscriber_identity = 1 ;
D_attach_detach_group_identity_type.group_identity_downlinks.data[ 0 ] = Group_identity_downlink_type;
D_attach_detach_group_identity_type.group_identity_downlinks.NoOfRepeatedElements = 1 ;
Group_identity_downlink Group_identity_downlink_type;
Group_identity_downlinks Group_identity_downlinks_type;
CArray < Group_identity_downlink, 63 > data;
D_attach_detach_group_identity_type.pdu_type = 10 ;
D_attach_detach_group_identity_type.group_identity_report = false ;
D_attach_detach_group_identity_type.group_identity_acknowlegement = true ;
D_attach_detach_group_identity_type.group_identity_attach_detach_mode = true ;
D_attach_detach_group_identity_type.O_Bit = true ;
D_attach_detach_group_identity_type.M_Bit = true ;
Group_identity_downlink_type.Is_gIADTI_Present = true ;
Group_identity_downlink_type.gIADTI = detach;
Group_identity_downlink_type.group_id_attach_or_detach.group_id_detachment_downlink = 3 ;
Group_identity_downlink_type.Is_gIAT_Present = true ;
Group_identity_downlink_type.gIAT = gssi;
Group_identity_downlink_type.group_id_address.group_short_subscriber_identity = 1 ;
D_attach_detach_group_identity_type.group_identity_downlinks.data[ 0 ] = Group_identity_downlink_type;
D_attach_detach_group_identity_type.group_identity_downlinks.NoOfRepeatedElements = 1 ;
在这个程序中,访问D_attach_detach_group_identity_type、Group_identity_downlink_type和Group_identity_downlinks_type这三个变量的成员时都会报告错误。虽然可以忽略这个错误继续运行,但是如果这个错误很多,却是非常麻烦。为什么会出现这个错误呢?
最主要的原因是在TAU G2中,类或结构体变量都是看作指针的,如果在WATCH窗口看这些变量,在访问变量前,它们都是null,而在赋值之后会显示变量的地址,这和C语言对指针变量的处理基本相同。所以在给这些变量第1次赋值的时候就会报告访问空指针。如果想避免这个问题,就应该在赋值前先使用new关键词分配空间。如:
CArray
<
Group_identity_downlink,
63
>
data;
D_attach_detach_group_identity_type = new D_attach_detach_group_identity();
Group_identity_downlink_type = new Group_identity_downlink();
D_attach_detach_group_identity_type.pdu_type = 10 ;
D_attach_detach_group_identity_type.group_identity_report = false ;
D_attach_detach_group_identity_type.group_identity_acknowlegement = true ;
D_attach_detach_group_identity_type.group_identity_attach_detach_mode = true ;
……
D_attach_detach_group_identity_type = new D_attach_detach_group_identity();
Group_identity_downlink_type = new Group_identity_downlink();
D_attach_detach_group_identity_type.pdu_type = 10 ;
D_attach_detach_group_identity_type.group_identity_report = false ;
D_attach_detach_group_identity_type.group_identity_acknowlegement = true ;
D_attach_detach_group_identity_type.group_identity_attach_detach_mode = true ;
……
这样就可以避免出现此类问题了。