游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4521|回复: 8

为何一个指针在debug和release中出现不同两种情况

[复制链接]

71

主题

399

帖子

405

积分

中级会员

Rank: 3Rank: 3

积分
405
发表于 2006-11-28 18:35:00 | 显示全部楼层 |阅读模式
class node
{
        public:
                // class constructor
                node();
                // class destructor
                ~node();
                node *next[2];
                // No description
                void printnode();
                int num;
                int val;
                // No description
                void add(int out);
};

void node::printnode()
{
    int i=0;
    i=next[0]->num;
    cout<<i<<endl;
}

在vc6中,用release编译出来运行没问题,但用debug编译到了i=next[0]->num;就出现
unhandled exception in **.exe :0x0000005:Access Violation
而用devcpp就不论是否加上调试信息都会出错
请问何解?

9

主题

688

帖子

688

积分

高级会员

Rank: 4

积分
688
发表于 2006-11-28 22:32:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

信息不足,无法判断

36

主题

1047

帖子

1147

积分

金牌会员

Rank: 6Rank: 6

积分
1147
发表于 2006-11-29 10:14:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

空指针访问错误。

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-29 10:33:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

vc6在Debug模式下会对指针有一些保护性措施,以便在错误使用时能够提供调试信息
而release模式下就没有了
你应该把使用的代码贴出来才能知道具体位置
不过肯定是使用了未初始化的指针导致

for your reference:

Magic debug values
Magic debug values are specific values written to memory during allocation or deallocation, so that it will later be possible to tell whether or not they have become corrupted and to make it obvious when values taken from uninitialized memory are being used.

Memory is usually viewed in hexadecimal, so common values used are often repeated digits or hexspeak.

Famous and common examples include:

0xBAADF00D
0xBAADFEED
0xBADBADBADBAD
Burroughs B6700 "uninitialized" memory (48-bit words)
0xC0EDBABE
0xC001D00D
0xCCCCCCCC
Used by Microsoft's C++ compiler to mark uninitialised stack areas in debug mode.
0xCDCDCDCD
Used by Microsoft's C++ debugging heap to mark uninitialised heap areas.
0xDDDDDDDD
Used by MicroQuill's SmartHeap and Microsoft's C++ debugging heap to mark memory returned to the heap.
0xDEADBEEF
Famously used on IBM systems such as the RS/6000, also in OPENSTEP Enterprise and the Commodore Amiga.
0xEBEBEBEB
From MicroQuill's SmartHeap.
OxFACADE
Used by a number of real-time OS's
0xFD
Used by Microsoft's C++ debugging heap to mark guard bytes in the heap.
0xFEEEFEEE
Used by Microsoft's C++ compiler to mark the storage area of a deleted class in debug mode.
Note that most of these are each 8 nybbles (32 bits) long, as most modern computers are designed to manipulate 32 bits at a time.

The prevalence of these values in Microsoft technology is no coincidence; they are discussed in detail in Steve McGuire's well-known book Writing Solid Code from Microsoft Press. He gives a variety of criteria for these values, such as:

They should not be useful; that is, most algorithms that operate on them should be expected to do something unusual. Numbers like zero don't fit this criterion.
They should be easily recognized by the programmer as invalid values in the debugger.
On machines that don't have byte alignment, they should be odd, so that dereferencing them as addresses causes an exception.
They should cause an exception, or perhaps even a debugger break, if executed as code.
Since they were often used to mark areas of memory that were essentially empty, some of these terms came to be used in phrases meaning "gone, aborted, flushed from memory"; e.g. "Your program is DEADBEEF".

71

主题

399

帖子

405

积分

中级会员

Rank: 3Rank: 3

积分
405
 楼主| 发表于 2006-11-29 17:44:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

  谢谢各位指教,不过我的情况好像有点莫名其妙,我在初始化函数node::node()加上num=0;就没事了,难道普通变量也要初始化才通过debug?

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-29 18:14:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

当对象产生时
未初始化的数值可能是堆或栈中的残留数据
所以可视作随机值
LZ可以观察一下,在num不初始为0的情况下,每次构造时是多少

15

主题

368

帖子

406

积分

中级会员

Rank: 3Rank: 3

积分
406
发表于 2006-12-7 03:03:00 | 显示全部楼层

Re: Re:为何一个指针在debug和release中出现不同两种情况

wjk98550328: Re:为何一个指针在debug和release中出现不同两种情况

debug下新new的指针和内存,vc帮你置0,release则没有这样,导致release会出现很多危险的非空指针。。。。

错了。是release下的空指针多。因为debug下new内存,vc自动置为0xcdcd(为什么置这个值,是因为这个值不存在与此相对应的机器码,保持执行到这种数据时时容易立即引发错误),而在release下不自动填充0xcdcd,所以在release下空指针挺多的。

121

主题

2029

帖子

2034

积分

金牌会员

Rank: 6Rank: 6

积分
2034
QQ
发表于 2006-12-18 21:06:00 | 显示全部楼层

Re:为何一个指针在debug和release中出现不同两种情况

to楼上。是0xcccc。x86里面,0xcccc是int 3的机器码。

15

主题

368

帖子

406

积分

中级会员

Rank: 3Rank: 3

积分
406
发表于 2007-1-8 01:01:00 | 显示全部楼层

Re: Re:为何一个指针在debug和release中出现不同两种情况

lingjingqiu: Re:为何一个指针在debug和release中出现不同两种情况

to楼上。是0xcccc。x86里面,0xcccc是int 3的机器码。

0xcccc是VC给栈内未初始化变量的初值。而非new出来的数据块内容初值。
这个问题很好验证,为什么不验证一下再来纠正我所说的?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-26 05:42

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表