PDA

View Full Version : C++ - 1120 errors when using "Release"

TheLutariFan
Dec 1, 2011, 03:23 AM
Hey everybody, Iīm using Xcode with c++.

Iīve been trying to make a calculator, which works fine when I set it on "Debug".

Though when I set it on "Release", it gives me 1120 errors in "NSObjRuntime.h".

And this is my code:
int g_anArray[0];
int g_nArraySize;
#include <iostream>
int main()
{
int nOperator;

using namespace std;
cout << "This is a calculator capable of adding, subtracting, multiplying and dividing. It has a limit of 6 digits, which means answers over 6 digits will be displayed as an error(except dividing, which will result in the answer being rounded up at 6 digits)." << endl;

cout << "Enter an operator(1 for +, 2 for -, 3 for * and 4 for /) and press enter." << endl;
cout << "Or enter 5 if you want to add many numbers together.";

cin >> nOperator;
if (nOperator ==1) {
double x;
double y;
cout << "Enter a number, press enter, then enter a second number. These numbers will be added." ;
cin >> x;
cin >> y;
cout << "The answer is " << x + y << endl;

}
else if (nOperator ==2) {
double a;
double b;
cout << "Enter a number, press enter, then enter a second number. These numbers will be subtracted." ;
cin >> a;
cin >> b;
cout << "The answer is " << a - b << endl;
nSecondLoop = 1;
}
else if (nOperator ==3) {
double c;
double d;
cout << "Enter a number, press enter, then enter a second number. These numbers will be multiplied." ;
cin >> c;
cin >> d;
cout << "The answer is " << c * d << endl;

}
else if (nOperator ==4) {

float e;
float f;

cout << "Enter a number, press enter, then enter a second number. These numbers will be divided." ;
cin >> e;
cin >> f;
if (f == 0) {
cout << "Dividing by zero? Really...";

}
else {
cout << "The answer is " << e / f << endl;

}
else if (nOperator ==5) {

cout << "Enter how many numbers you want to add together. (Max 6)" << endl;
cin >> g_nArraySize;
#define ARRAY_SIZE g_nArraySize
#define int g_anArray[ARRAY_SIZE];
if (g_nArraySize <2) {
cout << "You canīt enter less than 2 numbers!" << endl;

}
else if (g_nArraySize > 6) {
cout << "No more than 10 numbers are accepted" << endl;

}

else if (g_nArraySize == 2) {
cout << "Enter the numbers. (Press enter between numbers)" << endl;
cin >> g_anArray[1];
cin >> g_anArray[2];
cout << "The answer is " << g_anArray[1] + g_anArray[2] << endl;

}
else if (g_nArraySize == 3) {
cout << "Enter the numbers. (Press enter between numbers)" << endl;

cin >> g_anArray[1];
cin >> g_anArray[2];
cin >> g_anArray[3];
cout << "The answer is " << g_anArray[1] + g_anArray[2] + g_anArray[3] << endl;

}
else if (g_nArraySize == 4) {
cout << "Enter the numbers. (Press enter between numbers)" << endl;
cin >> g_anArray[1];
cin >> g_anArray[2];
cin >> g_anArray[3];
cin >> g_anArray[4];
cout << "The answer is " << g_anArray[1] + g_anArray[2] + g_anArray[3] + g_anArray[4] << endl;

}
else if (g_nArraySize == 5) {
cout << "Enter the numbers. (Press enter between numbers)" << endl;
cin >> g_anArray[1];
cin >> g_anArray[2];
cin >> g_anArray[3];
cin >> g_anArray[4];
cin >> g_anArray[5];
cout << "The answer is " << g_anArray[1] + g_anArray[2] + g_anArray[3] + g_anArray[4]+ g_anArray[5] << endl;

}
else if (g_nArraySize == 6) {
cout << "Enter the numbers. (Press enter between numbers)" << endl;
cin >> g_anArray[1];
cin >> g_anArray[2];
cin >> g_anArray[3];
cin >> g_anArray[4];
cin >> g_anArray[5];
cin >> g_anArray[6];
cout << "The answer is " << g_anArray[1] + g_anArray[2] + g_anArray[3] + g_anArray[4]+ g_anArray[5] + g_anArray[6] << endl;

}
else {
cout << "Really, what DID you enter?";

}

else {
cout << "You entered an invalid operator." << endl;

}

return 0;
}

gnasher729
Dec 1, 2011, 03:50 AM
Hey everybody, Iīm using Xcode with c++.

Iīve been trying to make a calculator, which works fine when I set it on "Debug".

Though when I set it on "Release", it gives me 1120 errors in "NSObjRuntime.h".

And this is my code:
int g_anArray[0];

That's not a very big array, is it?

And please read very carefully what #define does, because the #define in your code is utter nonsense.

TheLutariFan
Dec 1, 2011, 04:00 AM
You know what, Iīll just remove those while loops, it worked on Debug (still not release) before I added those.

----------

Ok, this code works perfectly in Debug.

I donīt think the current problem is with the #define, because i had those 1120 errors in Debug previously and fixed them.

gnasher729
Dec 1, 2011, 04:17 AM
You know what, Iīll just remove those while loops, it worked on Debug (still not release) before I added those.

----------

Ok, this code works perfectly in Debug.

I donīt think the current problem is with the #define, because i had those 1120 errors in Debug previously and fixed them.

If that is your solution to the problem, then you should quit programming. Seriously. Think about what you are doing here: I told you about one serious bug. In the first line of your code. Then I told you about two lines that show a serious misunderstanding of programming fundamentals. If these two lines had affected what the code does, I would have told you.

Instead of understanding and fixing the problem, you make random changes in completely unrelated areas.

TheLutariFan
Dec 1, 2011, 04:21 AM
Ok, I know, i should fix that, but now the problem is about the 1120 errors.

Iīve tried compiling this without the define and the array, and it turns out the problem is not there.

I fixed this on Debug by removing the definition of some precompiled header or whatever, fixed it. Now Iīm having the exact same problem.

And also i should tell you that I am kinda new to programming, thatīs why my code isnīt the best :(

gnasher729
Dec 1, 2011, 04:45 AM
Ok, I know, i should fix that, but now the problem is about the 1120 errors.

Iīve tried compiling this without the define and the array, and it turns out the problem is not there.

I fixed this on Debug by removing the definition of some precompiled header or whatever, fixed it. Now Iīm having the exact same problem.

And also i should tell you that I am kinda new to programming, thatīs why my code isnīt the best :(

Please answer this question: How many numbers can you store into the array g_anArray before things go wrong?

chrono1081
Dec 1, 2011, 04:51 AM
That array is an error waiting to happen. Why define an array of size 0?

I have no clue what your programming experience is but the fact you have all of those if/else statements verses a switch statement suggests you may be a little over your head.

Please let us know what type of things you know in C++ (if/thens, loops, switch statements, classes, etc) and maybe we can help more.

KnightWRX
Dec 1, 2011, 06:10 AM
If you want us to help with the 1120 errors, then please at least tell us what the error messages are.

And that code doesn't work fine in debug mode. That it doesn't just crash is a miracle in and of itself.

subsonix
Dec 1, 2011, 07:23 AM
Ok, I know, i should fix that, but now the problem is about the 1120 errors.

Usually, the answer can be found in the first few lines, where the real issue is. The rest is oftentimes the result of a cascading effect and dependencies.

Dec 1, 2011, 05:09 PM
Wirelessly posted (Mozilla/5.0 (iPod; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3)

First of all, C++ code should not be including NSObjcRuntime.h; that's an Objective C header. Make sure you aren't precompiling any headers (when adding a build target, choosing a Cocoa program or a Foundation tool will set the prefix header to either <Cocoa/Cocoa.h> or <Foundation/Foundation.h>). If it is using a prefix header, clear the value.

Damo01
Dec 1, 2011, 09:09 PM
This is a console application. you declaring an integer array to zero outside the main function but you haven't actually initialising it and I don't quite understand what you are trying to do with the array. Also you don't need #define to declare an array.

Remove or comment out the if statements that has anything do with the array of your code should get rid of the errors..

You need to have look at some tutorial on arrays here (http://www.cplusplus.com/doc/tutorial/arrays/)and learn how to use them correctly.

subsonix
Dec 2, 2011, 02:48 AM
This is a console application. you declaring an integer array to zero outside the main function but you haven't actually initialising it and I don't quite understand what you are trying to do with the array. Also you don't need #define to declare an array.

Yes, but that is not a compile error. The over 1k errors here is the result of something else. Most likely related to the NSObjRuntime.h remark and mixing up Obj-C and C++.

gnasher729
Dec 2, 2011, 06:40 AM
Yes, but that is not a compile error. The over 1k errors here is the result of something else. Most likely related to the NSObjRuntime.h remark and mixing up Obj-C and C++.

Once you have a bug like that and overwrite memory, anything can happen. That's why instead of making random changes and hoping that they help, you look for the first actual bug that you find, and you fix it.

subsonix
Dec 2, 2011, 07:47 AM
Once you have a bug like that and overwrite memory, anything can happen. That's why instead of making random changes and hoping that they help, you look for the first actual bug that you find, and you fix it.

It requires an executable binary, which in turn requires a successful compilation. Look, I'm neither suggesting that he should not fix this, nor that he should make random changes. I just pointed out that it's not related to his error message or going to fix his current problem.

TheLutariFan
Dec 2, 2011, 08:25 PM
Thanks for all the help,

Yes I know that my array declaring is weird, tried out other stuff previously, didnt work, well at least this works now ^^ weirdly

KnightWRX
Dec 2, 2011, 08:52 PM
Thanks for all the help,

Yes I know that my array declaring is weird, tried out other stuff previously, didnt work, well at least this works now ^^ weirdly

No, it doesn't work. That it produces a result you expect is just sheer luck. You're adressing unallocated memory and that usually results in undefined behavior. Undefined behavior can be many things :

- The OS kills your process for misbehaving
- Everything works as you expect until the day where it doesn't anymore
- Bats fly out of your USB ports.

You really need to fix your memory allocation in your code. It's seriously that broken. #define does not and cannot change the size of your array. You need to look into pointers, new, delete and other memory allocation paradigms for this code to work like you expect it to.

And finally, what is the error you're getting ? You still haven't replied...

chrono1081
Dec 3, 2011, 01:59 AM
No, it doesn't work. That it produces a result you expect is just sheer luck. You're adressing unallocated memory and that usually results in undefined behavior. Undefined behavior can be many things :

- The OS kills your process for misbehaving
- Everything works as you expect until the day where it doesn't anymore
- Bats fly out of your USB ports.

You really need to fix your memory allocation in your code. It's seriously that broken. #define does not and cannot change the size of your array. You need to look into pointers, new, delete and other memory allocation paradigms for this code to work like you expect it to.

And finally, what is the error you're getting ? You still haven't replied...

I loled at this :D

KnightWRX is right though, if you don't understand something post it and we can help. You never want to get a program working out of sheer luck, because you will be completely frustrated with programming and never want to do it again.

Everything you learn builds off of each other and by the looks of it you do not have the basics down.

As long as you have that zero array size, your code is broken. If you post the errors you are getting we can help show you how to fix it.

Mac_Max
Dec 3, 2011, 02:21 AM
Thanks for all the help,

Yes I know that my array declaring is weird, tried out other stuff previously, didnt work, well at least this works now ^^ weirdly

Yes very weird. As in you're lucking out that the C++ spec is ambiguous on many things and allows the hacks you're pulling. There's a quote by the creator of C++ about his language that I think fits here: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."

Your code is in the whole leg category.

int g_anArray[0];
int g_nArraySize;

...

cin >> g_nArraySize;
#define ARRAY_SIZE g_nArraySize
#define int g_anArray[ARRAY_SIZE];

Right there. The first line of code you wrote allocates a null pointer on the stack essentially. The second line is not a problem yet but it will be. There is no specified value of an uninitialized int in C++. Sometimes debuggers will set it to a specific value but otherwise it's still a garbage value. It can be negative (a big no no for array size) for example. It looks like XCode or GCC sets the value of that variable to zero which is very polite of it. I believe Visual Studio on the PC sets it to –2147483648.

Using cin to get the size of the array is fine, but the fifth line is nonsense. #defines do simple text substitution during compilation. They don't exist at runtime. You assigned no value to g_nArraySize so the compiler assigns (automagically) 0 to ARRAY_SIZE. The last line must get ignored by the preprocessor completely because if it did, every int after that point wouldn't work anymore. #define int g_anArray[ARRAY_SIZE]; if processed would tell the preprocessor to copy g_anArray[ARRAY_SIZE]; and paste it over int. #defines don't belong anywhere in your code anyway.

Not sure if anyone else did this but I copy/pasted your code into XCode. First off, what you gave won't compile. Second once I fixed it (so it would compile) I wasn't able to reproduce your errors. This actually scares me because I probably overwrote something important in memory. You really do need to pick up a book or take some classes.

Can you tell the difference between what you wrote and something like this:

cin >> g_nArraySize;
int* g_anArray = new int[g_nArraySize];

Your code takes an uninitialized pointer and writes data to it and one to five memory addresses after it. This is not good at all. The code snippet there is an example of how you properly create a dynamically sized array.

Edit:

Poking around, it looks like when writing to his array, the values you enter overwrite other variables that sit on the stack. Ironically, that includes g_nArraySize, which is set to whatever your first number is. This is an example of shooting yourself.

subsonix
Dec 3, 2011, 03:20 AM
There is no specified value of an uninitialized int in C++.

Except in this case it's a global variable, so it will be initialized to zero both in C and C++.

BTW, if you manage to run the program without a segfault or buserror, meaning you overwrote some legal address in the current process address space it's bad news for the functionality of that process but any attempts to overwrite memory in other segments or address space outside the process will end with a segfault or buserror.

Mac_Max
Dec 3, 2011, 12:32 PM
Except in this case it's a global variable, so it will be initialized to zero both in C and C++.

BTW, if you manage to run the program without a segfault or buserror, meaning you overwrote some legal address in the current process address space it's bad news for the functionality of that process but any attempts to overwrite memory in other segments or address space outside the process will end with a segfault or buserror.

Fun to know abut the default initialization.

Yeah like I said, poking around with the debugger it seems to be over writing the other ints declared earlier. Still just bad bad bad.