What other mental models do people use to think about variables and memory? I would like to hear about them.
How to Think About Variables in C
11–20 of 66 posts
Re: How to Think About Variables in C
#12What other mental models do people use to think about variables and memory? I would like to hear about them.
In Haskell: Variables? What state? Everything is puuuuuuuuure . In Python: Everything is an object (numbers, true/false values, strings, etc), some are mutable and some are not. Variables are temporary labels on objects (think of them as hard links). In Rust/C++: There are various types of boxes / smart pointers (shared, unique, heap, etc), and unsafe / raw pointers should be avoided when possible. In C: Not every va…
A void pointer has type "void* "; a function pointer also has some appropriate type.
Not every object has a type (e.g., a chunk of memory allocated by `malloc()`), but if "variable" means "object created by a declaration", then yes, every object has a type.
Re: How to Think About Variables in C
#13Earlier quoted context omitted.
[deleted]
My point is that pointer-or-not is irrelevant and muddies the issue. The problem is entirely that it's not an l-val.
The point I was trying to make is that &x would return a number not an assignable memory location. The reason it isn't an lvalue is because it doesn't return a memory address the way a variable assignment or dereference would.
I was probably a bit confusing in what I was trying to say.
Re: How to Think About Variables in C
#14Earlier quoted context omitted.
My point is that pointer-or-not is irrelevant and muddies the issue. The problem is entirely that it's not an l-val.
I can agree on that. The point I was trying to make is that &x would return a number not an assignable memory location. The reason it isn't an lvalue is because it doesn't return a memory address the way a variable assignment or dereference would. I was probably a bit confusing in what I was trying to say.
(x + 1) = 12
because (x+1) doesn't live in a writable location.Re: How to Think About Variables in C
#15The size of a type is just one of its many attributes. Even if, for example, "long", "float", and "void* " happen to have the same size, they're still very distinct types.
"Integer data types are defined in the limits.h file. Float data types are defined via macros in the floats.h file."
Integer and floating-point types are defined by the compiler, guided by the hardware and the ABI for the platform. and document the characteristics of the predefined numeric types.
"A pointer doesn’t hold a memory address, it holds a number that represents a memory address."
Sure, and a floating-point object is ultimately just a collection of bits -- but that's hardly the best way to think about either of them. Integers and pointers (addresses) are logically very distinct things, even if they happen to have similar representations. For example, the addresses of two distinct variables have no defined relationship to each other (other than being unequal); just evaluating (&x C lets you get away with a lot of type-unsafe stuff, particularly if you resort to pointer casts, but it's fundamentally much more strongly typed than the author seems to think it is.
Re: How to Think About Variables in C
#16What other mental models do people use to think about variables and memory? I would like to hear about them.
Also, when you get to manually allocated heap data (which this article doesn't cover) you don't have to worry about deallocations... usually.
Re: How to Think About Variables in C
#17What other mental models do people use to think about variables and memory? I would like to hear about them.
Re: How to Think About Variables in C
#18Extremely uninteresting- It is like a page of "C-S 1XX: Intro to C" fell out of its bindings and landed on Hacker News. This might have been mildly interesting if there had been the assembly for a few different architectures (x86, MIPS, ARM, PowerPC, etc) showing how the C code was translated to assembler for each. And could have been very interesting with an additional discussion of memory barriers and atomic operat…
Re: How to Think About Variables in C
#19"A data type is a number of bytes to the compiler." The size of a type is just one of its many attributes. Even if, for example, "long", "float", and "void* " happen to have the same size, they're still very distinct types. "Integer data types are defined in the limits.h file. Float data types are defined via macros in the floats.h file." Integer and floating-point types are defined by the compiler, guided by the har…
Re: How to Think About Variables in C
#20Extremely uninteresting- It is like a page of "C-S 1XX: Intro to C" fell out of its bindings and landed on Hacker News. This might have been mildly interesting if there had been the assembly for a few different architectures (x86, MIPS, ARM, PowerPC, etc) showing how the C code was translated to assembler for each. And could have been very interesting with an additional discussion of memory barriers and atomic operat…