LoginLogin

Array PUSHing Faulty

Root / Programming Questions / [.]

MariominerCreated:
So to show you what I mean, think what should happen when you run the following code:
DIM T0[0]
DIM T1[0]
DIM T2[0]
DIM T3[0]
DIM EMPTY[0]

DEF PUSHING
PUSH T0,1
PUSH T1,2
PUSH T2,3
PUSH T3,4
END

T0=EMPTY
T1=EMPTY
T2=EMPTY
T3=EMPTY
PUSHING
PUSHING
?LEN(T0)
The output should be 2, right? After all, you pushed data into the array T0 twice. But the output is 8. WHY?!?!?! Well, the contents of T0 are [1,2,3,4,1,2,3,4]. But it should be [1,2]. Because the array is set to be EMPTY, it pushes everything into EMPTY and everything is affected. See, that would make sense if this were C and it used pointers, but this is BASIC. Setting all arrays to EMPTY shouldn't matter because it's copying EMPTY into these arrays, not linking them. Correct me if I'm wrong, but this is ridiculous. I unfortunately had to encounter this in the wild, not the lab, so I have to go and change code to go around this.

This is not an issue with PUSH, that's doing its job just perfectly.
Setting all arrays to EMPTY shouldn't matter because it's copying EMPTY into these arrays, not linking them. Correct me if I'm wrong, but this is ridiculous.
Nope, it's not copying. It's not ridiculous either. Perhaps unexpected, for BASIC, but not ridiculous.

I think that it shouldn't. But you did "correct me if I'm wrong". I wasn't 100% sure if it was a bug, as under certain circumstances it could occur, but I believe that this should not be one of those occasions. This doesn't happen with integers, so why arrays? I understand that arrays (in the basic level) are pointers to data, but in BASIC that behavior just doesn't make sense. I may be wrong again though, who knows? XP

SmileBASIC is simply moving away from traditional BASIC practices in many ways.

That's true... In this sense I wish that it worked the expected way, but it does make sense in different senses and languages. IDK and IDC what happens to this thread, if it's not a bug. I wish that it was a little more consistent though, with other types of variables acting differently. I guess that arrays just got special treatment :3 Now This thread needs to be called "I hate pointers that I can't use" If you could call it that.

Notice that string variables also hold references to the string values they hold, and straight assignment copies those references.

I understand that they are references and all that, but I still think that it's not the best design choice. Sure, it makes sense, but why have it in the first place? That was a rhetorical question, don't answer that.

How would we share arrays and those behemoth 256-byte strings without this?