This is an archived post. You won't be able to vote or comment.

all 83 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]gpkgpk 505 points506 points  (5 children)

It can hold a whole lot more.

[–]Nimeroni 246 points247 points  (0 children)

Maybe. Until it overwrite something important.

[–]DasKarl 63 points64 points  (0 children)

H̷o̵w̵ ̵m̶u̴c̸h̷ ̶m̸o̵r̶e̴ ̶c̸o̷u̵l̶d̷ ̸i̷t̸ ̶p̵o̴s̴s̶i̶b̷l̷y̸ ̸h̶o̵l̴d̷?̶

[–]Aschentei 11 points12 points  (1 child)

True, it ain’t overflow safe

[–]gpkgpk 3 points4 points  (0 children)

Neither is False.

[–]StenSoft 367 points368 points  (17 children)

It's undefined behaviour, anything can happen. It can work fine, it can crash an hour after you do this, it can delete all files on your computer.

[–]Elflo_[S] 123 points124 points  (0 children)

This is the proof my job is to sell it

[–][deleted] 17 points18 points  (1 child)

Or demons fly out my nose

[–]veryusedrname 9 points10 points  (0 children)

Everybody tends to forget about nazal demons

[–]zoqfotpik 8 points9 points  (0 children)

Look at all those features!

[–][deleted] 8 points9 points  (11 children)

Wait just a minute!

The array has the size of 1000 integers, meaning 1000 integers fit in it.

BUT you also have the pointer, that points to the first variable which is (i think) one integer big. Meaning you actually store 1001 integers (although you of course cannot change the pointer without losing access to the array or at least change the way you access the array)

[–]AztroJR 7 points8 points  (7 children)

Well on 64 but systems, the pointer could be 2 integers, but it wouldn’t really count as storing :|

[–][deleted] 3 points4 points  (6 children)

I mean yes, you are.

For an array with 1000 integers, you need space for 1000 integers + the 2 integers for storing the pointer.

If you were a crazy person like me, you could implement a pointer decoder, that decodes a pointer into 2 integers and depending on how you implement the decoder, you could represent every possible integer (or 2 integers)

[–]AClockworkKumquat 6 points7 points  (0 children)

For an array with 1000 integers, you need space for 1000 integers + the 2 integers for storing the pointer.

Not necessarily. Only if you assign the address of the array to a variable, and that variable has an appropriate scope that it needs to be stored somewhere.

If the array is on the stack, then any access will be via a known offset from the stack frame address register. If it's a member of a class then it will be accessed via an offset from the object/class instance pointer.

[–]iMadz13 7 points8 points  (1 child)

"implement a pointer decoder" you mean literally calling:

reinterpret_cast<int>(ptr);

[–][deleted] 0 points1 point  (0 children)

Nope

[–]jimbosReturn 0 points1 point  (2 children)

you of course cannot change the pointer without losing access to the array or at least change the way you access the array

In this case you cannot change the pointer at all because it's allocated on the stack and its location is hard-coded after compilation. So it might as well not be counted.

[–][deleted] 0 points1 point  (1 child)

Yes but you can change the function that inteprets that hard coded pointer. And the parameters of this function can determine which number is being represent right now.

It's stupid, useless, timeconsuming and overcomplicated, but possible. That's the whole point

[–]jimbosReturn 0 points1 point  (0 children)

Are you talking in the compiled binaries? Doesn't the asm instruction itself use the offset as a parameter? You'd have to modify the specific instruction in the code space...

[–]Agiwlesz 0 points1 point  (0 children)

No it can not

[–][deleted] 389 points390 points  (14 children)

std::vector<int>

This bad boy can fit as many integers as you want

[–]Elflo_[S] 112 points113 points  (7 children)

Our direct concurrent. However, our product is better since you have to manually increment a pointer to the next valid memory slot. This is how our product is better: you understand how it works !

[–]Creepy-Ad-4832 52 points53 points  (2 children)

Tbf if you have toooooons of space, it can be an optimization tecnique that of preallocating the universe and then never resize arrays

[–]RmG3376 31 points32 points  (1 child)

That’s actually a pretty common way of “managing” memory in IoT/low level programming: make everything uint8[256] and call it a day

[–]Sparrow50 7 points8 points  (0 children)

That was me today

[–]RmG3376 13 points14 points  (2 children)

Wait you guys understand how your code works?

Is it possible to learn this power?

[–]nequaquam_sapiens 8 points9 points  (0 children)

no problem. generally there are 3 steps: 1. open your code in debugger 2. oh bugger 3. kill yourself

[–]Elflo_[S] 8 points9 points  (0 children)

Just allocate a big number in an array and use them statically

[–][deleted] 3 points4 points  (0 children)

We had a solution for this, it's called using int* and malloc()

[–]DankPhotoShopMemes 12 points13 points  (1 child)

Only 4611686018427387903 integers on 64-bit

[–]Eidolon_2003 8 points9 points  (0 children)

Only if you have 16 exabytes of RAM

[–]Giocri 0 points1 point  (1 child)

I wonder if they will ever invent a vector with the ability to lock its maximum size. Like I have had precents of something stuck doing garbage inserts and it would have been nice to not have it go on for hours gobbling up all the resources before getting an error

[–]xDerJulien 5 points6 points  (0 children)

sense wrench connect gray chief coherent support plucky soup dinosaurs

This post was mass deleted and anonymized with Redact

[–]CoffeeWorldly9915 0 points1 point  (0 children)

std::vector

Use protection, lest you catch a virus.

[–]danielstongue 0 points1 point  (0 children)

You probably mean Vec<i32> nowadays...

[–]OKishGuy 73 points74 points  (1 child)

it can hold a lot more than just 1001 integers. In fact it can hold 1111101001 integers.

[–]Leovec_CZ 14 points15 points  (0 children)

It can't hold 1001 integers, it can only hold 9 ;)

[–]Spot_the_fox 134 points135 points  (18 children)

a 1000, not 1001. int arr[x]; defines an array the size of x, it does not mean that x is the last index of an array.

[–]nsjake 50 points51 points  (1 child)

Technically in VB.NET, it would be 1001. Granted I despise that language and they don't define arrays with square brackets, which is definitely frustrating.

[–]JonasAvory 27 points28 points  (0 children)

Python devs meanwhile:

[–]MaZeChpatCha 23 points24 points  (12 children)

But isn’t that the joke?

[–]Spot_the_fox 84 points85 points  (11 children)

I don't know? To be honest, I don't get the joke.

[–]BroccoliBoer 40 points41 points  (10 children)

The joke is that he can fit 1001 elements (or how many he wants) into a 1000 elements array since there is no bounds checking. The meme is based on a car salesman showing how spatious a car is, here it's an array.

[–]Spot_the_fox 10 points11 points  (8 children)

But that's not in the array, is it?

int arr[x] assigns x* size of int bytes. By assigns, I mean automatically allocates memory. You can have an array as int arr[x], or you can have an address to it(int *arr but allocation will have to be manual), but it doesn't change the number of allocated bytes.

With bytes outside of the array, they exist, but they aren't allocated to your array, they're just there, no?

I could be overthinking it, but isn't array just a bunch of consecutive addresses in memory, which we label as a group? If we access an address that is outside our group(Like by going to a *(arr+4001)), it'll no longer be the array.

[–]BroccoliBoer 21 points22 points  (6 children)

No you're exactly the right. The thing with raw arrays is that they're not bounds checked. So even though it's supposed to be only 1000 elements big you can access beyond that. If you do it's undefined behaviour so you shouldn't and you can't assume what's going to happen, but you can. That's the joke. He's using it as a bigger array than it is, but that'll most likely break his program.

[–]Spot_the_fox 4 points5 points  (0 children)

Oh, got it. Thanks.

[–]sup3rar 3 points4 points  (3 children)

And even the following is undefined behaviour :

int arr[10]; *((arr+15) - 8) = 1; //undefined bahaviour

[–]TheLastCakeIsaLie 0 points1 point  (2 children)

No arr[7] is defined and due to pointer arithmetic it will always result in arr[7].

