"Pointers" <-- crappy name

This is a discussion on "Pointers" <-- crappy name within the General Discussions forums, part of the Community Boards category; Which takes us back to: Originally Posted by argv so many books and 'tutorials' and whatever do a horrible job ...

  1. #46
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Which takes us back to:

    Quote Originally Posted by argv View Post
    so many books and 'tutorials' and whatever do a horrible job of explaining what a pointer is.
    And I completely agree. Because I now understand what a pointer is, the Koenig and Moo statement makes sense (pointlessly). If I did not know, I doubt very much that it were serve to clarify the matter. At all.

    One day in the distant future maybe I will write a book for beginner's on C. I can totally remember going through material and thinking I'd like to, because much of the material is so bad. I honestly believe the standard of technical writing on CS and programming is a often a low one. This may be similar to other academic fields which suffer from a lack of original sources for material and must depend on a slightly inbred circus of "my teacher wrote the textbook I am required to pay for".

    In turn, the style of this low standard is taken up by people who prepare tutorials, etc, and gradually disseminates itself into acceptability. Once you know the language, it obviously easy to defend a piece of work by saying, "Well, the code is not wrong" or even "the code is good" and opinions on the kind of discourse you site above will be "subjective". What they are saying may be true, but it is almost semantic obfuscation masquerading as an "explanation".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #47
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    It's easy to describe things. But it's hard to explain things.

    Most people just say "the for loop iterates over each element in a sequence." Why use a for-loop? What is a sequence? What is iteration. These are implicitly books for programmers. Most often for C or Java programmers. But the required knowledge is almost never written anywhere.

  3. #48
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    But the required knowledge is almost never written anywhere.
    Yes! This what I meant by

    Quote Originally Posted by MK27 View Post
    it is not because of the name; it is because of the strong elliptical tradition in programming "instructional" discourse, which is virtually the de facto standard. You could call it anything, it will not improve the quality of the material to which you refer.
    Maybe the idea is to *hack* your way into understanding.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #49
    Registered User
    Join Date
    May 2009
    Posts
    37

    Wink

    Ok, but what really gets me is how people always call dimetrodons dinosaurs. They are even included in children's plastic dinosaur sets. In actually they are synapsids. Go figure......

  5. #50
    Registered User
    Join Date
    May 2009
    Posts
    29

    College books

    Quote Originally Posted by MK27 View Post
    Which takes us back to:



    And I completely agree. Because I now understand what a pointer is, the Koenig and Moo statement makes sense (pointlessly). If I did not know, I doubt very much that it were serve to clarify the matter. At all.

    One day in the distant future maybe I will write a book for beginner's on C. I can totally remember going through material and thinking I'd like to, because much of the material is so bad. I honestly believe the standard of technical writing on CS and programming is a often a low one. This may be similar to other academic fields which suffer from a lack of original sources for material and must depend on a slightly inbred circus of "my teacher wrote the textbook I am required to pay for".

    In turn, the style of this low standard is taken up by people who prepare tutorials, etc, and gradually disseminates itself into acceptability. Once you know the language, it obviously easy to defend a piece of work by saying, "Well, the code is not wrong" or even "the code is good" and opinions on the kind of discourse you site above will be "subjective". What they are saying may be true, but it is almost semantic obfuscation masquerading as an "explanation".
    The best thing I've learned is that if you want to learn something, learn it from someone who knows how to teach things. I.e. if a book is used in a college class, it's probably a good book to have on the shelf.

  6. #51
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by argv View Post
    The best thing I've learned is that if you want to learn something, learn it from someone who knows how to teach things. I.e. if a book is used in a college class, it's probably a good book to have on the shelf.
    Generally true, although we have seen a few examples here where there is a university lecturer with bigger ego than educational skill that writes a book and then uses that for the education in the school. Not always such a good thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #52
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Some bogus statements:

    No, string is an array of allocates bytes in which the string is stored.
    This is a recursive definition. "A string is something in which the string is stored". I like the following definition: a string is a null-terminated array of chars. This way, we avoid talking about memory, bytes, allocation and so on.


    A pointer is a value stored in memory.
    Certainly not. Consider the following code:

    Code:
    int x = 17;
    Now 17 is a value stored in memory. Is it a pointer? No.



    Here's my definition of a pointer. But let's first define variables: a variable is a 4-tuple consisting of name, size, address and value.

    Now it's easy to define pointers: a pointer is a variable whose value is an address.


    Pointers are not addresses, and addresses are not pointers: it doesn't make sense to ask for the address of an address, the size of an address or the name of an address. For pointers, these questions are easily answered. But for the sake of simplicity, we may sloppily call pointers "addresses" and vice versa if the context doesn't require further distinction.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #53
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    A pointer can be a value stored in memory (or in swap space or in a register).

    A pointer is a variable whose value is an address in memory. This graph shows you the contents of "pointer".
    Code:
         +----------------+
         |   0x3f93f84e   |       ------------------------->    Memory location at 0x3f93f84e
         +----------------+
    
            int *pointer;
    Then programmers say that pointer "points to" the thing that is at memory address 0x3f93f84e.

    If you say *pointer, you mean you wanna access whatever is at memory location at 0x3f93f84e. Since you meant pointer to be a pointer to _int_ (int *pointer is a * (pointer) to int), the computer fetches an _int_eger from memory location 0x3f93f84e.
    Code:
         +----------------+
         |   0x3f93f84e   |       ------------------------->   Segmentation Fault
         +----------------+
    
       printf("%d\n", *pointer);
    What happened? That's strange! But the solution is easy. The pointer is uninitialized. That means, it's value is undefined (probably not what you want). Since the space where pointer is stored (pointers are also stored in memory) has probably been used before, it will contain some crap left from other programs or your own program. This crap is 0x3f93f84e. The computer doesn't know that, it makes just what you want - in this case, it fetches an integer from somewhere in you memory.

    BUT - That's a security risk. What if 0x3f93f84e points to operating system data or *where your passwords are stored in*? Then any hacker can get into your computer by accessing anything he wants. He makes the pointer "point to" somewhere he wants, for example where your login name and password are stored. Then he accesses that location. Voila, he has you name and password, if they are not encrypted (don't worry about this part).

    Or say, you want to modify *pointer. You could write zeros there where your operating system's heart is beating. That would immediately crash your computer. Either you didn't want this to happen, or a hacker wanted to kill your session. Too late. You've lost your 1000 line program cuz you didn't save.

    To prevent this from happening, the operating system doesn't allow you to look at position 0x3f93f84e in memory. It neither allows you to write there. For your own security. That's, PAY ATTENTION where you pointers point to.

    Code:
         +----------------+                                   +----------------+
         |   0xac398ef4   |       ------------------------->  |   0x00000000   |
         +----------------+                                   +----------------+
     
            pointer = &i;                                         int i = 0;
    Now you say, "make pointer point to i", or "gimme the address of i and save it in pointer". The '&' operator takes the address of something, for example i. It returns 0xac398ef4 in this case, which is the memory location where i is saved in. So the value of pointer is the address of i.

    (Of course, we have assigned i to zero, that means in fact we have written all zeros into the location where i resides)

    Now, let's take a look at i. We could simply write "printf("%d\n", i)". But that's boring! We wanna learn something. Ok, let's try to use pointer. Remembered the part with the Segmentation Fault? Try again!

    Code:
         +----------------+                                   +----------------+
         |   0xac398ef4   |       ------------------------->  |   0x00000000   |
         +----------------+                                   +----------------+
     
      printf("%d\n", *pointer);                                        i
    Compile... Run... YESSS! No strange errors, just plain zero. i was zero, remember? Now we get the same zero! What happened? We wanted to fetch an integer from memory location 0xac398ef4. Now we know that we can read from there, cuz it's *our* memory and nobody else can read from there except we can. We own i, so we own 0xac398ef4 where i is stored. Congrats, for you own some memory now!

    But what if we want to put a 5 into i? "i = 5;", right? BOORING! We can do it like that:

    Code:
         +----------------+                                   +----------------+
         |   0xac398ef4   |       ------------------------->  |   0x00000005   |
         +----------------+                                   +----------------+
    
           *pointer = 5;                                               i
    Now we say "write a 5 wherever pointer points to!". Since pointer points to i, we write 5 into i. Now, test i:

    Code:
         +----------------+                                   +----------------+
         |   0xac398ef4   |       ------------------------->  |   0x00000005   |
         +----------------+                                   +----------------+
    
      printf("%d\n", *pointer);                                        i
    Compile... Run. What do we see? We printed out *pointer first remember?, that gave us a zero. Now, we write a 5 into the location where i lives. Now, we print out i... The output is... 5! Wow! Cool, huh? If we print *pointer again, we'll get another 5. If you assign i to 293, *pointer will give you 293. It's just always the same number.

    That was the basics. Anyone who doesn't understand can tell me. Don't be ashamed, I'm sure probably _nobody_ here knew it since s/he was a baby. But once you get it, you know very much. Pointers are one of the REAL powers of C.
    Last edited by Brafil; 05-30-2009 at 07:36 AM.

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by Snafuist
    I like the following definition: a string is a null-terminated array of chars. This way, we avoid talking about memory, bytes, allocation and so on.
    The official definition is: "a string is a contiguous sequence of characters terminated by and including the first null character." This is more accurate since a string might only occupy part of an array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #55
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    These are definitions for C-style strings. There is no requirement on std::string to be null-terminated, for example, yet it still is a string.

    More generally, a string is a contiguous sequence of characters. Not necessarily contiguous in memory (sgi's rope isn't, nor are typical strings in functional languages), just in concept - a string has no holes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #56
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by CornedBee
    These are definitions for C-style strings.
    Yes, hence "official" in this context means "as defined by the C standard".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #57
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Snafuist View Post
    Code:
    int x = 17;
    Now 17 is a value stored in memory. Is it a pointer? No.
    That's some bad logic you're doing there. You disprove "A -> B" by giving an example where A = false and B = true. But "A -> B" still holds there.

    Like:
    C is a programming language.
    Now you disprove this saying: Java is a programming language. Is it C? No.

    That said, a pointer doesn't have to be a value stored in memory. It can be stored in a register (or on a piece of paper for that matter).

  13. #58
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Brafil View Post

    BUT - That's a security risk. What if 0x3f93f84e points to operating system data or *where your passwords are stored in*? Then any hacker can get into your computer by accessing anything he wants. He makes the pointer "point to" somewhere he wants, for example where your login name and password are stored. Then he accesses that location. Voila, he has you name and password, if they are not encrypted (don't worry about this part).
    My instructor once blew the bounds of an array and wrote to the part of the computer that tells the computer to write to random parts of the hard drive
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  14. #59
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Wow. The computer shouldn't be writing to random parts of the hard drive. So you mean your instructor rescued it?

  15. #60
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by ಠ_ಠ View Post
    My instructor once blew the bounds of an array and wrote to the part of the computer that tells the computer to write to random parts of the hard drive
    On any modern operating system, that is impossible.
    But it is possible to overwrite other sensitive data by writing out of bounds (it might even have corrupted a buffer that holds contents to write to the disk in an open file).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Look at my crappy program. Look at it!
    By Hirumaru in forum C Programming
    Replies: 18
    Last Post: 03-12-2009, 01:17 AM
  2. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 02:17 PM
  3. x = x < y < 2; WHY IS THIS ALWAYS TRUE!?
    By sh4k3 in forum C Programming
    Replies: 5
    Last Post: 06-08-2007, 01:00 AM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21