使用指针的程序发布版本的问题 [英] Problem with release build of program using pointers

查看:57
本文介绍了使用指针的程序发布版本的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


以下代码是一个实际练习,在调试

环境中运行良好但在编译发布版本时失败。我相信

这是因为调试环境负责内存分配和发布版本依赖代码执行此操作。我在我的书籍和网站上的研究没有提供任何帮助。


如果我需要做正确的建议我将不胜感激分配

代码的内存资源,以便正确编译以作为.exe

程序发布。


干杯,


Cam


代码如下:


#include< iostream>

#include< conio.h>


using namespace std;


KeyboardInput(int * pkey); //收集输入

Add1Comp(int * presult); //执行添加


int key1 [8],carrykey1 [8],key2 [8],carrykey2 [8],result [8];

>
int main(int * pkey)////////////////////////////////////// //////// main()

/////////////////////////////// ////////////////////////////

{

do

{

KeyboardInput(key1); //调用其输出为key1的KeyboardInput

KeyboardInput(key2); //调用其输出为key2的KeyboardInput

Add1Comp(result); //调用Add1Comp,其输出结果为

}

while(1);

返回0;

}


int KeyboardInput(int * pkey)//////////////////////////// KeyboardInput()

////////////////////////////////////////// ///

{

重启:

int counter = 0;

cout<< \ n输入一个8位二进制数:" ;;

for(counter = 8; counter> 0; counter - )

{

pkey [counter] =(getche() - 48);

}

cout<< " \ n";

for(counter = 8; counter> 0; counter - )

{

if(pkey [counter]!= 0&& pkey [counter]!= 1)

{

cout<< \ n \ n您输入的是非二进制字符。请

restart\\\
\ n" ;;

goto restart;

}

}

返回0;

}


int Add1Comp(int * presult)////////////// ///////////// Add1Comp()

////////////////////////// ///////////////////////

{

int latch = 0;

restart:

int counter,carry = 0;

for(counter = 1; counter< 9; counter ++)

{

开关(key1 [counter] + key2 [counter] + carry)

{

case 0:// 0 + 0 + 0 = 0携带0

presult [counter] = 0;

cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<

key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带;

carry = 0;

cout<<随行;

休息;

案例1:// 0 + 0 + 1 xor 0 + 1 + 0 xor 1 + 0 + 0 = 1携带0

presult [counter] = 1;

cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<

key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带;

carry = 0;

cout<<随身携带;

休息;

案例2:// 1 + 1 + 0 xor 1 + 0 + 1 xor 0 + 1 + 1 = 0携带1

presult [counter] = 0;

cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<

key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带;

carry = 1;

cout<<随身携带;

休息;

案例3:// 1 + 1 + 1 = 1携带1

presult [counter] = 1;

cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<

key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带;

carry = 1;

cout<<随身携带;

休息;

}

if(counter == 8&& carry == 1)

goto addcarry;

if(counter == 8&& latch == 1&& carry == 1)

goto overflow;

}

cout<< " \ n \\ nnResult of of ;;

for(counter = 8; counter> 0; counter - )

{

if(latch)

key1 [counter] = carrykey1 [counter];

cout<< key1 [counter];

}

cout<< " +" ;;

for(counter = 8; counter> 0; counter - )

{

if(latch)

key2 [counter] = carrykey2 [counter];

cout<< key2 [counter];

}

cout<< " =" ;;

for(counter = 8; counter> 0; counter - )

{

cout<< presult [counter];

}

cout<< " \\\
\\\
----------------------------------------- \\ \\ n" ;;

latch = 0;

返回0;


addcarry:

latch = 1;

cout<< \ n \ n添加最终结转:\ n" ;;

for(counter = 1; counter< 9; counter ++)

{

carrykey1 [counter] = key1 [counter];

key1 [counter] =结果[counter];

carrykey2 [counter] = key2 [counter] ];

key2 [counter] = 0;

presult [counter] = 0;

}

key2 [1] = 1;

goto restart;

溢出:

cout<< \ n添加导致溢出;

main(key1);

}

Hi all,

The code below is a practical exercise and works well running in the debug
environment but fails when being compiled for a release build. I believe
this is because the debug environment takes care of memory allocation and
the release build relies on code to do this. My research in my books and on
the web has not provided any great help.

I would be grateful for advice on what I need to do to correctly allocate
the memory resources for the code to compile correctly to release as an .exe
program.

Cheers,

Cam

Code follows:

#include <iostream>
#include <conio.h>

using namespace std;

KeyboardInput(int *pkey); // Gathers input
Add1Comp(int *presult); // Carries out addition

int key1[8], carrykey1[8], key2[8], carrykey2[8], result[8];

int main (int *pkey) ////////////////////////////////////////////// main()
///////////////////////////////////////////////////////////
{
do
{
KeyboardInput(key1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(key2); // Calls KeyboardInput whose output is key2
Add1Comp(result); // Calls Add1Comp whose output is result
}
while(1);
return 0;
}

int KeyboardInput (int *pkey) //////////////////////////// KeyboardInput()
/////////////////////////////////////////////
{
restart:
int counter = 0;
cout << "\nEnter an 8 bit binary number: ";
for (counter = 8; counter > 0; counter --)
{
pkey[counter] = (getche() - 48);
}
cout << "\n";
for (counter = 8; counter > 0; counter --)
{
if (pkey[counter] != 0 && pkey[counter] != 1)
{
cout << "\n\nYou have entered non-binary character. Please
restart\n\n";
goto restart;
}
}
return 0;
}

int Add1Comp (int *presult) /////////////////////////// Add1Comp()
/////////////////////////////////////////////////
{
int latch = 0;
restart:
int counter, carry = 0;
for (counter = 1; counter < 9; counter ++)
{
switch (key1[counter] + key2[counter] + carry)
{
case 0: // 0+0+0 = 0 carry 0
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 1: // 0+0+1 xor 0+1+0 xor 1+0+0 = 1 carry 0
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 2: // 1+1+0 xor 1+0+1 xor 0+1+1 = 0 carry 1
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
case 3: // 1+1+1 = 1 carry 1
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
}
if (counter == 8 && carry == 1)
goto addcarry;
if (counter == 8 && latch == 1 && carry == 1)
goto overflow;
}
cout << "\n\nResult of ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key1[counter] = carrykey1[counter];
cout << key1[counter];
}
cout << " + ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key2[counter] = carrykey2[counter];
cout << key2[counter];
}
cout << " = ";
for (counter = 8; counter > 0; counter --)
{
cout << presult[counter];
}
cout << "\n\n-----------------------------------------\n";
latch = 0;
return 0;

addcarry:
latch = 1;
cout << "\n\nAdd final carry:\n";
for (counter = 1; counter < 9; counter ++)
{
carrykey1[counter] = key1[counter];
key1[counter] = result[counter];
carrykey2[counter] = key2[counter];
key2[counter] = 0;
presult[counter] = 0;
}
key2[1] = 1;
goto restart;
overflow:
cout << "\nThe addition has resulted in an overflow";
main(key1);
}

推荐答案



" Cam" < retsigerymmudathotmaildotcom>在消息中写道

新闻:40 ****** @ duster.adelaide.on.net ...

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@duster.adelaide.on.net...
大家好,

下面的代码是一个实际练习,在调试环境中运行良好,但在为发布版本编译时失败。我相信这是因为调试环境负责内存分配,并且发布版本依赖于代码来执行此操作。我在我的书中的研究和网上的
并没有提供任何帮助。


你的代码没有做任何分配。

但它确实会产生不确定的行为。

见下文。 />
我将非常感谢我需要做些什么来正确分配内存资源以便代码正确编译以作为
..exe程序发布。

干杯,

Cam

代码如下:

#include< iostream>
#include< conio .H>


这是一个非标准的标题。请从这里发布的代码中省略这样的



使用命名空间std;

KeyboardInput(int * pkey); //收集输入
Add1Comp(int * presult); //执行添加



这些数组中的每一个都有八个元素。因此

每个下标的有效范围是从0到7。

int main(int * pkey)///////////// ///////////////////////////////// main()


如果你使用它们,main()的前两个参数必须

分别是''int''和''char **''。

// ////////////////////////////////////////////////// ///////
{

{KEYInput(key1); //调用其输出为key1的KeyboardInput
KeyboardInput(key2); //调用KeyboardInput,其输出为key2
Add1Comp(result); //调用Add1Comp,其输出结果为
}
while(1);
返回0;
}
int KeyboardInput(int * pkey)/ /////////////////////////// KeyboardInput()
//////////////// /////////////////////////////
{
重启:
int counter = 0;
cout<< " \\\
输入一个8位二进制数:" ;;
for(counter = 8; counter> 0; counter - )


''counter''从值8开始。

{
pkey [counter] =(getche() - 48);


元素八超出你的数组''key1''和

''key2'',其地址你已经传递给这个函数。

这给出了''undefined behavior''。


也不是''getche()''是一个非标准函数。

请在此处张贴的代码中省略。

}
cout<< " \ n";
for(counter = 8; counter> 0; counter - )
{
if(pkey [counter]!= 0&& pkey [ counter]!= 1)


再次。

{
cout<< \ n \ n您输入的是非二进制字符。请
重新启动\ n \ n" ;;
goto restart;


考虑使用循环而不是''转到''。

}
}
返回0;
}

int Add1Comp(int * presult)/////////////////////////// Add1Comp()
/////////////////////////////////////////////////
{
int latch = 0;
重启:
int counter,carry = 0;
for(counter = 1; counter< 9; counter ++)


你在这里遇到同样的问题。数组索引是从零开始的元素数量减去一个。


for(counter = 0; counter< 8; counter ++)


更多以下。

{
开关(key1 [counter] + key2 [counter] + carry)
{
案例0:// 0 + 0 + 0 = 0携带0
presult [counter] = 0;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
进行= 0;
cout<<携带;
休息;
情况1:// 0 + 0 + 1 xor 0 + 1 + 0 xor 1 + 0 + 0 = 1携带0
presult [counter] = 1;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
进行= 0;
cout<<携带;
休息;
情况2:// 1 + 1 + 0 xor 1 + 0 + 1 xor 0 + 1 + 1 = 0携带1
presult [counter] = 0;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
随身携带= 1;
cout<<携带;
休息;
案例3:// 1 + 1 + 1 = 1携带1
presult [counter] = 1;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
随身携带= 1;
cout<<携带;
休息;
}
if(counter == 8&& carry == 1)
goto addcarry;
if(counter == 8& ;& latch == 1&& carry == 1)
goto overflow;
}
cout<< " \ n \\ n \\ nResult of of ;;
for(counter = 8; counter> 0; counter - )
{
if(latch)
key1 [ counter] = carrykey1 [counter];
cout<< key1 [counter];
}
cout<< " +" ;;
for(counter = 8; counter> 0; counter - )
{
if(latch)
key2 [counter] = carrykey2 [counter];
cout<< key2 [counter];
}
cout<< " =" ;;
for(counter = 8; counter> 0; counter - )
{
cout<< presult [counter];
}
cout<< " \\\
\\\
----------------------------------------- \\ \\ n;
latch = 0;
返回0;

addcarry:
latch = 1;
cout<< \ n \ n添加最终进位:\ n" ;;
for(counter = 1; counter< 9; counter ++)
{
carrykey1 [counter] = key1 [counter];
key1 [counter] =结果[counter];
carrykey2 [counter] = key2 [counter];
key2 [counter] = 0;
presult [counter] ] = 0;
}
key2 [1] = 1;
goto restart;
溢出:
cout<< \ n添加导致溢出;
main(key1);


''main()''可能无法在C ++中递归调用。

}
Hi all,

The code below is a practical exercise and works well running in the debug
environment but fails when being compiled for a release build. I believe
this is because the debug environment takes care of memory allocation and
the release build relies on code to do this. My research in my books and on the web has not provided any great help.
Your code doesn''t do any allocations.
But it does produce undefined behavior.
See below.

I would be grateful for advice on what I need to do to correctly allocate
the memory resources for the code to compile correctly to release as an ..exe program.

Cheers,

Cam

Code follows:

#include <iostream>
#include <conio.h>
This is a nonstandard header. Please omit such
from code posted here.
using namespace std;

KeyboardInput(int *pkey); // Gathers input
Add1Comp(int *presult); // Carries out addition

int key1[8], carrykey1[8], key2[8], carrykey2[8], result[8];
Each of these arrays has eight elements. Therefore the
valid range of subscripts for each is from zero through seven.