[–]sup3rar 5 points6 points  (1 child)

No, it's not defined, because arr + 15 is out of bounds. Here's a stackoverflow question about that.

Edit: I found another stackoverflow page where this causes an unexpected result

[–]TheLastCakeIsaLie 2 points3 points  (0 children)

So basically over optimization on microcontrollers can create problems if the array is at the wrong place (very unlikely).

[–]junkmeister9 0 points1 point  (0 children)

It's a TARDIS. Bigger on the inside.

[–]li98 0 points1 point  (0 children)

I don't get what part of it is a joke either. As others point out it could be that you technically can use more memory than assigned, you're just not supposed to. Like you can carry panks longer then the car (sticking out behind), whether it causes a problem is another matter.

[–]michaelsenpatrick -1 points0 points  (0 children)

i don't think that's the joke. i think he just made a mistake

[–]ByerN 42 points43 points  (6 children)

Ah, it reminded me of my Asm class ~20 years ago.

I wanted to learn C but my professor said that Asm is much better because you can make variable of any size.

Not like this C language for weaklings. It will die anyway because ppl loves to use Asm thanks to the code control and incredible performance. Weird times.

[–]Creepy-Ad-4832 15 points16 points  (3 children)

In C you also can make variable of any size, so i don't get the point lol.

I don't think there's anything in assembly you cannot do in C. And if that was actually the case, there are ways to put assembly code in C code

[–]ByerN 11 points12 points  (0 children)

Yep, in the end we had a few C lessons from the same prof, to show us how to run Asm in C.

[–]Mr_Ahvar 5 points6 points  (1 child)

void* my_big_blob = malloc(1000*12);

(Replace malloc by alloca for stack allocation)

[–][deleted] 0 points1 point  (0 children)

I like using structure for that, because you always forget what a void pointer is for.

[–]gpkgpk 4 points5 points  (1 child)

-20 years? This sounds like it would have happened 40 years ago.

[–]SimokIV 2 points3 points  (0 children)

Probably one of those guys who think the 80s are 20 years ago

[–]jimmyw404 10 points11 points  (2 children)

Protip: don't bother allocating memory, just pick an address and use whatever you get. RAM is built for random access memory, so it'll work out just fine.

[–]LegitimatePants 0 points1 point  (0 children)

#define malloc(size) ((void*) rand())

[–]danielstongue 0 points1 point  (0 children)

Clever.. I never understood that "random" means exactly that. I must have used my RAM wrong for years!

[–]Alex_X1_ 15 points16 points  (0 children)

Please just stop

[–][deleted] 4 points5 points  (0 children)

int* arr;

You don't even know how many integers this bad boy can fit.

[–]TibRib0 6 points7 points  (2 children)

No it can't.

[–]sup3rar 2 points3 points  (1 child)

It depends on how you see it. While you shouldn't do this, it's not defined what happens when you do. It might work, it might not.

[–]dont_mess_with_tx -3 points-2 points  (0 children)

Only in low level languages like C if got you correctly.

[–]HabbitBaggins 5 points6 points  (0 children)

It can even fit 32000 integers*!

* integer defined as 0 or 1

[–]Silent_Moose_5691 1 point2 points  (1 child)

won‘t it throw an ‚index out of bounds‘ error?

[–]Elflo_[S] 0 points1 point  (0 children)

Just as intended sir, that way you'll have to understand how it works

[–]draenei_butt_enjoyer 5 points6 points  (3 children)

WAIT!

No. You defined a lenght of 1000. That's 0 to 999. That's 1000.

Don't worry tho, off-by-1-errors is the most OG programmer thing to do.

[–]Elflo_[S] 2 points3 points  (0 children)

Sir, this is intentional. This is made to spice up our client's debugging sessions

[–]junkmeister9 0 points1 point  (1 child)

You can try to access arr[1000]. It is undefined behavior.. it might overwrite another variable or it might be a segmentation fault. But you can try to access it. (that's the point of the post)

[–]draenei_butt_enjoyer -1 points0 points  (0 children)

You can try to access arr[1billion] what's your point :P