Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Exercise

Name: Anonymous 2008-02-13 11:24

Hi.

I'm attending a C++ programming course. The teacher gave us some exercises, in which we must find what's wrong with the code.
The problem is I'm completly stuck in one of them. I tried to create and compile the following code:
#include<iostream>

using namespace std;

int my_sum(int a, int b)
{
    int c;
    c=a+b;
}

void main()
{
    cout << "2 + 3 = " << my_sum(2,3) << endl;
}


The problem is the compiler gives no errors and the program runs without any errors either.
Could someone PLEASE help me?
Thx in advance.

Name: Anonymous 2008-02-13 11:27

my_sym does not return anything

Name: Anonymous 2008-02-13 11:27

...

Name: Anonymous 2008-02-13 11:28

DONT HELP HIM

Name: Anonymous 2008-02-13 11:30

Hi.

I'm attending a trolling course. The teacher gave us some exercises, in which we must find what's wrong with the troll.
The problem is I'm completly stuck in one of them. I tried to create and compile the following troll:


#include<iostream>

using namespace std;

int my_sum(int a, int b)
{
    int c;
    c=a+b;
}

void main()
{
    cout << "2 + 3 = " << my_sum(2,3) << endl;
}


The problem is the compiler gives no errors and the troll runs without any errors either.
Could someone PLEASE help me?
Thx in advance.

Name: Anonymous 2008-02-13 11:33

You must rewrite my_sum in the ENTERPRISE READY way, fully object oriented client-server 100% reliable architecture designed according to the latest standards.

Name: Anonymous 2008-02-13 11:35

>>2
How yeah, I really am stupid. :P I didn't realise it was so simple.
But how come this code compiles and runs without problems?

Name: Anonymous 2008-02-13 11:35

The solution is easy, it's in a book called Structure and Interpretation of Computer Programs, check it out

Name: Anonymous 2008-02-13 11:36

>>7
Because you suck at programming.

Name: Anonymous 2008-02-13 11:37

>>7
Because your CFLAGS don't have the necessary -Warnings required

Name: Anonymous 2008-02-13 11:39

This is all true, but... how the fuck does it knows what to return???

Name: Anonymous 2008-02-13 11:43

>>1
The variable c is defined locally in the function my_sum, so you can't use c in the function main which is what you're trying to do. The solution is to define c as a global variable, like this:
#include<iostream>

using namespace std;

int c;

int my_sum(int a, int b)
{
    c=a+b;
}

void main()
{
    my_sum(2, 3);
    cout << "2 + 3 = " << c << endl;
}

Name: Anonymous 2008-02-13 11:45

>>12
MT-SAFE

Name: Anonymous 2008-02-13 11:58

>>12
ENTERPRISE-READY ADVICE

I'd take it one step more, so you can be a true ENTERPRISE PROGRAMMER:

Change my_sum() to write an XML document to disk with the resulting value. Then parse it in main() for the result.


<calculation>
  <sum>
   <input type="integer">2</input>
   <input type="integer">3</input>
   <result type="integer">5</result>
  </sum>
</calculation>


This way, you can simply generate a DOM tree and parse it in a simple manner - namely, see which sum contains matching inputs per your function, and return the result.

Name: Anonymous 2008-02-13 12:00

I made some changes to my code, so that it looks like this (still no return).

#include<iostream>

using namespace std;

int my_sum(int a, int b){
    int c;
    c=a+b;
}


int main(){
    cout << "1 + 3 = " << my_sum(1,3) << endl;
    cout << "2 + 3 = " << my_sum(2,3) << endl;
    cout << "3 + 3 = " << my_sum(3,3) << endl;
    cout << "4 + 3 = " << my_sum(4,3) << endl;   
   
    return 0;
}


and this is what happened:
$ g++ teste.cpp
$ ./a.out
1 + 3 = 4
2 + 3 = 5
3 + 3 = 6
4 + 3 = 7


So, does this proves that no return is required?

Name: Anonymous 2008-02-13 12:16

>>15
NOT POSSIBLE.

Name: Anonymous 2008-02-13 12:18

I had a firecracker and I did this:

1. Put it in my hand
2. Lit it
3. Didn't throw it


and this is what happened:

1. It didn't explode, probably because of a fabrication error.
2. My hand was not torn off


So, does this prove that throwing the firecracker is not required?

Name: Anonymous 2008-02-13 12:21

No, that proves you're a faggot

Name: Anonymous 2008-02-13 12:25

Is this a g++ bug, or is it a c++ feature?
It looks to me that an error should have appeard.

Name: Anonymous 2008-02-13 12:35

It's a nonstandard thing called "default return", i.e. if you don't return anything the value that was in the register is used, and that register (eax on x86) just happened to contain the result of the last calculation. Don't count on it.

Name: Anonymous 2008-02-13 12:40

Welcome to the wonderful world of undefined behaviour.

Name: Anonymous 2008-02-13 12:45

>>20
You right!
I've replaced the code in the function by:
int c;
int d=7;
c=a+b;
int e=1+d;

and now it gives me always 8.
This is both strange and dangerous behaviour.
I'll have to be more carefull. Thx for the tip.

Name: Anonymous 2008-02-13 12:55

What an interesing thread.

Name: Donald Knut 2008-02-13 13:10

>>1

Hrmm..

Seems you need more processing power. I suggest you create more threads.

Name: Anonymous 2008-02-13 14:33

Damn, my g++ just blows up.
$ make b
g++     b.cc   -o b
b.cc:11: error: '::main' must return 'int'
make: *** [b] Error 1

Name: Anonymous 2008-02-13 14:35

>>25
Downgrade to 2.95. That'll work.

Name: Anonymous 2008-02-13 14:52

>>26
Like with GHC.

Name: Anonymous 2008-02-13 19:04

-O3 and it'll run fine

Name: Anonymous 2008-02-13 19:23

Holy fucking niggers, 28 replies and no-one mentioned that MAIN MUST BE AN INT.

Name: Anonymous 2008-02-13 19:24

>>28
I never knew what was -O3.

Name: Anonymous 2008-02-13 19:59

>>29
What? It doesn't have to be an int

Name: Anonymous 2008-02-13 20:50

>>31
According to the standard, main must return an int.

Name: Anonymous 2008-02-13 21:06

>>15

Yeah, cause whatever's left in the %eax register is what your function returns. - That is, on an IA-32 architecture. Otherwise would be some kind of other variable, i.e. if it was Sparc, it would be l7 or something since those have liek double the amount that IA-32 has. So the compiler is def using that register for doing the math and the result is simply getting left behind.

Name: Anonymous 2008-02-14 2:25

>>30
Sorry, you are not allowed to read the O3 docs until you have achieved O2 Satori and paid your fees to the Church of Expert Programmers.

Name: Anonymous 2008-02-14 3:34

you did not return c.

Name: Trollbot9000 2009-07-01 10:59


Namespace std int my sum int a  70 NEXT I  had no idea  about optimizations sigh.

Don't change these.
Name: Email:
Entire Thread Thread List