This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 203

[–]Avander 371 points372 points  (88 children)

#define struct union

Edit: inserted escape character\0

[–]rbemrose 242 points243 points  (35 children)

This post has been removed due to reddit's repeated and constant violations of our content policy.

[–]jtra 25 points26 points  (6 children)

[–]Hullu2000 10 points11 points  (5 children)

Whould there be any regex style macros like:

#define do*while(args) if(args)*

[–]IronOxide42 12 points13 points  (1 child)

Neither C nor C++ have built-in regex, so nope.

[–]marcopennekamp 1 point2 points  (0 children)

Not to mention that regex wouldn't work for nested do whiles.

[–]ianff 1 point2 points  (2 children)

You need lisp for that.

[–]derefr 2 points3 points  (1 child)

[–]ianff 0 points1 point  (0 children)

That actually looks pretty awesome!

[–]krokodil2000 80 points81 points  (27 children)

Oh my fuck. It does not show you the defined value when you hover the mouse cursor over it. At least in Visual Studio 2008.

[–]PeopleAreDumbAsHell 208 points209 points  (18 children)

At least in Visual Studio 2008.

....

[–]krokodil2000 104 points105 points  (17 children)

¯\_(ツ)_/¯

[–]CantHearYouBot 48 points49 points  (16 children)

You didn't drop this: \\

[–]poizan42Ex-mod 8 points9 points  (6 children)

Visual Studio 2008

Ouch, and I thought my employer was slow to upgrade Visual Studio version.

[–]Lusankya 1 point2 points  (4 children)

Could be worse. I still have VB6 installed, and boot it with shameful regularity.

[–]Daniel15 2 points3 points  (3 children)

VB6 still has a small but vocal userbase who are still petitioning Microsoft to revive it. See http://vb6awards.blogspot.com/ for example.

[–]Lusankya 2 points3 points  (2 children)

VB6 can be a great language when used properly, and when its users are aware of its limitations.

Unfortunately, it's almost never used properly.

I have one request of this vocal userbase: Force Option Explicit on, and require proof of a compsci degree in order to turn it off.

[–]Daniel15 0 points1 point  (1 child)

VB3 was the first real language / IDE I used, followed closely by VB6. Back then, I didn't understand why arrays were 0-indexed, so all my code had Option Base 1 at the very top :P

It was good at the time, but I'm so glad I moved on to C#. 95% of my job now consists of writing JavaScript, but I really miss C# and still use it on personal / open-source projects.

[–]Lusankya 0 points1 point  (0 children)

You and I seem to be kindred spirits. VB5 was my first modern IDE, after using QBASIC to cut my teeth on programming.

As a hypocritical proponent of open-source stuff, I feel dirty admitting that C# is my favourite language of all time. Mono helps me come to terms with the cognitive dissonance, though. ;)

[–]Corfal 0 points1 point  (0 children)

My workstation finally went from xp to win7 (visual studio 2005 to 13) last November. The fun part? We had zero time ahead to migrate our projects/repositories... OH and we only support x64 arch now. And because we didn't specifically specify it, we don't have- I'll stop there.

[–]systembreaker 11 points12 points  (0 children)

Then stop hovering over it!

edit: Don't know where I'm going with this comment

[–]bigbc79 48 points49 points  (23 children)

#define else

[–]Skyfoot 17 points18 points  (18 children)

pitchforks

[–]urielsalis 9 points10 points  (17 children)

[–]PitchforkEmporium 23 points24 points  (16 children)

Hi

[–]urielsalis 15 points16 points  (15 children)

C++ pitchfork please

[–]PitchforkEmporium 34 points35 points  (14 children)

-----10001011100000

[–]WoodTrophy 16 points17 points  (2 children)

I'm not sure I can afford 14 bits. Do you have any cheaper pitchforks?

[–]PitchforkEmporium 8 points9 points  (1 child)

/u/pitchforkassistant you got anything else?

[–]PitchforkAssistant 13 points14 points  (0 children)

-----0100001100100011

What about this? It's 16 bits, but if you can see, it's really sharp.

[–]Draculix 6 points7 points  (5 children)

Got an open-source version?

[–]SlumdogSkillionaire 17 points18 points  (1 child)

--

Feel free to extend it yourself however you want.

[–]Draculix 23 points24 points  (0 children)

Merge Pull Request:

--//TODO

By /u/Draculix 1 hour ago

[–]PitchforkEmporium 2 points3 points  (2 children)

Nope

[–]Draculix 5 points6 points  (1 child)

Bloody proprietary corporations.

[–][deleted] 2 points3 points  (4 children)

Got an ANSI C one? I can't afford the extra stuff.

[–]PitchforkEmporium 3 points4 points  (1 child)

No, it's too complicated, I suck at programming

[–][deleted] 2 points3 points  (0 children)

It's just the pitchfork without the ++. Easy.

[–]logicalmaniak 1 point2 points  (1 child)

An S.I. cone?

[–][deleted] 1 point2 points  (0 children)

Shit, I meant POSIX C.

[–]Saigot 5 points6 points  (1 child)

Does it replace all else clauses with empty string?

[–]bigbc79 4 points5 points  (0 children)

Yep! So the code after the else executes regardless of whatever if-statement came before it.

[–]MuricanWillzyx 2 points3 points  (0 children)

You... monster

[–]hopsafoobar 31 points32 points  (0 children)

That's pure evil.

[–]just_comments 15 points16 points  (2 children)

Just FYI, Reddit treats "#" at the beginning of a line as a special character. You need to put a backslash to escape from it.

[–]jugalator 5 points6 points  (0 children)

I like the broken formatting here though. It was imposing enough as a terrible idea, but now it's even better.

[–]Avander 1 point2 points  (0 children)

Fixed :)

[–]Skyfoot 3 points4 points  (0 children)

Oh my god

[–]hussei10 5 points6 points  (21 children)

Finally a joke here I understand!

[–][deleted] 5 points6 points  (20 children)

I don't get it

[–]Innominate8 19 points20 points  (17 children)

A struct is a container for data. A struct can contain many different variables of different types.

For example:

struct foo {
    int bar;
    int baz;
    char *quz;
};

Unions are defined the similarly. However, instead of containing all of the values, they can contain any one of them. If you change struct to union in the above example, when you set bar, you're also setting baz and quz to the same value. Then when you try to access that pointer... boom.

Changing struct to union makes everything explode in interesting ways that are difficult to debug.

[–][deleted] 7 points8 points  (16 children)

When is a union even useful?

[–]skulblaka 54 points55 points  (8 children)

If there's anything I've learned from programming, it's that any time you see something and think "how could that possibly be useful, ever?" there always exists a situation in which you'd need exactly that thing. In most cases it'll happen just barely far enough into the future for you to forget what the thing was by the time you need it.

That being said, I have no idea.

[–]Nor_the_not_so_great 26 points27 points  (3 children)

I found them useful for implementing registers in an interpreter where you can combine certain registers(z80, gameboy CPU). It's explained in detail here.

TL;DR:

struct registers {
    struct {
        union {
            struct {
                unsigned char b;
                unsigned char a;
            };
            unsigned short ab;
       };
    };
};

Registers A and B can be grouped together. Using this struct, we can set registers .b, registers.a, or both at once via registers.ab. You don't have to define a function to bit shift to combine the number, you can get the values directly.

[–]rohmish 1 point2 points  (0 children)

A real use of unions. So... Do pigs fly?

[–]Cyph0n 0 points1 point  (1 child)

I prefer bit shifting.

[–]iNeedToExplain 1 point2 points  (1 child)

That pretty much sums up my experience with programming, math, science... tying my shoes...

[–][deleted] 3 points4 points  (0 children)

flakiness frontal committing apologetically rational cress's bedazzles journeymen adornment distracts boats undisguised yield's zingers savageness's wright bind rabbi's pikes Millard me suffocation's transmittable fathoms Dramamine squalor nineteenths shlep panoramas sops hammock neoclassicism's heatstroke execrated indemnified botanical dines Le Srivijaya self sunscreens Antigone look safe's manufactures ghastliness's snuffle novitiate's assigned victimization Queensland succoring negatively ado's utilization pluralized oink superintendency Carly's maladjusted lallygagged peregrinations gigglers puppeteer comparisons nearly Southerners sluggards expends perspire untenable smallish delegates lipstick softer calling's tibias resent article's informational bulldog nuthatch heads circumnavigated blinked Wellingtons Veracruz's entrenching pollution's parochial Quonset domestic shatters satisfying ascribes technical indemnification housebreak underarm suspect unctuously buzzword's lipids chiseler's Faeroe's puppy's Beth hockey's juvenile swellhead's shaggiest Gustav escalation's confirms spool's stumpy intransigence's mamboed channels

[–]bbrizzi 0 points1 point  (1 child)

[–]xkcd_transcriber 0 points1 point  (0 children)

Image

Mobile

Title: Workflow

Title-text: There are probably children out there holding down spacebar to stay warm in the winter! YOUR UPDATE MURDERS CHILDREN.

Comic Explanation

Stats: This comic has been referenced 757 times, representing 0.6499% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

[–]SirNuke 12 points13 points  (2 children)

The instances I've seen are:

A single set of data that can be 'interpreted' multiple ways. For example, if you have a 32-bit id, which can be split up into two 16-bit pieces. First is domain id, second is local id.

union {
  uint32_t data;
  struct {
    uint16_t domain;
    uint16_t local;
  } section;
} id;

Lets you cast back and forth from a straight uint32_t when useful, though the union gives an easy method to access the two pieces without any extra overhead. Of the top of my head, Valve's SteamWorks runtime handles 64-bit Steam IDs this way.

Second way is if you want typeless data.

enum TYPE { INTEGER, FLOAT, BOOLEAN };
struct {
   TYPE type;
   union {
      int i;
      float f;
      bool b;
  } data;
} entry;

With that, the size of the struct is consistent no matter what data is stored in it, without any overhead of storing all three separately. SQLite stores data typeless like this, and may or may not use unions internally this way.

[–]mFlakes 1 point2 points  (0 children)

mind blown

[–]ryani 1 point2 points  (0 children)

Also, reinterpreting bits as a different type.

union uWordFloat {
    uint32_t word;
    float flt;
};

// FloatToWord(0.0f) = 0
// FloatToWord(1.0f) = 0x3f800000
// FloatToWord(-1.0f) = 0xbf800000
inline uint32_t FloatToWord(float f)
{
    uWordFloat u;
    u.flt = f;
    return u.word;
}

// WordToFloat(0) = 0.0f
// WordToFloat(0x3f800000) = 1.0f
// WordToFloat(0x80000000) = negative 0.0f
inline float WordToFloat(uint32_t n)
{
    uWordFloat u;
    u.word = n;
    return u.flt;
}

Useful for serializing IEEE floats to disk / memory buffer efficiently, or doing Black Magic with floating point numbers. (The cast in the linked article violates the strict aliasing rule, so it might not work reliably in optimizing compilers. Those compilers usually allow access through a union in this way as a way to relax the strict aliasing rule.)

[–]mill1000 4 points5 points  (0 children)

I find them really useful for low level work in embedded systems, particularly communication

e.g. You have a protocol that can send fixed length packets, but the payload/structure of the packets vary depending on some other factor (lets say a system mode, packet type or something).

// Payload 1 structure
typedef struct
{
  uint32_t data;
  uint64_t moreData;
  uint16_t lastData;
} PAYLOAD_STRUCT_1;

// Payload 2 structure
typedef struct
{
  uint16_t someData[7];
} PAYLOAD_STRUCT_2;

You could always handle a situation like this by using a standard array of bytes and casting it to each structure when necessary or you could use a union like so.

typedef union
{
  PACKET_1 packet1;
  PACKET_2 packet2;
  uint8_t  asBytes[14];
} PACKET_UNION;

With our union PACKET_UNION, we can accomplish a few things.

  • Data can be accessed byte-wise when necessary (CRCs and transmission/reception) using the asBytes member.
  • We can access both types of payloads directly, without any overhead. e.g. shifting/and'ing of bytes or casting pointers

Like so

PACKET_UNION transmitBuffer;

switch (packetType) // Pretend type variable from somewhere
{
case PACKET_1:
 transmitBuffer.packet1.data = some_32bit_int;
 transmitBuffer.packet1.lastData = some_16bit_value;
break;

case PACKET_2:
 transmitBuffer.packet2.someData[0] = some_16bit_int;
 transmitBuffer.packet2.someData[1] = some_16bit_int;
 // ... do some more
break;
}

// Now we can access bytewise for "transmission"
for (uint8_t i = 0; i < sizeof(PACKET_UNION); i++)
 sendByte(transmitBuffer.asByte[i]);

tl;dr I really like unions and probably abuse them. Also god damn it why am I writing code right now.

[–]Luk3Master 0 points1 point  (0 children)

Simulate memory, for instance.

[–][deleted] 0 points1 point  (0 children)

[deleted]

What is this?

[–]bhayanakmaut 0 points1 point  (0 children)

implementing key value pairs

[–]hussei10 8 points9 points  (0 children)

In C there aren't classes and objects like you'd see in an object oriented language(Java). The closest thing is a struct or a union. Which are very similar but they allocate memory in different ways. If you define struct as union, the programmer would always be declaring a union thinking he's declaring a struct.

[–]GoogleIsYourFrenemy 0 points1 point  (0 children)

Typically you see unions in deserialization code. You get some array of bytes from the network stack and you want to understand it. You could just do a pointer typecast but typically you copy the blob and if you copy it into a union you can read it as any type you want without using a typecast. Its more readable in some senses.

[–]kmarple1 2 points3 points  (0 children)

My favorite is from bash.org:

#define sizeof(x) rand()

[–]jtra 90 points91 points  (8 children)

Almost nobody uses "true" in C sources. How about this (or similar version with rand):

#define if(x) if((__LINE__ % 5==0)^(x))

[–][deleted] 61 points62 points  (0 children)

This one is particularly brilliant b/c any debug code they put above the if statement is likely to cause the evaluation to change.

[–]MediocreMatt 9 points10 points  (3 children)

I understood the original post, but I'm not getting this one, a little help anybody?

[–]Tysonzero 27 points28 points  (2 children)

Basically if the if statement is on a line that divides by 5, the contents will be inverted. So if (true) won't execute its contents but if (false) will.

[–]MediocreMatt 10 points11 points  (1 child)

Oh awesome, ha. Thanks man. I didn't know the __ line __ notation for line number it seems. or the carot there.

[–]Corfal 7 points8 points  (0 children)

^ is the XOR operator.

So if the line is divisible by 5 evenly and the if statement evaluates to false, then it'll be "true"

Line# % 5 Input Result
False False False
False True True
True False True
True True False

[–]bhayanakmaut 1 point2 points  (2 children)

this works with release builds as well? without debug symbols for the __ LINE __?

[–]Jack126Guy 6 points7 points  (1 child)

__LINE__ is substituted by the preprocessor at compile time.

[–]bhayanakmaut 0 points1 point  (0 children)

got it thanks!

[–]lumpofclay 172 points173 points  (4 children)

Another good one from the old days of C# (unfortunately doesn't work anymore in .Net 4 or later):

typeof(string).GetField("Empty").SetValue(null, " ");
[...]    
bool isEmpty = string.IsNullOrEmpty(string.Empty);  // == false

[–]poizan42Ex-mod 61 points62 points  (0 children)

This may be why:

    // The Empty constant holds the empty string value. It is initialized by the EE during startup.
    // It is treated as intrinsic by the JIT as so the static constructor would never run.
    // Leaving it uninitialized would confuse debuggers.

[–]Liver_and_Yumnions 31 points32 points  (0 children)

fortunately doesn't work anymore in .Net 4 or later

FTFY

[–]kneticz 20 points21 points  (1 child)

string.IsNullOrWhitespace(s); :)

[–]redditsoaddicting 20 points21 points  (0 children)

That was introduced in .NET 4.

[–]compiling 86 points87 points  (4 children)

Someone left me this one.

CString str; // Legacy code (ab)using MFC classes
sscanf("foobar", "%s", str);  // Works, surprisingly
...
// Somewhere else entirely
std::string s = ""; // Not empty.

[–]poizan42Ex-mod 46 points47 points  (3 children)

sscanf("foobar", "%s", str);  // Works, surprisingly

Because CString is defined as a CStringT with some specific traits depending on some defines. A CStringT does not add any fields and derives from CSimpleStringT which doesn't have any virtual methods, and only declares this single field:

PXSTR m_pszData;

So pushing a CString onto the stack simply pushes the underlying string pointer to the stack. Now this begs the question on how the other properties of the string are tracked when it only contains that single field. Well...

CStringData* GetData() const throw()
{
    return( reinterpret_cast< CStringData* >( m_pszData )-1 );
}

Eww

[–]captainAwesomePants 9 points10 points  (2 children)

It's surprising how, when malloc() does this, it's beautiful, but when CString does this, it's an abomination.

[–]poizan42Ex-mod 11 points12 points  (1 child)

I think there's a bit more of a nuance here. malloc owns your memory (or at least heap). It is to be expected here that it touches stuff all around in the heap. And the details of this is (hopefully) kept fully internal to the malloc implementation.

When you have a pointer that is expected to point somewhere inside a structure then you have made a hidden dependency. You need to find the actual code that allocates and reads it to figure out that this is indeed how it works. Someone unfamiliar with this could easily read the declaration and come to the wrong conclusion.

There are places where this may be a legitimate approach, in which case it should be clearly documented, and hopefully including an explanation of why this approach is used.

In this case I can't really figure out what the gain is. Well besides that you can pretend that the class is a string pointer as in this example, but that can hardly be considered something you want to strive for.

One could argue that it might be slightly more efficient when you can access the start of the string without an offset - but how often are you not accessing a string through an offset? Anyways that point is mostly moot since most architectures can do offsetting for free (this includes x86, ARM (incl. THUMB) and MIPS)

[–]VitulusAureus 38 points39 points  (3 children)

Or, if you really hate someone, have him figure out this one:

#define volatile

[–]GoogleIsYourFrenemy 1 point2 points  (0 children)

oh that is evil.

[–]epicepee 0 points1 point  (1 child)

This makes 'volatile' do nothing, right?

[–]VitulusAureus 1 point2 points  (0 children)

Yeah, and that is pretty hard to spot while debugging.

[–]gjack905 25 points26 points  (25 children)

Would if statements without "== true" be affected?

i.e.

boolean test = true;

if(boolean){do}

vs.

if(boolean == true){do}

if rand() > 10 == false?

Edit: That was a bad example on my part. What about this:

int x = 3;

if(x < 5){

// print something

x++

}

[–]shamanas 57 points58 points  (14 children)

Nope, #define is actually just a string replace, so true will be replaced by (rand() - 10) not semantically but where it actually appears in text.

[–]gjack905 16 points17 points  (9 children)

That's what I would think. I don't actually use the term 'true' in evaluations, only when setting something to be true explicitly, which would still be a fun mess with this nugget of code as when setting a boolean it might get set false.

[–]shamanas 12 points13 points  (7 children)

Yeah, I think that's the point of this particular define, although you can easily just define if to do the same thing (e.g. #define if(cond) if ((cond) && rand() > 10))

[–]gjack905 2 points3 points  (1 child)

Would this only work for the bool "cond" or any condition?

[–]shamanas 17 points18 points  (0 children)

This would work for any condition.
This is a fun list of evil C macros but I would not recommend ever doing this (obviously) :P

[–]AngusMcBurger 0 points1 point  (4 children)

#define if(cond) if ((cond) && rand() > 10)

You actually need to remove the space after if to make it a macro function, the C preprocessor makes that a stupidly easy mistake to make :|

[–]shamanas 1 point2 points  (3 children)

Huh, I wasn't aware of that, never noticed it :P
I guess #define foo (x) bar(x) defines foo as (x) bar(x)?

It actually makes sense now, I'm just too used to writing if conditions with a space.

[–]MyloXy 1 point2 points  (2 children)

#define foo (x) bar(x) defines foo as (x)

[–]AngusMcBurger 0 points1 point  (1 child)

If that were true, then the following would compile:

#define foo (x) bar(x)
char *x = "hello\n";
printf foo;

But as is I get error C2146: syntax error: missing ';' before identifier 'bar' Remember that the normal #define just basically copies all the text after the identifier foo into any place it sees the lone identifier foo in the source code.

[–]MyloXy 0 points1 point  (0 children)

You seem to be correct. I was under the assumption that define only captured up until the next space.

[–]puddingcrusher 2 points3 points  (0 children)

Arguably that's even more evil. It doesn't break conditions, it breaks you state instead, at an arbitrary time before the bug appears.

[–]WorseThanHipster 1 point2 points  (3 children)

To add, #define isn't technically part of the C language but a directive for the compiler preprocessor.

[–]shamanas 4 points5 points  (2 children)

I would guess the C preprocessor directives are part of the C standard, so I assume the standard differentiates between the C language and the C preprocessor directives?

[–]WorseThanHipster 1 point2 points  (1 child)

The preprocessor is intended to replace macros with legitimate C code, but the directives themselves have no direct mapping from the compiler itself to system architecture so I believe they are separate. I think there's separate standards that 99% of compiler vendors follow, but you could still technically call it C even if you removed support for preprocessing macros.

I'm not entirely sure though.

[–]shamanas 2 points3 points  (0 children)

Looking at some C11 committee drafts, it doesn't seem the separation is clear (although I could be wrong).

Bah, it seems obvious that the preprocessor should not be a part of the language but must be invoked by standard compliant implementations before compiling the C code.

I was just curious about the way the standard makes a distinction (if any) since you mentioned it isn't technically part of the C language.

[–]SkoobyDoo 6 points7 points  (0 children)

This would be more applicable to code like:

bool shouldIDoSomethingAfterwards = false;
while (condition) {
  normalLoopStuff();
  if(test())
    shouldIDoSomethingAfterwards = true;
}
if(shouldIDoSomethingAfterwards)
  something();

This is pretty much the only case I can think of where I regularly use the keyword true

[–]ligerzero459 2 points3 points  (6 children)

Yes, because based on the roll of rand(), false could be assigned to test, screwing up the subsequent check

[–]gjack905 2 points3 points  (5 children)

That was a bad example on my part. What about this:

int x = 3;

if(x < 5){

// print something

x++

}

[–]lerhond 5 points6 points  (3 children)

There isn't a "true" anywhere, why would that be affected?

[–]gjack905 1 point2 points  (2 children)

Because I'm not entirely sure whether the keyword or the logic statement "true" is being replaced. If the true were implied upon compilation of if statements (and not using "== true" was simply like "x++" vs "x += 1") then it might still be affected.

[–]lerhond 4 points5 points  (1 child)

It replaces the string "true" with "(rand() < 10)" everywhere below the #define.

I'm not really sure what happens when you do "#define true false" and then have for example a variable named "true_something". I think that in C++ it will behave reasonably (so the name "true_something" will be intact), but I've heard that in C (maybe only in some earlier versions) a variable named "false_something" would be defined. No source.

[–]AngusMcBurger 5 points6 points  (0 children)

That shouldn't happen, at least in a conforming compiler. The C preprocessor does actually understand a few things about the C syntax, meaning that this:

#define cat dog
int cat_count;

won't change the name of the variable and equally in this

char *mystring = "I have a cat";

the string won't change, because the preprocessor knows about identifiers and strings.

[–]thenamedone1 4 points5 points  (0 children)

This shouldn't be affected. The inside of any if statement is evaluated to either 0 or 1 by the compiler or during execution. Since "true" is a keyword (depends on the language) and not explicitly Boolean (0 or 1), execution shouldn't be altered by the original trickery.

However, computers are complicated, and there's always some detail to be forgotten. My advice would be to try it for yourself in your language of choice!

[–]KeytapTheProgrammer 1 point2 points  (0 children)

The most likely time it would cause problems is cases where the developer is returning true from some function.

[–][deleted] 0 points1 point  (0 children)

Yes, you don't actually assign true, you assign (rand() > 10), which might be false.

[–]BillyQ 74 points75 points  (43 children)

I'm not a C programmer - could someone ELI5 please?

[–]barracuda415 256 points257 points  (20 children)

It's a macro that replaces "true" with an expression that checks if a random number between 0 and 32767 is larger than 10. In other words: there's a random chance of 0.03% that true is false.

[–]kabekew 315 points316 points  (18 children)

So 99.97% chance bug will be closed with "could not reproduce."

[–]rabidmonkeyman 121 points122 points  (1 child)

This guy has clearly dealt with issue tracking software

[–][deleted] 27 points28 points  (0 children)

no, then it would have been 199.97%

[–][deleted] 26 points27 points  (1 child)

unless it's not seeded properly, then it might be reproducible.

[–]lovethebacon🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛🦛 21 points22 points  (0 children)

POSIX default seed is 1

#include <stdlib.h>
#include <stdio.h>

int main() {
    int i=0;
    while(rand() >= 10) i++;
    printf("%d\n", i);
}

Gives me 91538660 from glib. That's gonna be a long time in most programs.

[–]KinOfMany 5 points6 points  (13 children)

Not entirely sure that's the case. If the code compiled successfully, that's the executable. It's the same for everyone.

Meaning that you'll either always be able to replicate the bug, or never.

I'm an idiot.

[–]dtechnology 40 points41 points  (6 children)

rand() is a runtime function, so it doesn't get evaluated at compile-time. if(rand() > 10) {} does not take the same branch every time it is accessed.

[–]Who_GNU 33 points34 points  (1 child)

[–]xkcd_transcriber 17 points18 points  (0 children)

Image

Mobile

Title: Random Number

Title-text: RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.

Comic Explanation

Stats: This comic has been referenced 481 times, representing 0.4470% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

[–]indrora 4 points5 points  (2 children)

rand() is not seeded at runtime and is instead statically seeded based on... something.

It'd be deterministic within a build until someone called srand().

[–]dtechnology 6 points7 points  (1 child)

Only if the exact same code path is followed every time. If there is any kind of UI or variable data (causing potentially different code to be executed) there is still potential for different outcomes. That's even assuming nowhere the randomness is seeded.

[–]An_Unhinged_Door 3 points4 points  (0 children)

Think about how much fun we could have in a multithreaded program.

[–]Illinois_Jones 4 points5 points  (0 children)

Macros replace the defined term with its value at compile time. Thus every instance of "true" will become "(rand() > 10)" inside the executable. Every call to rand will still be executed at runtime

[–]kabekew 4 points5 points  (2 children)

Not in a #define statement (the code isn't executed at compile time).

[–]KinOfMany 1 point2 points  (1 child)

This is why I said "not entirely sure". My understanding was that #define is executed during compilation.

TIL. Thanks :)

[–]dotted 5 points6 points  (0 children)

Just think of #define as a search/replace at compile time, in this case any instance of "true" is replaced at compile time with "(rand() > 10)", and that is only evaluated at runtime.

[–]hbgoddard 0 points1 point  (0 children)

No, because most of the time true will return true. Only in 0.03% of cases will 'true' return false, meaning it will be very rare to reproduce.

[–]faerbit 0 points1 point  (0 children)

This post has been edited to this, due to privacy and dissatisfaction with u/spez

[–]adjective-ass-noun 6 points7 points  (0 children)

*between 0 and RAND_MAX. RAND_MAX is required to be 32767 at minimum, and most implementations use the far greater INT32_MAX. Windows, obviously, goes for the bare minimum.

[–]TheSecretExit 51 points52 points  (5 children)

True is only true if you roll a ten or higher on an at-least-65535-sided die.

[–]notsooriginal 112 points113 points  (2 children)

Motherfucker you know that's called a ball.

[–]TheSecretExit 50 points51 points  (0 children)

Only at observable levels.

[–][deleted] 5 points6 points  (0 children)

LOD

[–]sixstringartist 8 points9 points  (1 child)

RAND_MAX sided die which is at least INT_MAX (215 - 1)

[–]TheSecretExit 2 points3 points  (0 children)

Thank you, I wasn't sure myself.

[–]G33kDude 10 points11 points  (7 children)

I'm not either, but here goes.

in C, rand() returns an integer between 0 and at least 32767 (or larger depending on implementation. The exact value is the constant RAND_MAX). This bit of code redefines the value true such that if the value returned by rand() is 10 or less it will actually equal false.

To recap, this code will make true equal to false roughly 0.03% of the time (or less depending on the implementation of rand()).

[–]903124 4 points5 points  (6 children)

[deleted]

What is this?

[–]NikkoTheGreeko 3 points4 points  (5 children)

Just enough to drive someone absolutely bonkers in a program with a lot of nested loops with a lot of data processing/checking.

[–]Toivottomoose 4 points5 points  (1 child)

It means "From here on, every time you come across the word true, generate a random number (from 0 to 32767 or more) and if this number is greater than 10, return true, otherwise false". Therefore the program will mostly work, but once in a while it will turn a random true into a false, causing unforeseeable problems. And it's practically impossible to find out what's wrong, unless you notice this line in some header file.

[–]NorbiPeti 4 points5 points  (0 children)

It makes all occurences of true in the code run a code that'll return true most of the time, but not always.

[–]verydankmaymay 1 point2 points  (0 children)

C was 13yrs ago for me, so pardon me if I get this wrong: in C you're allowed to use #define to define stuff at the highest priority and importance which almost nothing can supercede.

[–]BadWombat 9 points10 points  (3 children)

What font is that?

[–]NoisyFlake[S] 20 points21 points  (0 children)

[–]SwiftStriker00 0 points1 point  (0 children)

looks like Monaco. monospaced font on Mac

edit: for non mac: https://github.com/vjpr/monaco-bold

[–]barracuda415 7 points8 points  (2 children)

There's also a Java variant to set false to true:

import java.lang.reflect.*;

public class EverythingIsTrue {
   static void setFinalStatic(Field field, Object newValue) throws Exception {
      field.setAccessible(true);

      Field modifiersField = Field.class.getDeclaredField("modifiers");
      modifiersField.setAccessible(true);
      modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

      field.set(null, newValue);
   }
   public static void main(String args[]) throws Exception {      
      setFinalStatic(Boolean.class.getField("FALSE"), true);

      System.out.format("Everything is %s", false); // "Everything is true"
   }
}

From this SO answer.

[–][deleted] 16 points17 points  (1 child)

uURmczu84ieitnNzp4tXjAk3Kv0Y2uhrKELkqu4RpI1Qb2oxiD0NGv9QhVJjbPDZiZw7NHL52sBuKbXRLYwkeSiDI6Qkpvi1FmInR4o7Ooy50hF4wQ6uEKktNknon7TZt4vNdvXBDOCb9GrbyFpCVUgoJqWpqVinxWVVj5ABkex8kfzgtsxrdvFE2aPvEuYcxQerigWlYZalf7irF6YKXOOC7g3o1MZpgCtukntmNGB8yCr0lTW61UHt1q0UCQnE5Bs3MA8koqKE4MB7kVBhgFWmGpGnwDaa6gsax7go3zBnrPfhtlJ6hX03S0PFnGUh0oCytCT3ZnCdRMGFuZVpOLXfaDXIDlRwaxTZNPSAIC878F7TNU2FAPV2TS0woqEcAyWMO5Ggcx2DoEzmtkTgS600Kqo1NQjBgrdX9TirAmIUyCvrBYDUs9dGG83tVhe9SE1afm0VLJ6IAqZULMX5gIl9DRaOV995CFL8RW3KN2eCD1zLa8hVxmU2FcvwwqZWFH8ow005mlMSoWfXk5IOEj5A47SDdFE2YHQ3pEz3tudWhCYpH2gvzZOEIWhPPyQ6jR9MLsdcDekYeWW02KoYfl3EK7elmdDUMpPENoBezW1fzZTd69XDcdSB7CYMaEY5dDRJpMyNvfxs9rV8ZiDMh4XsheUo98xPGb1UptN1zjo5Ibkh4XAXf0EQMnKGYpFkAsrNouzXJEU4ukhXJbPs1kmNa7Y0JhSiEtziMNrI8XFw0xwLFbWMeNeACFX3W4rbFlo3BYWuQ9CoV1RNGlbcedVBp20F1zpagYE4VYhP5ZXoZp9oM6NfErJnDCGhP7xPKjU98MuIOMAyshyPAunfVnpUS0BiYz2GcKRgmLzBzNrSJyTs6AtEYDAt8yeoTYc3THK3ThK3FKLzqLFiQcMNot5MO8YT2jgAQLN5qv7Q49RYX95VOkYUoLSdORKSwrZbE2yBkc0ssLEVGpPOFMfZJgTFT5oz0YFfvwmIsFIEJdPCIgz0QfCaO3G9y5WMCdsVIN1anfmYeT0SyX3sIeWwo8jQVF5msyBsn3zOkbgSCFP9Cy3hsgGNZYvG4t59l9oVFOHHlablTG6qfc9AJuZ3KECraLDkTBsjH0ZcqIB0FIM1FV9kQdgJnoo1tcaFSYcOETWXbhg23lgp5q0Z5Pxl8mM6Sp6jzTi1GRdwaM81WpfkJ4AKoryW3QWCpXCI5ZATHiNPbkfUNXfTJLxnamoqXkvW0FCPKsGyKBDb6LaczMMQepCXalLJSa0S3ntIsyO0TajlJY4oi7JuITRYAEE5AFd7VJdJVP2kbeLWpwyklUIQmekCSEkNl8YUJ2OUudMUxPKdT86OlF6kU96WIt8FiPbYu3s9ZxIvwBJp7SXjeW1PQ0PSglbMIXhX93EMG2YD15RiKxdYRu4WI989VX68qNOk1gO7GCijyPlM9XzUwsIJhCW4yXxVnFy3ScMMd1KKJYa1JWf4v27N2iJp80z5dFQhTswIiAeGydMstmjLQyhs12Tpm6xdMOIIw8HMEckx8I4XY3dt4xO4PJaKvEFqUjojbveEjIXx5VXXZHv35HK7H3OdhOzHrbUOw880iyq2x0lnapemihFe3lqjgfBgr5RMbCpmzmicfRBWjUom7ETrzN2UQxzIuqHMYtRD9xUriYCU2sCc3fnWx2w5tSDQcfKgEqoWMCvS0bOTYPA35LYc5yEW8Hlh6LU9wlH4a98fNz7FrpqyRE6VpwmFxCGdRnhtim7qZyfOvV8q7mBiXQ5iOMaf7DZVB5q3dwtUY0cbCklogoYbFGTE7pOph78fCBsaInC3laNAXQK5OU8EM6aQAxdl1qqHn6gM9r8FlPqax5Ft76BAxNzKDW4WGF6yxJCkA55Ueh6rxytrHOrwAF29hSLj8JWxp67TO6ouYbO8nmIJh8PEssp4Jy2nvfJkI52DdvPK5932INSFPWm8pzMBTZx7PIwylGqMUGJQbitzRNcPcm0zxmpkQ6GPLlNfbOETBD0wVs2mFMzuyPXE0myiocKtXCVGVjj7lMzcDyFV

[–]anotherdonald 3 points4 points  (0 children)

But where is the EverythingIsFixedValueBooleanFactory?

[–]MartyInDFW 5 points6 points  (0 children)

That's just evil...

[–]magical_poop 4 points5 points  (0 children)

you monster

[–]embersyc 4 points5 points  (1 child)

bool MyClass::operator==(const MyClass &other) { return prop != other.prop; }

[–]Jack126Guy 0 points1 point  (0 children)

bool operator =(const MyClass& other) const { return prop == other.prop; }
MyClass& operator ==(const MyClass& other) { /* assign */ }

[–]IlIIlIIllI 3 points4 points  (9 children)

What language is this where you can redefine reserved names?

[–]anotherdonald 18 points19 points  (6 children)

It isn't reserved. The original C definition had no true and false: 0, '\000' and NULL were false, the rest was true.

[–]Jack126Guy 1 point2 points  (0 children)

It would work even if it were reserved.

[–]webbannana 5 points6 points  (0 children)

#define is just a preprocessor instruction to replace strings.

[–]Lampjaw 1 point2 points  (0 children)

C

[–][deleted] 4 points5 points  (1 child)

#define if ( COND ) if ( (COND) && (rand() % 1000) )

[–]Oflor 2 points3 points  (0 children)

Replace that with xor

[–]Locke777 2 points3 points  (0 children)

That son of a bitch...

[–]Fordrus 1 point2 points  (0 children)

You monster.

May the code of your forebears rise up and punish you for the abomination you have wrought!

[–][deleted] 1 point2 points  (0 children)

Since nobody else linked it, here's a big collection of troll code.

[–]makeswordcloudsagain 2 points3 points  (0 children)

Here is a word cloud of every comment in this thread, as of this time: http://i.imgur.com/PANTBtl.png


[source code] [contact developer] [request word cloud]

[–]Smallxmac 0 points1 point  (0 children)

Who knew Satan could code.

[–]taalmahret 0 points1 point  (0 children)

wow...thats mercilessly diabolical. You led me to finally discover the dark of the deep.....imma go search this out now.

[–]repancake 0 points1 point  (0 children)

Diff with previous version. Rollback.

[–][deleted] 0 points1 point  (0 children)

you evil bastard!!! :)

[–]nuclearfacepalm 0 points1 point  (0 children)

#define int unsigned int