Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
mr_jim
Oct 30, 2006

OUT OF THE DARK

tripwire posted:

Ulillillia released the source code for a program he designed in C which converts the sampling rate of PCM wave files.
Check it out:
http://www.ulillillia.us/files/WAVFileSampleRateGeneratorV2Source.zip


I looked at the code. I think he's Rainman. But it does compile with only a few warnings on linux, and doesn't crash when run. :downs:

code:
$ wc -l WAV\ file\ sample\ rate\ generator.c
1189 WAV file sample rate generator.c

$ gcc -Wall -lm WAV\ file\ sample\ rate\ generator.c
WAV file sample rate generator.c: In function ‘CreateOutputFiles’:
WAV file sample rate generator.c:684: warning: too many arguments for format
WAV file sample rate generator.c:838: warning: too many arguments for format
WAV file sample rate generator.c:1190:2: warning: no newline at end of file

<- After running... ->

$ ls
a.out
C:\My Documents\Songs for MP3\source files\to convert\16K files\050000 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\16K files\051200 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\16K files\055000 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\16K files\060000 Final Fantasy 6 World.wav

<- snip about 80 lines ->

C:\My Documents\Songs for MP3\source files\to convert\48K files\130000 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\48K files\133333 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\48K files\140000 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\48K files\141421 Final Fantasy 6 World.wav
C:\My Documents\Songs for MP3\source files\to convert\48K files\150000 Final Fantasy 6 World.wav
WAV file sample rate generator.c
Edit:

code:
int main()
{
	// main function, required with C, but used to call the related functions
	Settings(); // gets some settings set
	CreateOutputFiles(); // initiate the loop for creating the output files copying 
comment wrapping mine ->     // the data above changing the 8 bytes as needed
	
	return 0; // function must return something, usually 0
}
This is all the way at the bottom of the file. I'm guessing from that first comment that C isn't his first language, so he has no clue about good style. Hence the three pages of global variable declarations at the top of the file, and the 560 line void FindNextSampleRate() function.

mr_jim fucked around with this message at 05:48 on Dec 22, 2008

Adbot
ADBOT LOVES YOU

mr_jim
Oct 30, 2006

OUT OF THE DARK

Whatever makes him happy, I guess. I spent about a half hour reading his site and now I'm all :psyduck:

mr_jim
Oct 30, 2006

OUT OF THE DARK

Rottbott posted:

There are several functions over 3000 lines in the C codebase I work with.

But do they all have a "void funct(void)" type signature and do all their interaction though global variables?

mr_jim
Oct 30, 2006

OUT OF THE DARK

That Turkey Story posted:

Metaprogram that poo poo, bitch.

Quotin' dis. It would take some pretty unusual circumstances for me to feel comfortable having a 3000 line function without trying to factor out any of it into helper functions.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Factor Mystic posted:

code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
Whoops :ohno:

It took me a while to notice the problem with that code. I kept thinking "Well, it looks reasonable..."
Edge-case bugs suck, but you'd think they would have done some testing of the leap-year handling code before release.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Vanadium posted:

I kept trying to figure out how it would arrive at a wrong answer and could not come up with anything. :shobon:


If you're still curious, the error occurs when days == 366, as in New Years Eve on a leap year. The "if (days > 366)" statement doesn't get executed, so days never gets decremented. But the condition for the while loop (days > 365) is still true, so an infinite loop occurs.

It's a piece of code that looks ok at first glance, but it's really a time bomb. Luckily, in this case, it just caused minor inconvenience.

mr_jim
Oct 30, 2006

OUT OF THE DARK

From http://www.jgc.org/blog/2009/01/single-line-assignment-filled-with-epic.html:

code:
dir.permissions = 666; // Make the file writable
There are so many kinds of bad right there.

mr_jim
Oct 30, 2006

OUT OF THE DARK

terminatusx posted:

Little known fact, setting a file's permissions to 666 gives read and write access to everyone, and allows Satan to execute YOU. in hell.
...with a trident.

Actually, I think the Satan thing comes up when you set a directory's permissions to 666 decimal instead of 0666 octal. With no user intervention. And then put in a misleading comment.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Mill Town posted:

Apparently XOR is defined on strings in PHP. :psyduck:
http://ca.php.net/manual/en/language.operators.bitwise.php

:wtc:

How is that supposed to be useful?

mr_jim
Oct 30, 2006

OUT OF THE DARK

shrughes posted:

Why, so you can encrypt passwords with xor encryption!

Encryption occurred to me after posting; I've been reading up on stream ciphers lately. I guess the context it was being used in threw me off.

