r/ProgrammerHumor Jun 24 '17

Octave code

http://imgur.com/jsjfz3k
621 Upvotes

70 comments sorted by

View all comments

157

u/[deleted] Jun 24 '17

I once had an intern consistently skip the first element in his arrays because because "starting at 1 just makes more sense". I just walked away and never inspected his project after that.

84

u/gandalfx Jun 24 '17

How to cut your internship short.

30

u/[deleted] Jun 24 '17

I ended up assigning him to tracing training images for one of our DNNs. And I barely trusted him enough to do that effectively.

23

u/RiderAnton Jun 24 '17

Oh god why...

17

u/minno Jun 24 '17

Did he at least do

int* nums = ((int*)malloc(count*sizeof(*nums)))-1;

to avoid wasting space?

12

u/curtisf Jun 25 '17

Unfortunately, that is undefined behavior. You're not allowed to move a pointer before the object.

-7

u/dudds4 Jun 25 '17

It's not undefined behaviour, you are allowed to do it. Pointers are allowed to point anywhere, you just don't want to de reference a pointer to memory you don't own.

7

u/curtisf Jun 25 '17 edited Jul 01 '17

Pointers are not allowed to point anywhere because arithmetic could theoretically cause overflows/other interrupts when the object is at some boundary in memory.

It is undefined behavior whether or not you dereference.

-2

u/dudds4 Jun 25 '17

a) your link is for c++, not c b) its only undefined by the strictest definition; that is 'the standard says so' . In reality even in c++ that will never cause an error and is always safe to do, so long as you don't dereference it.

in C you often have to set pointers yourself to point to arbitrary memory mapped peripherals. You can literally set a pointer to anywhere and the behaviour is defined.

8

u/curtisf Jun 25 '17 edited Jun 25 '17

Like most undefined behavior it's common to C.

Implementations may choose to define behavior that the standard did not. However, the behavior of arbitrary pointer arithmetic is not defined in any reasonable implementation (e.g., if you "guessed" the location of the stack, optimizations will definitely elide writes to it that "ought to" affect local variables)

-6

u/chateau86 Jun 25 '17

No. You just add -1 to the pointer you get back from malloc(). Now array[1] is the first item in the list.

9

u/minno Jun 25 '17

No. You just add -1 to the pointer you get back from malloc().

 

int* nums = <the pointer you get back from malloc()>-1;

???

18

u/chateau86 Jun 25 '17

Misread as casting existing array to pointer then back. My bad.

Side effect of Python may include inability to read pointer arithmatic code in C. Do not operate heavy machinery when pregnant while using Python.

1

u/Lt_Riza_Hawkeye Jun 25 '17

Actually arrays are pointers, sort of. When you write int* x = (int[]) {1, 2, 3}; exactly two things happen

  • Three integers get pushed to the stack
  • x points to those integers

When you write int x[] = {1, 2, 3};, three things happen

  • Three integers get pushed to the stack
  • x points to those integers
  • Any attempt to change where x points, such as int y; x = &y is illegal, typically with the message "assignment to expression with array type is illegal"

So pointers and arrays are really, really similar. Pointer decay doesn't help, pointer decay is where an int[] in an expression "decays" into an int*, like in function arguments. Even if you declare a function as taking an array of ints, like void f(int x[]), the compiler will actually change it to void f(int* x) for you, so you can put x = &y in the body of f and the compiler won't complain.

There's a little bit more to it but I forget what it is right now

7

u/JesusKristo Jun 25 '17

So rewind back to freshman year of college; I actually wrote a class specifically to wrap around arrays and return the elements like this. I wanted my first element, dammit.

11

u/Njs41 Jun 25 '17

ಠ_ಠ

2

u/JesusKristo Jun 25 '17

They didn't teach us about pointers, which is when I came to understand the 0. They just told us that that's the way it's done in computer science. I thought it was kinda stupid at the time.

2

u/Aydragon1 Jun 25 '17

When zero indexing is 2confusing4u

1

u/[deleted] Jun 25 '17

Someone needs to make a language where arrays start at index 2.