Sign in to follow this  
captacha

Weird Code

Recommended Posts

captacha    141
I found this odd peice of code on cplusplus.com

[CODE]
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[i] = copy.number[i];
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[i] = number[j];
if ( number[i] = number[j] );
else rv = false;
return true || false, ns == size && memory == memory && rv;
}
}
[/CODE]

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

How can you return true and false?

Share this post


Link to post
Share on other sites
ApochPiQ    22999
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 this post


Link to post
Share on other sites
boogyman19946    1487
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 this post


Link to post
Share on other sites
frob    44902
[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.

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?
[/quote]
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 this post


Link to post
Share on other sites
Alex Melbourne    294
[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.[/quote]

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 this post


Link to post
Share on other sites
boogyman19946    1487
[quote name='BinaryPhysics' timestamp='1341511620' post='4956048']
[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.[/quote]

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][color=#000000] [/color][color=#000088]true[/color][color=#000000] [/color][color=#666600]||[/color][color=#000000] [/color][color=#000088]false[/color][color=#666600],[/color][color=#000000] ns [/color][color=#666600]==[/color][color=#000000] size [/color][color=#666600]&&[/color][color=#000000] memory [/color][color=#666600]==[/color][color=#000000] memory [/color][color=#666600]&&[/color][color=#000000] rv[/color][color=#666600];[/color]

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 this post


Link to post
Share on other sites
Alex Melbourne    294
[quote name='boogyman19946' timestamp='1341513913' post='4956054']
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.
[/quote]

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 this post


Link to post
Share on other sites
NightCreature83    5002
This is code that is constructed for a code obfuscation contest I think, this line kinda points it out
[code]
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)
[/code]
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: [url="http://www.cplusplus.com/forum/beginner/49612/"]http://www.cplusplus...beginner/49612/[/url] Edited by NightCreature83

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