code:
$todayDate = $todayDate ^ "pippopippopippo";
I have no idea what this is supposed to achieve.

mr_jim
Oct 30, 2006

OUT OF THE DARK

I'm pretty sure FeloniousDrunk is right about it generating session keys. That didn't occur to me because a) I'm not a web programmer, and b) "pippopippopippo" is odd enough that I thought it must have some significance beyond obfuscating the time stamp. :downs: I'm retarded.

mr_jim
Oct 30, 2006

OUT OF THE DARK

UberJumper posted:


code:
/*
	JAVA IS STUPID, LET THIS BE A WARNING TO ANYONE!!!

	I MADE 9 WRAPPERS ENDING WITH LETTERS, SINCE JAVA cant make classes right.
	THEY ARE ALL THE SMAE!!!!!!
	
	JAVA is hirrable because I want to make 50 classes at once! It cant let me.
	This fails WHY? With C++ it would work.
	
	for(z = 0; z <= 100; z++)
	{
		WrapperFIXED_ + z z = new WrapperFIXED_ + z;
	}

	WHY IS THIS SO HARD??!@?! If i want 50 clases i need to be able to do it
	better. 
*/
:suicide:

Please someone tell me what the gently caress he is rambling about?

It looks like he's confusing classes and objects. He wants 50 objects of the same class, but he thinks classes and objects are the same thing so he makes 50 identical classes instead.

Unless he's gotten the class/object distinction right in the rest of the code, in which case I have no clue what he's going on about. :psyduck:

Also, JAVA is hirrable. :downs:

mr_jim
Oct 30, 2006

OUT OF THE DARK

Lexical Unit posted:

code:
sprintf (cmd, "rsh %s /bin/rm -rf %s/%s &", host, path, file);

Wow. How has this not blown up in your face yet?

At the very least an exception could be raised if not all of the environment variables are set.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Kelson posted:

code:
sprintf (cmd, "rsh %s /bin/rm -rf %s/%s &", host, path, file);
I was more worried about someone modifying host, path, or file to include ";echo hackAccount::0:0::/root:/bin/bash >> /etc/passwd"

I really hope that the program is running under an unprivileged account, but that might be expecting too much.

mr_jim
Oct 30, 2006

OUT OF THE DARK

A friend just told me about a guy he works with who prefaces every function name with his user name, as in:

code:
userName_funcName();

mr_jim
Oct 30, 2006

OUT OF THE DARK

Spell posted:

That's great when you want to call a function you have to remember who wrote it. Then you know who to blame when it fails.

I told my friend that it was so he'd know who to punch in the face for not knowing about the "blame" feature of most SCMs.

mr_jim
Oct 30, 2006

OUT OF THE DARK

PrBacterio posted:

... I don't really see how that makes it any more or less bad than any other case of using hungarian notation? :confused:

If you use it privately, you're only hurting yourself, I guess.

mr_jim
Oct 30, 2006

OUT OF THE DARK

This is paraphrased from a bit-array library that I'm working on.

code:
#define array_size(bits) (((bits) - 1) / (sizeof(unsigned int) * 8) + 1)
Any guesses for what array_size(0) returns?

mr_jim
Oct 30, 2006

OUT OF THE DARK

Nope. Here's a hint: it involves the C standard's arithmetic conversion rules.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Mill Town posted:

I was guessing 0 due to integer division roundoff. Boy was I wrong!

gcc always truncates towards 0, but the C89 standard doesn't mandate that (C99 apparently does). That would make the expected result 1. That's not really what I wanted, but it's not as bad as what actually happens.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Unless you're planning on using the result of sizeof() in expression with a signed type, you don't need a cast. And you shouldn't cast it to a signed type unless you're sure it won't overflow, as in this case. sizeof(unsigned int) isn't likely to be out of range for an int. ;)

gcc is following the standard by converting both operands to an unsigned type here, but it caught me by surprise.

By the way, -Wconversion will make gcc warn you when it performs a conversion that can change signs. I wasn't using that and couldn't figure out why I was getting a sudden jump in memory usage during some unit tests.

Edit:

My solution didn't involve a cast either. I just explicitly check for negative or 0 values. This prevents the overflow problem and makes sure array_size(0) return 0 instead of 1.

code:
#define array_size(bits) (bits <= 0 ? 0 : (((bits) - 1) / (sizeof(unsigned int) * CHAR_BIT) + 1)

mr_jim fucked around with this message at 21:58 on Jun 2, 2009

mr_jim
Oct 30, 2006

OUT OF THE DARK

CanSpice posted:

code:
/* not really sure what I'm doing here... */

At least they're being honest.

mr_jim
Oct 30, 2006

OUT OF THE DARK

RoadCrewWorker posted:

Hm, lets google this...

Well, im convinced!

Christ, that's terrible even for single-point-of-return advocates. Usually they'll stick with creating extraneous variables for return values. Abusing a loop statement to avoid using a return statement is just awful.

mr_jim
Oct 30, 2006

OUT OF THE DARK

dwazegek posted:

Even if you subscribe to single point of return, that function doesn't make sense, a regular if-block would do.

That's what I was talking about, actually.

code:
int result = 0;
if (b == null) {
    ...
    result = ...;
} else {
    ...
    result = ...;
}

return result;
I think this is silly as well, but it's not as much of a horror as the "do { ... } while(false)" thing.

mr_jim
Oct 30, 2006

OUT OF THE DARK

RussianManiac posted:

code:
do {
    if (b == null) { // abort
      break;
    }
     ... // do something useful
} while (false);

... // cleanup
I have seen examples of this in a source code in a large Silicon Valley company which I won't name.

I don't think it is an example of coding horror, but it does look weird the first time you see it.

Someone posted it a couple of pages ago.

http://forums.somethingawful.com/showthread.php?threadid=2803713&perpage=40&pagenumber=66#post363641531

mr_jim
Oct 30, 2006

OUT OF THE DARK

Avenging Dentist posted:

Wow, and I was thinking about replying "that's not really that bad, it's useful when you have to implement multi-statement macros in C". :barf:

I thought you were joking. Then I read this: http://c-faq.com/cpp/multistmt.html

:barf: indeed.


But using it to avoid an early return, or a goto to cleanup code? That's just silly.

mr_jim fucked around with this message at 02:20 on Jul 27, 2009

mr_jim
Oct 30, 2006

OUT OF THE DARK

RussianManiac posted:

Better use that technique than goto. Avoiding early return is stupid tho, i agree.

Why?

code:
if (b == null) goto CLEANUP;

// Do something useful

CLEANUP:

// Cleanup

How is that wrong?

mr_jim
Oct 30, 2006

OUT OF THE DARK

Zakalwe posted:

It's not. Some people have "OMG goto is evil" beaten into them by idiots who should know better.

They are half right, in that if there is a control statement that clearly expresses what you want to accomplish, you should use it.

I figured out what bugs me about this code.

code:
do {
    if (b == null) { // abort
      break;
    }
     ... // do something useful
} while (false);

... // cleanup
That break statement is a goto. But to make it look less like a goto, it is wrapped inside a loop statement that has been abused into unconditionally executing a list of statements exactly once. That is the opposite of clarity.

If your language supports try/finally, then using that is a better choice because it makes your intent clear. In C, a goto is the next best thing.

mr_jim fucked around with this message at 04:06 on Jul 27, 2009

mr_jim
Oct 30, 2006

OUT OF THE DARK

dancavallaro posted:

Ummmmm duh didn't you know that a famous computer scientist said gotos are bad? So obviously we should never use them. And also Knuth said we should never do optimizations because they're evil.

You're right. I fixed the code to avoid gotos, ugly do/while hacks, and still have a single point of exit!

code:
int is_it_safe;
if (b == null) {
    is_it_safe = 0;
} else {
    is_it_safe = 1;
}

if (is_it_safe) {
    // do stuff
}

// cleanup
:downs:

mr_jim
Oct 30, 2006

OUT OF THE DARK

porkfactor posted:

I feel like I've spent my life patching other peoples idiocy. I've seen some horrors, but absolutely nothing could prepare me for this

code:
class MyStringClassIterator;

class MyStringClass {
  ...

  typedef MyStringClassIterator iterator;
  typedef const MyStringClassIterator const_iterator;
  ...
}
It's a good thing the coder considered that referential parameters are fundamentally wrong, (to the extent that his disaproval of them was written into the coding standard*), else he might have one day realised what he had created here.


*yeah, that part got dropped the very moment the opportunity arose.

I don't have a lot of C++ experience. What's the horror here?

mr_jim
Oct 30, 2006

OUT OF THE DARK

OK, I get it now. It's "const pointer" vs "pointer to const" generalized to C++ iterators.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Scaevolus posted:

10:33:11 < MononcQc> This has to come close to balls next to a cliff in terms of analogies: http://groups.google.com/group/comp.lang.scheme/msg/e7431893c4da4bbf?hl=en

