问题描述
大家好,
以下代码是一个实际练习,在调试
环境中运行良好但在编译发布版本时失败。我相信
这是因为调试环境负责内存分配和发布版本依赖代码执行此操作。我在我的书籍和网站上的研究没有提供任何帮助。
如果我需要做正确的建议我将不胜感激分配
代码的内存资源,以便正确编译以作为.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);
}
推荐答案
你可能是正确的,你的实施'''调试
模式''''保护'你'免于崩溃。
-Mike
You''re probably correct that your implementation''s ''debug
mode'' was ''protecting'' you from a crash.
-Mike
你可能是正确的,你的实施'''调试
模式''''保护'你'免于崩溃。
-Mike
You''re probably correct that your implementation''s ''debug
mode'' was ''protecting'' you from a crash.
-Mike
这并不奇怪。您的代码中充满了导致
未定义行为的部分。可能的行为之一是以预期的方式工作。另一个是不按预期工作。您显然已经见证了两个,一个在发布模式下调试另一个。他们确实没有特别的那样。
。他们可能反之亦然
或两者都是相同的。这是不可能预测的,因为它只是简单地
_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
这篇关于使用指针的程序发布版本的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!