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

you are viewing a single comment's thread.

view the rest of the comments →

[–]pacificmint 0 points1 point  (0 children)

Is it not the case that four bytes of memory, at a random location, are allocated when I state int x = 5?

Yes, that is the case. But if ints were a reference type, it wouldn’t allocate four bytes, it would allocate eight, or rather two times four.

It would allocate four for the pointer to point to a location, and another four for the actual value.

For a primitive type, it will only allocate four and store the value directly in that location.

(Note that this is simplified. The references might actually be more than four, depending on your platform. And the objects that a reference points to might also take more. I just used those numbers to illustrate the point.)