473,769 Members | 5,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i nt *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(k ey1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(k ey2); // 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 1430

"Cam" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.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(i nt *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(k ey1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(k ey2); // 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" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.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(i nt *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(k ey1); // Calls KeyboardInput whose ouput is key1
KeyboardInput(k ey2); // 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" <retsigerymmuda thotmaildotcom> 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" <retsigerymmuda thotmaildotcom> 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" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.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" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.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" <retsigerymmuda thotmaildotcom> wrote in message
| news:40******@d uster.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" <retsigerymmuda thotmaildotcom> wrote in message
| news:40******@d uster.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" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.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

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

Similar topics

10
1135
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 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...
5
2345
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 run this works fine. but if build in unicode release or release this does't work. IS THERE ANY PROBLEM OF INSTANTIATING TEMPLATE CLASSES
0
1943
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 and added it to my solution (selecting the "Primary Output" as the files to include from my project). However, when I attempt to build the setup project (assuming a clean output directory), I get "Unrecoverable build error" - and NO other...
1
3583
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 to port into C++ (.Net) and ran into errors wich i have no clue whats this is about. After compiling the linker is throwing those errors :
2
1692
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 OnInitDialog calls this // parent is Tab Control, grandparent is Tab Vie m_pTabViewWnd = (CTabVw *)GetGrandParent() //this is the function CWnd* CSmsPropPage::GetGrandParent(
1
1590
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 Basic Express edition when I build my project it is creating two directories inside bin, debug and release. So far I come to know that in the release mode all the debugging information is removed
4
6254
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 version to build successfully. Unfortunately, it crashes right away when I start it. So now I need to build the debug version. Unfortunately, the compiler gives innumerable error messages of the sort shown below. The #include file referenced...
0
2120
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 nant file which is causing the problem, the object code that is not being built and the error message which is being produced. The weird thing is that VS.net builds without a problem. And the intellisense within the object WorkQueue knows that...
4
2534
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 error LNK1141: failure during build of exports file error: command '"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe"' failed with exit status 1141.What shoud I
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.