int main (int *pkey) ////////////////////////////////////////////// main()
If you use them, main()''s first two arguments must
be type ''int'', and ''char **'', respectively.
///////////////////////////////////////////////////////////
{
do
{
KeyboardInput(key1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(key2); // Calls KeyboardInput whose output is key2
Add1Comp(result); // Calls Add1Comp whose output is result
}
while(1);
return 0;
}

int KeyboardInput (int *pkey) //////////////////////////// KeyboardInput()
/////////////////////////////////////////////
{
restart:
int counter = 0;
cout << "\nEnter an 8 bit binary number: ";
for (counter = 8; counter > 0; counter --)
''counter'' starts out with a value of 8.
{
pkey[counter] = (getche() - 48);
Element eight is out of bound for your arrays ''key1'' and
''key2'' above, whose addresses you''ve passed to this function.
This gives ''undefined behavior''.

Also not that ''getche()'' is a nonstandard function.
Please omit such from code posted here.
}
cout << "\n";
for (counter = 8; counter > 0; counter --)
{
if (pkey[counter] != 0 && pkey[counter] != 1)
And again.
{
cout << "\n\nYou have entered non-binary character. Please
restart\n\n";
goto restart;
Consider using a loop instead of a ''goto''.
}
}
return 0;
}

int Add1Comp (int *presult) /////////////////////////// Add1Comp()
/////////////////////////////////////////////////
{
int latch = 0;
restart:
int counter, carry = 0;
for (counter = 1; counter < 9; counter ++)
You have the same problems here. Array indices are
from zero the the number of elements less one.

for (counter = 0; counter < 8; counter ++)

More below.
{
switch (key1[counter] + key2[counter] + carry)
{
case 0: // 0+0+0 = 0 carry 0
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 1: // 0+0+1 xor 0+1+0 xor 1+0+0 = 1 carry 0
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 2: // 1+1+0 xor 1+0+1 xor 0+1+1 = 0 carry 1
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
case 3: // 1+1+1 = 1 carry 1
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
}
if (counter == 8 && carry == 1)
goto addcarry;
if (counter == 8 && latch == 1 && carry == 1)
goto overflow;
}
cout << "\n\nResult of ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key1[counter] = carrykey1[counter];
cout << key1[counter];
}
cout << " + ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key2[counter] = carrykey2[counter];
cout << key2[counter];
}
cout << " = ";
for (counter = 8; counter > 0; counter --)
{
cout << presult[counter];
}
cout << "\n\n-----------------------------------------\n";
latch = 0;
return 0;

addcarry:
latch = 1;
cout << "\n\nAdd final carry:\n";
for (counter = 1; counter < 9; counter ++)
{
carrykey1[counter] = key1[counter];
key1[counter] = result[counter];
carrykey2[counter] = key2[counter];
key2[counter] = 0;
presult[counter] = 0;
}
key2[1] = 1;
goto restart;
overflow:
cout << "\nThe addition has resulted in an overflow";
main(key1);
''main()'' may not be called recursively in C++.
}




你可能是正确的,你的实施'''调试

模式''''保护'你'免于崩溃。


-Mike



You''re probably correct that your implementation''s ''debug
mode'' was ''protecting'' you from a crash.

-Mike




" Cam" < retsigerymmudathotmaildotcom>在消息中写道

新闻:40 ****** @ duster.adelaide.on.net ...

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@duster.adelaide.on.net...
大家好,

下面的代码是一个实际练习,在调试环境中运行良好,但在为发布版本编译时失败。我相信这是因为调试环境负责内存分配,并且发布版本依赖于代码来执行此操作。我在我的书中的研究和网上的
并没有提供任何帮助。


你的代码没有做任何分配。

但它确实会产生不确定的行为。

见下文。 />
我将非常感谢我需要做些什么来正确分配内存资源以便代码正确编译以作为
..exe程序发布。

干杯,

Cam

代码如下:

#include< iostream>
#include< conio .H>


这是一个非标准的标题。请从这里发布的代码中省略这样的



使用命名空间std;

KeyboardInput(int * pkey); //收集输入
Add1Comp(int * presult); //执行添加



这些数组中的每一个都有八个元素。因此

每个下标的有效范围是从0到7。

int main(int * pkey)///////////// ///////////////////////////////// main()


如果你使用它们,main()的前两个参数必须

分别是''int''和''char **''。

// ////////////////////////////////////////////////// ///////
{

{KEYInput(key1); //调用其输出为key1的KeyboardInput
KeyboardInput(key2); //调用KeyboardInput,其输出为key2
Add1Comp(result); //调用Add1Comp,其输出结果为
}
while(1);
返回0;
}
int KeyboardInput(int * pkey)/ /////////////////////////// KeyboardInput()
//////////////// /////////////////////////////
{
重启:
int counter = 0;
cout<< " \\\
输入一个8位二进制数:" ;;
for(counter = 8; counter> 0; counter - )


''counter''从值8开始。

{
pkey [counter] =(getche() - 48);


元素八超出你的数组''key1''和

''key2'',其地址你已经传递给这个函数。

这给出了''undefined behavior''。


也不是''getche()''是一个非标准函数。

请在此处张贴的代码中省略。

}
cout<< " \ n";
for(counter = 8; counter> 0; counter - )
{
if(pkey [counter]!= 0&& pkey [ counter]!= 1)


再次。

{
cout<< \ n \ n您输入的是非二进制字符。请
重新启动\ n \ n" ;;
goto restart;


考虑使用循环而不是''转到''。

}
}
返回0;
}

