Programming (explanations for the non-programmers, mostly)

Discussion in 'General Chatter' started by seebs, May 3, 2015.

  1. seebs

    seebs Benevolent Dictator

    Okay, so, thinking about pointers, and C.

    The thing here is in part that C has an odd relationship between declarations and things. The famous claim is "declaration reflects use". So, the idea is this. Imagine that ip is a pointer to an int, and currently valid (it points at something). Then *ip is "the thing pointed to by ip". How do you declare a pointer to int? "int *ip;" And people often find that confusing, because the * (in that context) means "the contents of the thing", so it goes from a pointer to a not-a-pointer. But when you put it on a declaration, it converts the thing into a pointer. And this seems to be Very Confusing.

    What "declaration reflects use" means is this: "int ???;" declares that ??? is an int. In "int *ip;" the ??? is "*ip". So "*ip" is an int, which means that ip must be a pointer-to-int.

    So, okay, back to the general question of "pointers". Okay, so, the computer's memory holds a bunch of things, and in practice they are at some level all numbers. And you can view the computer's memory as having an inherent order to it. If you have 8GB (~8 billion bytes) of storage, there is such a thing as "the first byte", "the second byte", and so on.

    And that means that one of the things a number could mean to the programmer is "a location in memory where a thing is being stored".

    What this allows you to do is make things that use a pointer to find the data they are going to operate on, so that running the same code with two different pointers does the same thing to two different things.

    One analogy people sometimes use is that the "address" (the "which location in memory" thing) is reasonably analagous to a street address. It could be something like "304 Elm Street" or "317 Plum Street". So say you have a window-washing business. You have a list of addresses, and then you go to each address and wash the windows there. You don't have to have a separate set of instructions for each set of windows; you have a general set of instructions for "wash the windows", and then you just apply it to each address in turn.

    So the most common things to do with pointers are to "dereference" them, which is to say, interact with the thing they point to, or to "take the address of" a thing, which is to say, obtain a value which can be used to point to that thing. And you can also do arithmetic on them, answering questions like "okay, but what's the address of the object after this one?"

    And a huge number of problems, including about 99% of all the crashes you will ever see, involve pointers which are wrong somehow. Either the pointer is just plain garbage (does not actually describe a location in memory, say), or it's wrong in some other way, like pointing to a different thing.
     
    • Like x 1
  2. Morven

    Morven In darkness be the sound and light

    I'm pretty sure the C declaration syntax is universally acknowledged as a Bad Idea; certainly the language's creators went on to make less complicated syntaxes in their later language designs. It's powerful but I've seen long-time C programmers get it wrong a lot for complex declarations.
     
  3. Exohedron

    Exohedron Doesn't like words

    I've never understood that bit about declaring pointers until now. So int *ip unpacks to "let ip be a pointer such that *ip is an int", because *ip automatically marks ip as a pointer. I was always confused because there's no explicit pointer declaration in int *ip, just an int.
     
    • Like x 1
  4. seebs

    seebs Benevolent Dictator

    Yeah, it's confusing as hell. I honestly still can't reliably do complicated things like "pointer to function returning array of pointers" without typedefs for the intermediate stuff.

    BTW, that is also why int* ip, jp; doesn't do what the novice programmer probably expects; it ends up being parsed as if it were int *ip; int jp; and jp is just a regular int.

    Same thing with arrays, mostly; int a[5]; means "a is an array of 5 things, such that a[x] is an int". Although a[5] isn't a valid object, because C is 0-indexed and the members are 0 through 4.
     
  5. WithAnH

    WithAnH Space nerd

    Tiny aside: MATLAB, because it has to be a special snowflake, is 1-indexed. 65% of the bugs in my code are because I am constantly switching between (0-indexed) C and IDL and (1-indexed) MATLAB and so now I get the indices wrong in all of them. :(
     
  6. seebs

    seebs Benevolent Dictator

    Lua is also 1-indexed. Oddly, it seems to give me less trouble in lua specifically than in some other languages.
     
  7. siveambrai

    siveambrai Negative Karma Engine nerd.professor.gamer

    Yea. The semester I taught lua the class laughed at me a lot because I kept indexing wrong.
     
  8. Aya

    Aya words words words

    I think that programming and computer science education is more strongly oriented toward people who think in a particular way than many other fields. It's not seen as essential knowledge, so it's okay for only a very small number of people to know how to do it... so if methods of teaching work for exactly one kind of thinking and don't work well for other kinds, who cares, because you don't need that many programmers, right...?

    I don't think it's odd at all that a test for a particular style of thinking is a really good predictor of who's going to succeed at lessons taught in that style of thinking.
     
    • Like x 3
  9. Exohedron

    Exohedron Doesn't like words

    The int* notation made me so confused, because the * is not in any way attached to the int. It looks like it's trying to dereference an integer, and I never could get my head around why dereferencing an integer gave you a pointer.
     
  10. Astrodynamicist

    Astrodynamicist Adequate Potato Goblin

    an amusing story about indexing from 0 vs from 1: in one of my intro computer engineering classes, half the course staff decided they were going to be clever and index the homeworks from 0, because programming class. the other half were under the impression that they were doing the normal thing of indexing homeworks from 1. the end result was that the server they stored the homework files on was indexed differently from what the actual documents said, and we ended up with two different "homework 1"s.
     
  11. seebs

    seebs Benevolent Dictator

    Yup. And the real answer is, dereferencing a pointer would give you an int. Confusing until that clicks. Then still confusing but less so.
     
  12. ADigitalMagician

    ADigitalMagician The Ranty Tranny

    Today I'm working with my kids to build a snake clone.

    My daughter is already grasping object notation. She's having more trouble with spelling than the actual objects in play.
     
    • Like x 4
  13. Lissa Lysik'an

    Lissa Lysik'an Dragon-loving Faerie

    I prefer "int * VarName" - mostly cause I use C++ a lot and novice C++ users don't grasp that "int &VarName" means VarName is reference to an int because they think of the & as "address of" if they come from C - so spaces help. So putting spaces between the variable name, the type, and the type of indirection helps.
     
  14. seebs

    seebs Benevolent Dictator

    I learned with int *ip and am used to that because the * is part of the declaration of the specific variable, but... There's a lot of variance. Also, I don't do C++ much because I am really not good at it. I have not made the effort to get used to how C++ works, and every time I try it's changed again anyway. :P
     
    • Like x 1
  15. seebs

    seebs Benevolent Dictator

    I added a new bbcode:

    Inline rendering of [c]sample code[/c] => sample code.
     
    • Like x 2
  16. Morven

    Morven In darkness be the sound and light

    C++ burned me for way too long in the "standards not being remotely close to being implemented correctly" way. Every neat feature in the books ended up not working quite right in the implementations I had access to. So I lost interest.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice