Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C modulo operation rant

Name: OnePissedCustomer 2007-03-01 7:21 ID:Sj4eeSQv

So, the other day I was writing a loop that would index over a field backwards.
I had:
FRAME Frame_Tbl[MAX_FRAME]
int index = some value less than MAX_FRAME and greater than 0

I though it would be really clever to go index = (index - 1)%MAX_FRAME because I was, at that point, convinced that -1 % MAX_FRAME was equal to MAX_FRAME - 1. Ran the program. Segmentation fault. What the fuck?

Turns out that the C modulo operation, against all common logic, returns NEGATIVE values for a % b when a<0 and b>0. Just...Wow...
[INSERT PYTHON/PERL/RUBY/PHP/JAVA FANBOYISM HERE]

Name: Anonymous 2007-03-01 8:29 ID:DdhaVQmT

Yeah, it's retarded, but
less than MAX_FRAME and greater than 0
would be index % (MAX_FRAME-1) + 1.

Name: Anonymous 2007-03-01 9:22 ID:Heaven

Never use % on negative values in C. The result may vary between compilers and architectures. Also, don't be "clever" when writing code unless you're in a programming competition where the only thing that matters is code speed.

Name: Anonymous 2007-03-02 5:12 ID:KmGbwEwq

>>3
That is why I prefer high level languages. They allow me to use my "clever ideas" and creative thinking. And keep me from becoming a code monkey.

Name: Anonymous 2007-03-02 5:29 ID:ceuyxTVW

at the same time >>4 your clever code in those languages make no difference at all.

Name: Anonymous 2007-03-02 6:04 ID:sd5nP4f0

>>4 has obviously never had to maintain code written by someone else who likes to use "clever" ideas

Name: Anonymous 2007-03-02 6:58 ID:F8jqCpE4

foldl motherfuckers!

Name: Anonymous 2007-03-03 7:41 ID:ruVKY6g7

When programming professionally, "clever" is another word for "expensive".  Expensive to write, expensive to test, and expensive to maintain.

Name: Anonymous 2007-03-03 8:10 ID:dOFwMnw/

>>8
Good thing I only program on a hobby basis then.

Wait...

That's kind of sad :(

Name: Anonymous 2009-01-14 13:30

JAVA fails

Name: Anonymous 2009-03-06 5:46


The A implementation of   this Suppose a   is some kind   of stupid nigger   go to hell   EAT SHIT DIE   AND SUCK MY.

Name: Anonymous 2010-06-25 14:53

WARNING: NECRO POST

Name: Anonymous 2010-11-15 12:08

Name: Sgt.Kabu䙼kiman䢫㲼 2012-05-28 22:52

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List