Hi,
I understand continuations as taking a photo of what is happening, a special photo that you can use afterwards to go "to the past" where the photo was taking.
Continuations are very good on daily work, because you can take a photo (capture the *current* continuation) while you're having beers at the bar with your friends. If your daily work is boring you can always get your photo from your pocket and go instantly back to the bar with beers and friends. Great!!
Furthermore, when you take your photo (capture the *current* continuation) you pass a procedure to your camera (you *call* this procedure with the *current* continuation). The value returned by this procedure is then substituted in the very place you took your photo.
So continuations are a way to "return values from the future too" (you *call* a procedure with the *current* photo).
And this is fantastic!!
Imagine, for instance, that you're having beers with your friends at the bar. You then capture the current, comfortable, beer&friends continuation (take your photo) and say "let's capture a continuation here, and let's call a procedure that seeks what the winning lottery number is".
You then go to work, and you find in google what the winning lottery number is. You then pass this value (the value returned by the procedure) to the photo (the captured continuation). As a consequence you go immediately back to the bar... knowing what the winning lottery number is going to be!!
Now, isn't this fantasic? You just can win the lottery while being at the bar with beers and friends!! You won't have to go back to work again if you don't want to!!
In real life continuations don't exist (I wish they existed!), but while working with Scheme you can use continuations for doing some sort of things:
- Take a photo before seeking for something in a list. Pass a procedure to the photo that seeks for something in the list. (Looping).
- Iterate over all possible solutions to a problem. For each possible solution take a photo and pass it a procedure that sees if the solution is acceptable or not (backtracking).
- Perform a task. In the middle of the task take a photo and pass it a procedure that does another task (multitasking).
For examples of real-life usages of continuations you can take a look at [1], that contains "Design Patterns" explaining common situations for using continuations.
Cheers,
Antonio

Time works the same way.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Otto Skorzeny posted:

-1 x -1= +1 is WRONG, it is academic stupidity and is evil. The educated stupid should acknowledge the natural antipodes of +1 x +1 = +1 and -1 x -1 = -1 exist as plus and minus values of opposite creation - depicted by opposite sexes and opposite hemispheres.

You have opposite brains to think opposite, but Big Brother icepick academic lobotomy has destroyed your mentality to think opposite of the evil singularity you are taught.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Zombywuf posted:

This is the closest I've seen to managing to parody timecube. However it still falls short. Is it actually possible to parody it?

That's a copy/paste from timecube.com.

mr_jim
Oct 30, 2006

OUT OF THE DARK

I don't know if this has been mentioned yet, but I just found out that according to the C standard, a[i] and i[a] are equivalent.

This function:
code:
int foo() {
    char c1 = "abcd"[2];
    char c2 = 2["abcd"];
    return c1 == c2;
}
compiles without errors or warnings, and returns 1.

Having never entered any obfuscated C contests, I was unaware of this.

edit:
code:
#include <stdio.h>
int main() {
    int i, c;
    for (i=0; (c = i["Hello, World!\n"]); i++) {
        putchar(c);
    }
    return 0;
}
:cool:

mr_jim fucked around with this message at 01:30 on Sep 18, 2009

mr_jim
Oct 30, 2006

OUT OF THE DARK

quote:

The Committee found no reason to disallow the symmetry that permits a[i] to be written as i[a].

From the "Rationale for ANSI C" (http://www.lysator.liu.se/c/rat/c3.html#3-3-2-1)

Personally, I don't see any reason for array subscripting to be commutative. But I wasn't on the committee.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Avenging Dentist posted:

Guys, the a[i] / i[a] equivalence is just because the standard defines a[i] as being equivalent to *(a+i).

I understand why they're equivalent. But I also think one could make a case for disallowing that particular syntax, even though it would make writing a compiler slightly more complex. Then again, I doubt "i[a]" ever gets used in real code, so what the harm?

mr_jim
Oct 30, 2006

OUT OF THE DARK

ShoulderDaemon posted:

But if we take S as the union set, then we have to admit + as a partial function, as (pointer + pointer) is a nonsense operation.

With casting, anything is possible.

code:
    char *a = malloc(1);
    char *b = malloc(1);

    char *c = (char*)((long)a + (long)b);
Though I wouldn't try dereferencing the resulting pointer, so yes, it is a nonsense operation. And casting is probably cheating.

mr_jim
Oct 30, 2006

OUT OF THE DARK

Vexed Morgan posted:

Hey, you might call the Jargon File bogus, but ESR made it accessible to Aunt Tillie.

Thanks to the appendices, I learned that I'm likely to be politically moderate to neo-conservative, and my love of "exotic beers" like Guinness is influenced by Linus Torvalds.

Also, "anti-idiotarianism" is a very common term. Thanks ESR! :allears:

Adbot
ADBOT LOVES YOU

mr_jim
Oct 30, 2006

OUT OF THE DARK

I am adding that to every program I write, just in case.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply