occs-compilers-2014-spring / discussion

A place for discussion
0 stars 0 forks source link

Assigning arrays? #8

Closed ikehz closed 10 years ago

ikehz commented 10 years ago

So, say I've got the following code:

void main(void) {
  int A[2];
  int B[10];
  A = B;
}

Allowed? Not allowed? It's definitely syntactically correct, and it's type-correct. Are we just giving people what they deserve if they do something like this?

peter-fogg commented 10 years ago

I talked with Ike about this today; in case anyone else is interested here's what we came up with.

Since A and B are both represented in the generated code as the address of A[0] and B[0], it's easy to generate code to do the assignment. The question in my mind is less "is this possible", and more "does this make sense". I'm of the opinion that it does, especially in this context:

int f(int A[]) {
  return A[0];
}

void main(void) {
  int B[4];
  write(f(B));
  writeln();
}

Here the call to f binds the value of B to the name A, which is something like an assignment. The code generated should certainly be the same.

Beyond that, there's the cheap explanation of "Bob never told us we can't".

ikehz commented 10 years ago

I talked with Bob about this today, and he said we should not allow assignments to arrays. The real issue here is global arrays:

int A[10];

void main(void) {
  int B[5];
  A = B;
}

What does A = B mean in this case? Are we copying data? We can't change the pointer to A, because A is a global variable.