Weird Code

This topic is 2230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I found this odd peice of code on cplusplus.com

 bool integer::resize(VERY long ns) { if (ns == size) return true && false; if (ns == 0) { NUL = true; //delete [] number; return true || false; } if (ns > size && ns <= memory) return size = ns, false, true; if (ns > memory) { integer copy (*this); delete [] number; size = ns; memory = (size%mem_chunk_size == 0) ? size/mem_chunk_size:(size/mem_chunk_size+1)*mem_chunk_size; number = new digit[memory]; initialize(); for (VERY BIG NUMBER i = memory - size; i < memory; ++i) number = copy.number; return size == ns; } if (ns < size && memory == (memory = (ns < mem_chunk_size) ? mem_chunk_size: ns%mem_chunk_size == 0 ? ns/mem_chunk_size*mem_chunk_size:(ns/mem_chunk_size+1)*mem_chunk_size), memory) { integer copy(*this); delete [] number; size = ns; number = new digit[memory], initialize(); bool rv = true; for (VERY BIG NUMBER i = memory - size, j = copy.memory-size; i < memory, j < memory; ++i, ++j) //number = number[j]; if ( number = number[j] ); else rv = false; return true || false, ns == size && memory == memory && rv; } } 

What does it mean? It doesnt really make sense to me.

How can you return true and false?

Share on other sites
That code is utter crap, in numerous ways, but that's kind of irrelevant to your question :-)

To explain how the returns work: remember that everything to the right of the "return" keyword is evaluated as a standalone expression, and the result of that evaluation is what is actually returned. So the expression "true && false" is going to evaluate to false, so that could be simplified to "return false". On the flip side, "true || false" is going to be true always, so that's just "return true".

Then there's the horrible, awful, evil abuse of the comma operator. Frankly I hate the comma operator and refuse to remember its semantics, so I can't tell you off the top of my head what that does because I don't want to think about it ;-) But you can probably figure it out by remembering the above rule of thumb.

Share on other sites
I've never heard of the comma operator before, but having looked it up, that whole statement doesn't make any sense to me.

I just tested a statement similar to the one above and it seems that the operator will always return the second operand, so the first operand doesn't seem to have an effect. Is the true || false statement completely useless then?

Share on other sites

I've never heard of the comma operator before, but having looked it up, that whole statement doesn't make any sense to me.

I just tested a statement similar to the one above and it seems that the operator will always return the second operand, so the first operand doesn't seem to have an effect. Is the true || false statement completely useless then?

It will run, effectively do nothing, and the result will be discarded. The optimizer will probably remove it.

I've got to agree that the code is garbage, written to confuse and mislead rather than to convey the meaning behind the code.

As for its intent, it appears to be part of a "large integer" class, that holds very large integers. It appears to resize a portion of the memory needed to hold that very large number, expanding or contracting as necessary. It does so with quite a lot of unnecessary complexity. Edited by frob

Share on other sites
It also means someone in the company may be a Wally.

(If you don't recognize the reference, read Dilbert)

Share on other sites
I've never heard of the comma operator before, but having looked it up, that whole statement doesn't make any sense to me.

The comma operator should not be confused with the comma that appears in function calls or declaration lists. Ever. They are not the same thing.

Now that that is cleared up: the comma operator is a method of chaining statements together. It is left-to-right associative and thus will return the latter of the two operands (the one on the right). Unlike other operators in C and C++ the comma operator guarantees that it's operands will be evaluated in order; this means that effects from the first operation (although the result is discarded) will be available to the second. Edited by BinaryPhysics

Share on other sites

[quote name='boogyman19946' timestamp='1341260113' post='4955026']I've never heard of the comma operator before, but having looked it up, that whole statement doesn't make any sense to me.

The comma operator should not be confused with the comma that appears in function calls or declaration lists. Ever. They are not the same thing.

Now that that is cleared up: the comma operator is a method of chaining statements together. It is left-to-right associative and thus will return the latter of the two operands (the one on the right). Unlike other operators in C and C++ the comma operator guarantees that it's operands will be evaluated in order; this means that effects from the first operation (although the result is discarded) will be available to the second.
[/quote]

I know that, which is exactly why the statement does not make any sense.

The statement

[color=#000088]return[color=#000000] [color=#000088]true[color=#000000] [color=#666600]||[color=#000000] [color=#000088]false[color=#666600],[color=#000000] ns [color=#666600]==[color=#000000] size [color=#666600]&&[color=#000000] memory [color=#666600]==[color=#000000] memory [color=#666600]&&[color=#000000] rv[color=#666600];

is, as far as I can see, exactly the same as if the comma operator was not used there at all. It's not the operator that's confusing me, it's the seemingly useless application of it.

Share on other sites

I know that, which is exactly why the statement does not make any sense.

The statement

return true || false, ns == size && memory == memory && rv;

is, as far as I can see, exactly the same as if the comma operator was not used there at all. It's not the operator that's confusing me, it's the seemingly useless application of it.

Oh, my apologise. Yeah, the logical operators only evaluate their second operand if the condition isn't satisfied by the first (if the first is false in an 'or' expression, or if the first is true in an 'and' expression) so you're completely right - the rest of the expression is completely pointless.

The whole example is really, really shoddy and probably shouldn't be analysed for how it behaves. It should be framed so that no-one ever writes such awful shit ever again and the author should be burnt at the stake.

Share on other sites
This is code that is constructed for a code obfuscation contest I think, this line kinda points it out
 if (ns < size && memory == (memory = (ns < mem_chunk_size) ? mem_chunk_size: ns%mem_chunk_size == 0 ? ns/mem_chunk_size*mem_chunk_size:(ns/mem_chunk_size+1)*mem_chunk_size), memory) 
So many side effects, I like how the comma operator in the end is used to check whether the memory variable isn't zero lol.

For reference this is the article on cplusplus.com: http://www.cplusplus...beginner/49612/ Edited by NightCreature83

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5

• 10
• 13
• 14
• 68
• 14
• Forum Statistics

• Total Topics
632134
• Total Posts
3004304

×