[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
I've been programming for at least 5 years and I've
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 20
Thread images: 1
File: 09.jpg (63 KB, 1118x618) Image search: [Google]
09.jpg
63 KB, 1118x618
I've been programming for at least 5 years and I've only just now wrapped my head around this.

Sharing, in case someone might be interested.

#include <stdio.h>

void main()
{
char a = -128, b = 128;
printf("%d", a == b); //output: 1
}
>>
Signed overflow is undefined behavior in c
>>
Kill thyself
>>
>>55088338
took me like less than a minute to figure out desu
>>
hello nasal demons
>>
>>55088349
Asking like a beginner, but what should be the preferred approach? Never to set an out-of-bounds value, such as 128 for signed char?
>>
>>55088375
Yes, not doing undefined behavior is generally better than doing undefined behavior.
>>
>>55088394
Thanks
>>
>>55088338
>5 years

and that's why college is useless.
>>
>>55088411
Well, I'm just now starting with uni, I was in a CS-themed secondary school.
>>
>>55088338
You are fuckin idiot.
Char is 8bit long, convert -128 to bits and 128 to bits, then take last 8 bits from the number. ITS THE SAME YOU DUMB FAG.

Try with short or int or long, it will be corrent cause these types are >8 bits long.
>>
>>55088452
Yes, that's what I've come to realize.
>>
>>55088452
correct* <== dumb me
>>
>>55088349
Undefined behavior isn't a bad thing. Unless you want to try and convince me we should all be writing Rust?
>>
>>55090176
Undefined behavior is a great thing, if you are a compiler writer; you get to do whatever the fuck you want there. and if you are a language designer, specifying something is undefined behavior takes some burden off you and gives some wriggle room to the various implementations.

If you're writing something in the language though, undefined behavior is a very bad thing to rely on. There is zero guarantee that your code will do what you want from platform to platform. You are tying your program to a specific compiler that handles the undefined behavior in exactly the way you want it to and that is very bad.
>>
>>55088338
>programming 5+ years
>can't understand an 8-bit signed data structure
confirmed for bait, move on
>>
A good read about undefined behavior http://blog.regehr.org/archives/213
>>
>>55090368
I'll point out the obvious solutions that should have been obvious
- Don't write undefined behavior if you don't know how the compiler will use it.
- Don't bother writing cross-platform code. You only need to support Windows, phones, and embedded microcontrollers, and you can't use the same code for all of these anyway.
- git gud
>>
>>55090529
Or you can just not write a program that relies on undefined behavior. What do you do when your compiler silently changes the way it handles the undefined operation you do in an update? Idiots like you writing not C, but languages defined by exactly by the codified rules of whatever compiler you happened to use on whatever hardware you happened to be using at exactly the point in time when you were writing it are the fucking problem. Assholes like you are why C and languages like it have such a bad name. You are the reason there are so many fucking security vulnerabilities in mission critical software. No, we all shouldn't be writing Rust, but you should stay the fuck away C and other "unsafe" languages and stick to Rust or Ada or whatever other languages sacrifice simplicity in the specs, compilation speed, and execution speed in the name of safety. (not to imply these tradeoffs make the languages worse)
>>
>>55088375
Don't use a language which has undefined behaviour in its spec.
Thread replies: 20
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.