Live data from Hacker News

How to Think About Variables in C

denniskubes.com

1–10 of 66 posts

Re: How to Think About Variables in C

#3
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 operations in C and their relation to assignments and pointers.

Re: How to Think About Variables in C

#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 variable has a data type, e.g. void or function pointers.

Re: How to Think About Variables in C

#5

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…

I agree. I liked the opening line though: "C is memory with syntactic sugar." It is a good introductory article for someone who has never used C -- CS-1xx Intro as you said.

Re: How to Think About Variables in C

#6

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…

I was trying to describe a simple mental model that has been helpful to me. While I agree assembly details would have been interesting putting that in would have lost more than half the audience.

Re: How to Think About Variables in C

#7
1 int x = 10;

2 &x = 20; // this doesn't work

3 * (&x) = 20; // this does work

Why does line 2 &x not work but line 3 does? Because &x returns a pointer, a number representing a memory address. This is an important distinction. A pointer doesn’t hold a memory address, it holds a number that represents a memory address.

=======

No, that is not why. Note that the following does work:

int * x = 0;

and the following works, though typically yields a warning:

int * x = 20;

Line 2 fails because & doesn't give back an l-value.

Re: How to Think About Variables in C

#8
Integers are the simple case, but you really haven't grasped the C memory model until you're comfortable handling text strings at any length, calling functions by pointers, working with structure pointers, and knowing when you need a pointer to a pointer. Part of it is understanding variable scope, local vs global vs stack frame memory. It's not rocket science, just takes practice, and the courage to segfault your way through it.

Re: How to Think About Variables in C

#9

1 int x = 10; 2 &x = 20; // this doesn't work 3 * (&x) = 20; // this does work Why does line 2 &x not work but line 3 does? Because &x returns a pointer, a number representing a memory address. This is an important distinction. A pointer doesn’t hold a memory address, it holds a number that represents a memory address. ======= No, that is not why. Note that the following does work: int * x = 0; and the following work…

[deleted]

Re: How to Think About Variables in C

#10
post #9

1 int x = 10; 2 &x = 20; // this doesn't work 3 * (&x) = 20; // this does work Why does line 2 &x not work but line 3 does? Because &x returns a pointer, a number representing a memory address. This is an important distinction. A pointer doesn’t hold a memory address, it holds a number that represents a memory address. ======= No, that is not why. Note that the following does work: int * x = 0; and the following work…

[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.
Post reply on HN