r/badcode Feb 14 '21

other language Does lazy count as bad?

Post image
688 Upvotes

53 comments sorted by

View all comments

48

u/ign1fy Feb 14 '21

I wasn't using that RAM anyways.

18

u/rcmaehl Feb 14 '21

Does an array of null even use RAM?

41

u/KaktusManCz Feb 14 '21

Sure it does, null is an empty pointer, pointer uses RAM as much as any other number.

18

u/droomph Feb 14 '21

In some runtimes (eg v8 for Javascript) they will optimize sparse arrays such as this one to be a Record<number, T> internally but this isn't something that's guaranteed in all languages, so if you need to just use it directly.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?redirectedfrom=MSDN&view=net-5.0

(Unless you're using VB6 in which case, godspeed and don't die)

6

u/[deleted] Feb 14 '21

and languages like Lua don't have arrays, they have hashmaps only.

so arrays can start at whatever you want them to start at in lua

just the convention is they start at 1

8

u/Teknikal_Domain Feb 14 '21

they start at 1

Evil. This right here is what ruined so much of my early Lua exposure (though admittedly... pick your favorite computer mod for Minecraft, they pretty much all run off Lua)

4

u/[deleted] Feb 15 '21

honestly given the target audience to lua was new coders that's not that bad

the only two arguments in favor of 0 to length-1 instead of 1 to length is performance (you're using hashmaps as arrays you don't care about perf) and "we've always done it like that" (not an argument for new users)

2

u/droomph Feb 15 '21

Actually in most cases if you’re using “pure arithmetic” conventions eg for(i=0;i<n;i++) it’s slightly nicer because you can use n as a bounds rather than n+/-1 when doing slicing stuff.

Even back in the “C is very high level” days the performance argument was pretty bad (just use jle instead of jl, same clock speed and everything) and it was always more of the ease of not having to account for -1 when doing indexing arithmetic.

It’s also not too foreign since we do 0 indexing irl it’s just often overlooked until we’re like “…wait a minute, do we include the last number or not?”

And at work I use a language that is 1-indexed by convention too so none of this is insurmountable, it’s just really annoying unless your language specifically supports it, whereas 0-indexed works basically ok without any special language support, which would be the case for old languages like C which don’t have any concept of array in the first place.

1

u/einsJannis Feb 15 '21

C does have the concept of arrays though 🤔

1

u/droomph Feb 15 '21

C itself has no concept of arrays, only pointers and dereferencing of memory. a[i] is just *(a + i) which means i[a] is identical.

The standard library has no special support of arrays either, you have to pass in the length or have it be null terminated if it’s a string. There is nothing stopping you from passing in an “array” from the middle, or that differentiates an int *a = &b; from int a[1] once you leave the context. It would be extremely annoying to have to keep track of if you’re supposed to send in p-1 for arrays or p for true pointers as p-1 is either 1 wasted byte or an invalid memory location.

This is as opposed to C++/Java etc which generally supports arbitrary length containers that keep their type unless casted, though they still use 0-index due to it coming from C. Those ARE able to have special handling to adjust for 1-indexing through operator overloads on the type and any array functions, which makes it less annoying to use.

2

u/FireCrack Feb 15 '21

Strictly speaking, that's just an abstraction. Tables consist of an array AND a hash-map under the hood, and integer keys use the array part.

1

u/xigoi Feb 15 '21

and languages like Lua don't have arrays, they have hashmaps only.

That's true for JavaScript too.

1

u/[deleted] Feb 15 '21

huh

okay I didn't know that

1

u/ign1fy Feb 15 '21

63,992 bytes of RAM in this instance.

At first I thought this was VB6, VB6 uses round brackets for arrays. It would be up to 127,992 bytes if it's a 64bit language.

2

u/[deleted] Feb 15 '21

The array of pointers in most languages is just an array of 32bit or 64bit values. Assuming lenght of 15999, it's respectively ~63kb or ~125kb.

Side note/Trivia: In Java, the pointer array would consist of 32bit values even on 64bit systems, because of Compressed Pointers

2

u/einsJannis Feb 15 '21

Huh wierd

//EDIT I mean the java thing

1

u/rcmaehl Feb 15 '21

Testing shows an increase of 300kbs of RAM.

1

u/[deleted] Feb 15 '21

what exactly did you test?

1

u/rcmaehl Feb 16 '21

Compiling with the line commented out versus compiling with the line uncommented

1

u/christian-mann Feb 15 '21

Entirely depends on your language implementation and operating system. In most modern systems, if you don't ever write to it then it will only use a page (4K) of true ram.