int Add1Comp(int * presult)/////////////////////////// Add1Comp()
/////////////////////////////////////////////////
{
int latch = 0;
重启:
int counter,carry = 0;
for(counter = 1; counter< 9; counter ++)


你在这里遇到同样的问题。数组索引是从零开始的元素数量减去一个。


for(counter = 0; counter< 8; counter ++)


更多以下。

{
开关(key1 [counter] + key2 [counter] + carry)
{
案例0:// 0 + 0 + 0 = 0携带0
presult [counter] = 0;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
进行= 0;
cout<<携带;
休息;
情况1:// 0 + 0 + 1 xor 0 + 1 + 0 xor 1 + 0 + 0 = 1携带0
presult [counter] = 1;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
进行= 0;
cout<<携带;
休息;
情况2:// 1 + 1 + 0 xor 1 + 0 + 1 xor 0 + 1 + 1 = 0携带1
presult [counter] = 0;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
随身携带= 1;
cout<<携带;
休息;
案例3:// 1 + 1 + 1 = 1携带1
presult [counter] = 1;
cout<< \ nBit <<计数器<< : << key1 [counter]<< " + <<
key2 [counter]<< " + <<携带<< " = << presult [counter]<< "携带" ;;
随身携带= 1;
cout<<携带;
休息;
}
if(counter == 8&& carry == 1)
goto addcarry;
if(counter == 8& ;& latch == 1&& carry == 1)
goto overflow;
}
cout<< " \ n \\ n \\ nResult of of ;;
for(counter = 8; counter> 0; counter - )
{
if(latch)
key1 [ counter] = carrykey1 [counter];
cout<< key1 [counter];
}
cout<< " +" ;;
for(counter = 8; counter> 0; counter - )
{
if(latch)
key2 [counter] = carrykey2 [counter];
cout<< key2 [counter];
}
cout<< " =" ;;
for(counter = 8; counter> 0; counter - )
{
cout<< presult [counter];
}
cout<< " \\\
\\\
----------------------------------------- \\ \\ n;
latch = 0;
返回0;

addcarry:
latch = 1;
cout<< \ n \ n添加最终进位:\ n" ;;
for(counter = 1; counter< 9; counter ++)
{
carrykey1 [counter] = key1 [counter];
key1 [counter] =结果[counter];
carrykey2 [counter] = key2 [counter];
key2 [counter] = 0;
presult [counter] ] = 0;
}
key2 [1] = 1;
goto restart;
溢出:
cout<< \ n添加导致溢出;
main(key1);


''main()''可能无法在C ++中递归调用。

}
Hi all,

The code below is a practical exercise and works well running in the debug
environment but fails when being compiled for a release build. I believe
this is because the debug environment takes care of memory allocation and
the release build relies on code to do this. My research in my books and on the web has not provided any great help.
Your code doesn''t do any allocations.
But it does produce undefined behavior.
See below.

I would be grateful for advice on what I need to do to correctly allocate
the memory resources for the code to compile correctly to release as an ..exe program.

Cheers,

Cam

Code follows:

#include <iostream>
#include <conio.h>
This is a nonstandard header. Please omit such
from code posted here.
using namespace std;

KeyboardInput(int *pkey); // Gathers input
Add1Comp(int *presult); // Carries out addition

int key1[8], carrykey1[8], key2[8], carrykey2[8], result[8];
Each of these arrays has eight elements. Therefore the
valid range of subscripts for each is from zero through seven.

