Re: Problem with release build of program using pointers
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 04/09/04
- Next message: Steven T. Hatton: "Re: High performance alternative to MI of virtual bases"
- Previous message: Leor Zolman: "Re: access to private members of internal class"
- In reply to: Cam: "Problem with release build of program using pointers"
- Next in thread: Victor Bazarov: "Re: Problem with release build of program using pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 09 Apr 2004 03:03:19 GMT
"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:407607cf@duster.adelaide.on.net...
> 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++.
> }
You're probably correct that your implementation's 'debug
mode' was 'protecting' you from a crash.
-Mike
- Next message: Steven T. Hatton: "Re: High performance alternative to MI of virtual bases"
- Previous message: Leor Zolman: "Re: access to private members of internal class"
- In reply to: Cam: "Problem with release build of program using pointers"
- Next in thread: Victor Bazarov: "Re: Problem with release build of program using pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|