Thread: The use of pointers...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't see where c++ wants to protect the programmer from pointers. If it would do so there would be
    You are looking at part of C++'s C legacy. That said, I do not think that C++'s goal here is to protect the programmer from pointers. Rather, it is to provide abstractions to ease tasks that would otherwise need direct use of pointers, and yet allow the programmer to use such a low level construct as necessary.

    So you need to know about pointers for the simplest possible c++ program.
    Command line arguments can be introduced later. The version of main() that has no parameters does exist, after all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One doesn't need to know anything about pointers to understand iterators.

    And the point isn't to completely get rid of pointers from the teaching. Rather, its to limit there use in cases where better alternatives exist. One doesn't necessarily need to understand a function in order to start learning with int main(). Besides, the signature of main that you mention in your example would not be used by most beginners.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would liken the supposition that pointers aren't useful, to suggesting that the division operator isn't useful.

    Sure if every program you can currently think of can be solved easily by only adding addition and multiplication, then division would seem pretty useless.

    All it means is that you haven't ventured very far into computer programming yet. Don't worry about it, if you continue programming the question will answer itself soon enough.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by pheres View Post
    I don't see where c++ wants to protect the programmer from pointers. If it would do so there would be

    Code:
    int main(vector<string> argv);
    
    // instead of
    
    int main(int argc, char** argv);
    So you need to know about pointers for the simplest possible c++ program.
    And even if the first version of main would exists one wouldn't be able to list all command line arguments without dereferencing the iterator.
    Better get pointers right.

    http://www.joelonsoftware.com/articl...vaSchools.html
    I don't believe I made such a claim that C++ protects programmers from pointers - indeed it doesn't, as you point out (Although I would add that a perfectly valid standards conforming declaration of main permits those parameters to be omitted).

    The issue I was addressing was that of complete newcomers to programming, rather than those who have already passed the basics - That it is possible in C++ to learn a large, useful, portion of the language without having to deal directly with pointers - This is largely thanks to the availability of strings, references, STL containers and iterators.

    Of course, once a beginner gains confidence with the easier parts of the language, then the floodgates are open for all sorts of low-level techniques which helps them better understand the why's and how's of things working the way they do. (Taking the "top down" approach in C++ doesn't mean that primitive topics are in any way bypassed, as I believe joelonsoftware is suggesting in his JavaSchools article)

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    ok you are right, int main() can be used at the beginning.

    but what is about reading code?
    C++ has A LOT of stuff build in it. And even a lot of that stuff isn't required to write a lot of programs (maybe pointers belongs to that stuff).
    Reading code makes good programmers, gets new ideas, let's one see how problems can solved better, cleaner, faster. To have fun with nearly all c++ books or tuts one has to read code.
    But people who write interesting code to read about do not bother using int main(), or to resign from using all other deep c++ stuff.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    C++ has A LOT of stuff build in it. And even a lot of that stuff isn't required to write a lot of programs (maybe pointers belongs to that stuff).
    I think you are not reading what we are saying: You have to learn how to use pointers, so you would be more insulated against the Law of Leaky Abstractions (since you want to quote Joel Spolsky ). But you should not start by learning how to use pointers. Read Koenig and Moo's book Accelerated C++ for an example of how to introduce C++ using the standard library, leaving pointers for as late as chapter 10 (and that includes a chapter 0).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Thanks a bunch. I still don't 100&#37; understand but I read in one of those links that they are only really important for more "dynamic programs". So long as I know how to use them (not when), that should be sufficient for now.
    IMO "WHEN" to use pointers depends on how you are designing your program. You usually run into pointers when you need interchangability and flexibility. For example, you have a list of functions that all have the same prototype and you want to be able to assign any of those functions to be executed when you press the Z Key on your keyboard. You would use a function pointer. Now what happens when you press the Z Key depends entirely on which of the functions you have assigned to the pointer. Another example would be when you do not want to create a duplicate copy of the data. Lets say you have several integer arrays that have 10 values each and you need to modify them all by adding 1 to each of their values. You can store a list of all the integer arrays by creating an array of 10 pointers to them. Now you can loop through them to process all 10 arrays.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by laserlight View Post
    But you should not start by learning how to use pointers. Read Koenig and Moo's book Accelerated C++ for an example of how to introduce C++ using the standard library, leaving pointers for as late as chapter 10 (and that includes a chapter 0).
    Thanks for the tip, I will get that book as soon I get money

    I guess we are just talking from different situations. You are right, it would be nice if everyone could learn everything step by step from ground to top. But not everyone learns c++ for pleasure. Sometimes you just have to get the thing done asap. for example loading code from a dll you get at run time. this is no big deal so far, but I bet chapter 0 to 9 doesn't explain it. They can't, because you need a silly pointer. Now.

  9. #24
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Wink 13 levels of pointers

    I remember hearing from a friend about a math major where he worked and that when the math major discovered pointers he wrote a program with 13+ levels of indirection.

    He kept complaining to his workmates that the compiler kept crashing.

    The response was "just because you can think it, doesn't mean the compiler can do it"

    Dave

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I once wrote a program that would automatically generate a pointer with very large numbers of indirection. I can't remember the exact results, but I think that at least 3000 levels of indirection were supported (this was GCC 2.95). It depended on the surrounding code, so I think that when the compiler eventually crashed it was because it ran out of stack space.

    Anyway, I think the C89 standard specifies that a compiler must support at least 8 levels of pointer indirection. I'm pretty sure that C99 says 64, but I'm not sure about C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Dave++ View Post
    I remember hearing from a friend about a math major where he worked and that when the math major discovered pointers he wrote a program with 13+ levels of indirection.

    He kept complaining to his workmates that the compiler kept crashing.

    The response was "just because you can think it, doesn't mean the compiler can do it"

    Dave
    With that many levels of indirection, it's VERY difficult to manage memory. One has to make sure that enough memory is allocated at each level so each access at each level is in bounds. His program probably was just buggy. And if the compiler can't handle more than a certain number of levels, it should give an error when the limit is exceeded.

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by dwks View Post
    Anyway, I think the C89 standard specifies that a compiler must support at least 8 levels of pointer indirection. I'm pretty sure that C99 says 64, but I'm not sure about C++.
    I shudder to think of the use cases that prompted the standard writers to change this requirement. I mean ...
    Code:
    char ****************************************************************wtf;
    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

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'd be surprised what generated code can look like. I'm sure that sort of thing might be generated by bison or something.

    [edit] Actually, this page says (with much elaboration) that C89 supports 12 levels of pointer indirection. At least as far as I read. http://archives.devshed.com/forums/c...e-2322818.html

    Strange. I thought I read somewhere that C89 supported 8 levels. I guess not. [/edit]
    Last edited by dwks; 07-11-2007 at 06:23 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The final draft of C99 also speaks of 12 levels of indirection.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM