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
Rothon
Jan 4, 2012

Suspicious Dish posted:

I have one case where I use goto; matched with a label, out:

code:
static Texture *
load_texture_from_name (char *name)
{
  Texture *texture = NULL;
  char *filename;
  FILE *fp;
  png_structp png_ptr = NULL;
  png_infop info_ptr = NULL;

  filename = asprintf (TEXTURE_DIR "%s.png", name);
  fp = fopen (filename, "rb");

  png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if (png_ptr == NULL)
    goto out;

  info_ptr = png_create_info_struct (png_ptr);
  if (info_ptr == NULL)
    goto out;

  if (setjmp (png_jumpbuf (png_ptr))
    goto out;

  /* Not going to include full IO stuff, consult libpng docs */

out:
  free (filename);
  fclose (fp);
  png_destroy_read_struct (png_ptr, info_ptr, NULL);

  return texture;
}
That is, memory management in C sucks.

Another nice feature is that execution falls through labels nicely, so you can do more complicated cleanup like this.

Adbot
ADBOT LOVES YOU

Rothon
Jan 4, 2012
I like the way Go handles switch statements. It's sort of the reverse of C; breaking at the end of a case is the default, and there is a fallthrough keyword if you actually want to continue to the next case.

Rothon
Jan 4, 2012

seiken posted:

Obviously there are many faster ways to do it but missed optimizations like that probably aren't nearly so much of a horror as the other fundamental ones present (like huge sections of pointless code). You may as well say it's a horror not to implement AKS in baby's first python function.

Actually implementing AKS would be a horror. It's extraordinarily slow and complex. Everyone just uses Miller or Miller-Rabin instead.

Rothon
Jan 4, 2012
The *Impl "pattern" is also used in Java to help out with unit testing. The *Impl class isn't actually the only implementation: tests define mock implementations and use those to avoid dependencies on other parts of the application. You need the interface because the mock libraries use java.lang.reflect.Proxy under the hood which can only implement interfaces, not extend classes.

Rothon
Jan 4, 2012

Arcsech posted:

Why are VLAs bad? I haven't really run into them before, so honest question.

It makes it really easy to run out of stack if you aren't careful.

Rothon
Jan 4, 2012

zergstain posted:

Most of the issues I fix aren't like that, but that one is pretty common. They also cast the free() argument to char *, what would make someone think this is necessary?

I believe that in the pre ANSI-C days, void * wasn't a type so free took a char * which you'd need to manually cast to. It's the same reason that some people still do this cast
code:
struct foo *foos = (struct foos *) malloc(10 * sizeof(struct foo));

Rothon
Jan 4, 2012

nielsm posted:

So an infinite recursion takes how long to crash?

Not very long:

code:
~ > cat test.go
package main

func loop() {
	loop()
}

func main() {
	loop()
}
~ > time go run test.go
fatal error: out of memory (stackcacherefill)

goroutine 1 [running]:
[fp=0x7f9def007f58] runtime.malg(0x400c18)
	/usr/lib/go/src/pkg/runtime/proc.c:1451 +0xc3
[fp=0x7f9def007f60] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f68] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f70] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f78] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f80] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f88] main.loop()
	/home/sfackler/test.go:4 +0x18
[fp=0x7f9def007f90] main.main()
	/home/sfackler/test.go:8 +0x18
[fp=0x7f9def007fb8] runtime.main()
	/usr/lib/go/src/pkg/runtime/proc.c:182 +0x92
[fp=0x7f9def007fc0] runtime.goexit()
	/usr/lib/go/src/pkg/runtime/proc.c:1223

goroutine 2 [runnable]:
exit status 2
Command exited with non-zero status 1
0.56user 1.07system 0:01.66elapsed 98%CPU (0avgtext+0avgdata 3628716maxresident)k
11792inputs+0outputs (31major+911213minor)pagefaults 0swaps

Rothon
Jan 4, 2012

Volte posted:

What, the exit(1) is only for when an impossible thing happens and it aborts the program.
This is almost literally true but it was during the regrading that the TA made the statement about green comments and I still got a poo poo mark. :smith:

It would be more appropriate to assert(3) or abort(3) there.

Rothon fucked around with this message at 17:53 on Jul 27, 2013

Rothon
Jan 4, 2012

Doctor w-rw-rw- posted:

Speaking of security shame, here's a syllabus for Berkeley's (computer science) security class this semester:
http://people.ischool.berkeley.edu/~tygar/161/161.2013.08.29.syllabus.pdf

This particular professor is a special kind of bad. In summary:
  • Late to a class? You get an F and leave.
  • Late to section? You get an F and leave. By the way, four sections have been dropped. If you have a schedule conflict, report it by September 2nd, or get an F and leave.
  • Unanticipated absence due to family emergency or medical reasons? Full documentation must be submitted on the same day, or get an F and leave.
  • Religious absence? Submit it by September 2nd, or get an F and leave.
  • Cell phone beeps? Hell, even just your watch? Get an F and leave.
  • Use any non-medical electronic device at all? Get an F and leave.
  • Don't have a book yet? Pay $50 and also pay out the nose extra to get it overnighted; there's an exam on Monday. Oh, and the second textbook hasn't been published yet. Too poor? Too bad.
  • Requesting a re-grade? *All* of your exams will be re-graded.
  • Need to use the bathroom during a quiz? No.
  • Need to use the bathroom during an exam? Your test will be taken from you and photographed, and you will be escorted to the restroom.
  • Suspect someone is looking at your exam sheet? Stand up and report it immediately during the exam.

That has to be some elaborate metaphor for overly burdensome security practices. It's just too nuts.

Adbot
ADBOT LOVES YOU

Rothon
Jan 4, 2012

Ender.uNF posted:

On a slightly different note, I've said it before but when I first saw that a message to nil in Objective-C does nothing and returns zero I thought it was the dumbest thing ever. Now I realize that's the way all languages should work. 99.9% of the time when some reference is null, I just want to ignore it and/or skip that section of code. It is extremely rare that I want to throw an exception, assert, crash, etc.

Yet in most languages, I have to constantly litter my code with if (dicks != null) { dicks.butts(); }


If you aren't going to do that, then all references should be non-Nullable unless they are Nullable<T>. At least then I would know.

The return value is only *usually* 0. If you call something that returns a struct by value that's larger than a register, the contents of it are undefined!

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