Live data from Hacker News

How to Think About Variables in C

denniskubes.com

11–20 of 66 posts

Re: How to Think About Variables in C

#12
post #4

What 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…

"Not every variable has a data type, e.g. void or function pointers."

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

#13
post #9

Earlier 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.

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.

Re: How to Think About Variables in C

#14

Earlier 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.

It absolutely does return a memory address; it even has the type of a memory address ("int *"). You just can't write to it because it doesn't live in a writable location (an lvalue), for the same reason you can't write:

    (x + 1) = 12
because (x+1) doesn't live in a writable location.

Re: How to Think About Variables in C

#15
"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 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

#16

What other mental models do people use to think about variables and memory? I would like to hear about them.

Go: Basically the same as C, but with better specification for type sizes, more rigid rules about automatic type conversion, no pointer arithmetic (you can do it using the unsafe package but it is highly discouraged by both the language design and idiomatic usage) and a compiler which can do type inference.

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

#18

Extremely 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…

HN has a pretty broad audience and a pretty big chunk of it doesn't know $language. These types of beginner posts for $language pop up from time to time. It's nothing to worry about.

Re: How to Think About Variables in C

#19
post #15

"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…

See also: strict aliasing

Re: How to Think About Variables in C

#20

Extremely 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…

The least they could have done is explain how structs work.
Post reply on HN