Sign in to follow this  
Pharazon

How well you know your c++?

Recommended Posts

Lets see if everyone here at the GDNET can figure out why these c++ questions return the way they do: Try answering the following questions: Why does the following code segment display "equal"? short x = -32768; short y = -x; if (x == y) cout << "equal"; Why does the following code segment display "x does not equal 0.01, y equals 0.05"? float x = 0.01; float y = 0.05; if (x != 0.01) cout << "x does not equal 0.01, "; if (y == 0.05) cout << "y equals 0.05"; For integer values of x, (i) When does (x == x == x) evaluate to true? (ii) When does (x != x != x) evaluate to true? (iii) Why is it that (4 >= 3 >= 2) evaluates to false but (2 >= 1 >= 0) evaluates to true? Post your answers. If you don't know, I'll put up answers in a couple days.

Share this post


Link to post
Share on other sites
Off the top of my head

1) overflow on negation
2) 0.01 cannot be represented exactly in binary
3) x=1, never (NaN if x were a float), left-associativity and bool to int promotion rules.

Share this post


Link to post
Share on other sites
1. What does this do innocent code do:


#include <iostream>

void foobar() {
std::cout << "HELLO\n";
}

int main() {

typedef void (&foo)(); //<--- what do you think this is? hint: no its not a pointer to function.

typedef const foo const_foo;

const_foo f = foobar;

f();

return 0;

}


2. Why can't we have this for type members but only for functions? i would love to know the answer to that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
1) Function reference type. f() should call foobar()


Hooray, yes a reference to a function, give this man a gold star. [smile]

Quote:
Original post by Fruny
2) Do you mean member typedefs or member functions?


i mean't instead of pointer to member functions or pointer to data members, why no reference to member functions or reference to data members but you can with free/class functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
i mean't instead of pointer to member functions or pointer to data members, why no reference to member functions or reference to data members but you can with free/class functions.


Hmmm. Good question. I didn't even know you could not.

Share this post


Link to post
Share on other sites
Off the top of my head, please correct any mistakes.

1) -32768 as a short in binary is
1000000000000000
and negating that is the same as the 2's complement + 1 so we get
0111111111111111 + 1
1000000000000000
2) you're comparing float's to doubles. 0.01 is uses some of the added accuracy of a double, while 0.05 doesn't.

3i) x == 1
3ii) when x != 0
3ii) order of operations and comparisons return 0 or 1, so
4 >= 3 >= 2 becomes
1 >= 2 which is false
while
2>= 1 >= 0
1 >= 0 which is true

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by snk_kid
i mean't instead of pointer to member functions or pointer to data members, why no reference to member functions or reference to data members but you can with free/class functions.


Hmmm. Good question. I didn't even know you could not.

i was stuck with that recently.
thread
additional info

I'm a Pascal programmer swiched to C++ not so long ago, yet i have no troubles answering such questions.

BTW :
"
Why does the following code segment display
"x does not equal 0.01, y equals 0.05"?
float x = 0.01;
float y = 0.05;
if (x != 0.01) cout << "x does not equal 0.01, ";
if (y == 0.05) cout << "y equals 0.05";
"

probably there should be 0.5 , not 0.05
0.05 can't be exactly represented by float nor double. When comparing, IIRC on x68 float is converted to double. IIRC by standard, it's double must be converted to float, but i'm not 100% sure.

Share this post


Link to post
Share on other sites
to Eriond:

4.5 Integral promotions
An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true
becoming one.

to Cocalus:

Isn't the 2's complement just a binary not followed by +1 ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dmytry
BTW :
"
Why does the following code segment display
"x does not equal 0.01, y equals 0.05"?
float x = 0.01;
float y = 0.05;
if (x != 0.01) cout << "x does not equal 0.01, ";
if (y == 0.05) cout << "y equals 0.05";
"

probably there should be 0.5 , not 0.05
0.05 can't be exactly represented by float nor double. When comparing, IIRC on x68 float is converted to double. IIRC by standard, it's double must be converted to float, but i'm not 100% sure.


I tried the 0.05 version, the test fails. For 0.5 it works as Dmytry said.

BTW nice thread, thx for the idea.

Share this post


Link to post
Share on other sites
Oh, come on. If you're going to ask a question like "How well you know your c++?", you should at least post problems specific to C++. All of those problems can happen in any number of languages.

The first two are tied to the design of the CPU (i.e. 2's complement negative integers and IEEE floating point numbers). The last question is simple boolean algebra, and again is not specific to any language (other than the fact that some languages will evaluate from left-to-right and others from right-to-left).

Share this post


Link to post
Share on other sites
Quote:

to Cocalus:

Isn't the 2's complement just a binary not followed by +1 ?


Mybad, 2's complement is inverting all the bits (I think that's called the one's complement) then adding one. I remember concepts well but not names of things.

-Cocalus

Share this post


Link to post
Share on other sites
Undefined behavior, I think - accessing *p after deleting it could just give you 4 anyways (if the OS doesn't mind you accessing memory you don't own), or it might open a gateway to inferno, requiring space marine tech support. Iostream.h is also undefined by the standard, so who knows?

Share this post


Link to post
Share on other sites
Quote:
Original post by MrPoopypants
Here is a toughy:


#include <iostream.h>

int main(){
int *p = 4;
delete p;

cout << *p << endl;

return 0;
}


What will this code do to your computer?

(I found out last night)
It would not compile on my computer :) If I use iostream, it still doesn't compile (can't convert from int to int*), when casting 4 to int* the program crashes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well you make a pointer and then delete and then get the value at its adress... so it would probaly give you some random information or 4, or get mad at you and cause random errors.

btw that winking smilie is fugly! :p

Share this post


Link to post
Share on other sites
Actually, it froze my computer with flying colors.

It wasn't like one of those freezes that results from CPU overusage, my computer simply took a crap right there in front of me. Nothing worked. No mouse, no keyboard control (no Ctrl+Alt+Delete), no nothing. I had to give it a hard boot. The .cpp file i used opened itself as a binary file when I tried to open it again.


Maybe that wasnt the exact code, but i know i delcared a pointer
deleted the pointer, then tried to access the value at the address the deleted pointer pointed to.

In fact, before my fatal compile, I did a cout of p and it printed 0x00000000. So perhaps it tried to get from that memory slot, which probably only contained dust and silicon. I have no idea.

So in conclusion, don't try it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this