473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem with release build of program using pointers

Cam
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);
}
Jul 22 '05 #1
10 1109

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@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
Jul 22 '05 #2

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@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
Jul 22 '05 #3
"Cam" <retsigerymmudathotmaildotcom> wrote...
The code below is a practical exercise and works well [...]


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
Jul 22 '05 #4
"Cam" <retsigerymmudathotmaildotcom> wrote...
The code below is a practical exercise and works well [...]


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
Jul 22 '05 #5

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@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.


That's untrue.

john
Jul 22 '05 #6

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@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.


That's untrue.

john
Jul 22 '05 #7
Cam
Hi John,

Thank you for your help previously.

The fact that my arrays start with an element at position 1 doesn't seem to
cause problems in the debug environment.

Is this is what is preventing the code from undergoing a release build?

Thanks,

Cam
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...
|
| "Cam" <retsigerymmudathotmaildotcom> wrote in message
| news:40******@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.
|
| That's untrue.
|
| john
|
|
Jul 22 '05 #8
Cam
Hi John,

Thank you for your help previously.

The fact that my arrays start with an element at position 1 doesn't seem to
cause problems in the debug environment.

Is this is what is preventing the code from undergoing a release build?

Thanks,

Cam
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c5*************@ID-196037.news.uni-berlin.de...
|
| "Cam" <retsigerymmudathotmaildotcom> wrote in message
| news:40******@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.
|
| That's untrue.
|
| john
|
|
Jul 22 '05 #9

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@duster.adelaide.on.net...
Hi John,

Thank you for your help previously.

The fact that my arrays start with an element at position 1 doesn't seem to cause problems in the debug environment.

Is this is what is preventing the code from undergoing a release build?


Yes, arrays start at position 0 in C++, that applies to debug builds,
release builds, different compilers, everything.

As others explained you program has undefined behaviour. And undefined
behaviour means anything can happen, including working in the debug build
and not working in the release build.

If you change your program so that it does not have undefined behaviour,
then it will work in any build.

john
Jul 22 '05 #10

"Cam" <retsigerymmudathotmaildotcom> wrote in message
news:40******@duster.adelaide.on.net...
Hi John,

Thank you for your help previously.

The fact that my arrays start with an element at position 1 doesn't seem to cause problems in the debug environment.

Is this is what is preventing the code from undergoing a release build?


Yes, arrays start at position 0 in C++, that applies to debug builds,
release builds, different compilers, everything.

As others explained you program has undefined behaviour. And undefined
behaviour means anything can happen, including working in the debug build
and not working in the release build.

If you change your program so that it does not have undefined behaviour,
then it will work in any build.

john
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Cam | last post by:
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...
15
by: Ken Allen | last post by:
I have been developing a suite of assemblies over the past couple of weeks, and this afternoon somethign started misbehaving. If I do not run the IDE and compiler the code from the command line,...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
0
by: Adam Clauss | last post by:
I have a C++ COM Addin I wrote for Outlook (2002) using VS.Net 2002. Having completed it, I am now looking to create a setup project and install it on another machine. I created a setup project...
1
by: Sebastian Sosna | last post by:
Hello NG ! Iam developing in C# so my cpp knowledge is limited. Ive downloaded a C++ sample wich is doing some AD Job. U can get it here : http://support.microsoft.com/?id=302515 Ive tried...
2
by: Jerry | last post by:
I have an SDI document that runs fine in Debug Mode but crashes in Release Mode. I have set up my project so I can debug in Release Mode. The project has a tab view Here is what is happening ...
1
by: c_shah | last post by:
I am a SQL DBA just started to learn Visual Basic 2005 so I apologize for this very simple question. What's the fundamental difference between debug mode vs release mode. I am using Visual...
4
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release...
0
by: james.mcdonagh | last post by:
Hi I am a newbie using nAnt for .net 2.0. As such I have not come across this bug before, and I would be happy of any help that you may be able to provide. In order to help I have included the...
4
by: vedrandekovic | last post by:
Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.