C 语言最大难点揭秘[3]

发表于:2008-04-25来源:作者:点击数: 标签:难点揭秘语言
关键字:c++悬空指针

  悬空指针比较棘手。当程序员在内存资源释放后使用资源时会发生悬空指针(请参见清单 5):


  清单 5. 悬空指针
               
       void f8()
       {
    struct x *xp;

    xp = (struct x *) malloc(sizeof (struct x));
    xp.q = 13;
    ...
    free(xp);
    ...
        /* Problem!  There's no guarantee that
    the memory block to which xp points
    hasn't been overwritten. */
    return xp.q;

原文转自:http://www.ltesting.net