int main (int *pkey) ////////////////////////////////////////////// main()
If you use them, main()''s first two arguments must
be type ''int'', and ''char **'', respectively.
///////////////////////////////////////////////////////////
{
do
{
KeyboardInput(key1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(key2); // Calls KeyboardInput whose output is key2
Add1Comp(result); // Calls Add1Comp whose output is result
}
while(1);
return 0;
}

int KeyboardInput (int *pkey) //////////////////////////// KeyboardInput()
/////////////////////////////////////////////
{
restart:
int counter = 0;
cout << "\nEnter an 8 bit binary number: ";
for (counter = 8; counter > 0; counter --)
''counter'' starts out with a value of 8.
{
pkey[counter] = (getche() - 48);
Element eight is out of bound for your arrays ''key1'' and
''key2'' above, whose addresses you''ve passed to this function.
This gives ''undefined behavior''.

Also not that ''getche()'' is a nonstandard function.
Please omit such from code posted here.
}
cout << "\n";
for (counter = 8; counter > 0; counter --)
{
if (pkey[counter] != 0 && pkey[counter] != 1)
And again.
{
cout << "\n\nYou have entered non-binary character. Please
restart\n\n";
goto restart;
Consider using a loop instead of a ''goto''.
}
}
return 0;
}

int Add1Comp (int *presult) /////////////////////////// Add1Comp()
/////////////////////////////////////////////////
{
int latch = 0;
restart:
int counter, carry = 0;
for (counter = 1; counter < 9; counter ++)
You have the same problems here. Array indices are
from zero the the number of elements less one.

for (counter = 0; counter < 8; counter ++)

More below.
{
switch (key1[counter] + key2[counter] + carry)
{
case 0: // 0+0+0 = 0 carry 0
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 1: // 0+0+1 xor 0+1+0 xor 1+0+0 = 1 carry 0
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 0;
cout << carry;
break;
case 2: // 1+1+0 xor 1+0+1 xor 0+1+1 = 0 carry 1
presult[counter] = 0;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
case 3: // 1+1+1 = 1 carry 1
presult[counter] = 1;
cout << "\nBit " << counter << ": " << key1[counter] << " + " <<
key2[counter] << " + " << carry << " = " << presult[counter] << " carry ";
carry = 1;
cout << carry;
break;
}
if (counter == 8 && carry == 1)
goto addcarry;
if (counter == 8 && latch == 1 && carry == 1)
goto overflow;
}
cout << "\n\nResult of ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key1[counter] = carrykey1[counter];
cout << key1[counter];
}
cout << " + ";
for (counter = 8; counter > 0; counter --)
{
if (latch)
key2[counter] = carrykey2[counter];
cout << key2[counter];
}
cout << " = ";
for (counter = 8; counter > 0; counter --)
{
cout << presult[counter];
}
cout << "\n\n-----------------------------------------\n";
latch = 0;
return 0;

addcarry:
latch = 1;
cout << "\n\nAdd final carry:\n";
for (counter = 1; counter < 9; counter ++)
{
carrykey1[counter] = key1[counter];
key1[counter] = result[counter];
carrykey2[counter] = key2[counter];
key2[counter] = 0;
presult[counter] = 0;
}
key2[1] = 1;
goto restart;
overflow:
cout << "\nThe addition has resulted in an overflow";
main(key1);
''main()'' may not be called recursively in C++.
}




你可能是正确的,你的实施'''调试

模式''''保护'你'免于崩溃。


-Mike



You''re probably correct that your implementation''s ''debug
mode'' was ''protecting'' you from a crash.

-Mike


" Cam" < retsigerymmudathotmaildotcom>写了...
"Cam" <retsigerymmudathotmaildotcom> wrote...
下面的代码是一个实际的练习,效果很好[...]
The code below is a practical exercise and works well [...]




这并不奇怪。您的代码中充满了导致

未定义行为的部分。可能的行为之一是以预期的方式工作。另一个是不按预期工作。您显然已经见证了两个,一个在发布模式下调试另一个。他们确实没有特别的那样。
。他们可能反之亦然

或两者都是相同的。这是不可能预测的,因为它只是简单地

_undefined_。


一旦你修复它以符合C ++标准,你可能会

感到惊讶它始终有效...


具体来说,看一下你用来访问你的指数

8元素数组。想想在这种情况下正确的范围和

验证你的指数总是落在那个范围内...


祝你好运!


V



Which is not surprising. Your code is full of parts that cause
undefined behaviour. One of possible behaviours is "to work as
expected". Another is "not to work as expected". You apparently
witnessed both, one in debug the other in release mode. They did
not have to be particularly that. They could have been vice versa
or both the same. It''s impossible to predict because it''s simply
_undefined_.

As soon as you fix it to comply with C++ Standard, you might be
surprised that it works always...

To be specific, check out what indices you used to access your
8-element arrays. Think of the correct range in that case and
verify that your indices always fall in that range...

Good luck!

V


这篇关于使用指针的程序发布版本的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