Re: Can't understand this! (Help required please)

From: Rich (Someone_at_somewhere.com)
Date: 02/05/05

  • Next message: Mike Wahler: "Re: [FAQ] help needed Error when entering wrong data type in do-while loop"
    Date: Sat, 05 Feb 2005 20:04:07 +0000
    
    

    > I would have expected that if #include <string> was missing,
    > the compiler would have complained about using 'std::string'.
    > Whatever.

    I am using VC++.NET 2003, I thought this was supposed to be one of the
    most standards C++ compilers around.

    >
    > So do I understand that your program compiles now?
    > That's just half (often less) of the battle. Now,
    > does it work the way you want? Make sure you test
    > exhaustively, checking 'boundary conditions', bad
    > input, etc.

    Well yes it does seem to do the job, since the array initialised is a
    string array that feeds the strings to the stack, I am using a fixed
    size array, since I dont think it is possible to calcualte the number of
    elements using sizeof, but yess it feeds the stack using push, and then
    spits them out in reverse order using pop, and noe errors with that
    delete and still no errore without it, and task manager seems to
    indicate that memory is eventually restored

    > <string> is where the << operator for std::string is declared
    > (not all the << operators are declared by <ostream>).

    Thanks for the tip, I did not realise this

    >>One interesting thing you point out is that I do not need delete on the
    >>p_arrayString object, this contradicts what our tutor says,
    >
    >
    > Your tutor is wrong (or perhaps you misunderstood her).

    Our tutor is never wrong :) except when she is

    Another part of the code that I am unsure of is this line
    for( int i = 0; i < 3; ++i )
                    stringStack.push( new std::string ( arrayString[ i ] ) );

    This does indeed use new operator, but my understanding is that the
    delete in pop takes care of this?

    >
    >

    > Your understanding is correct. Also note that there are
    > special array versions of 'new' and 'delete', and that they
    > must not be mixed with the single-object versions. IMO it's
    > important that you sort this issue out with your tutor (and if
    > she's wrong on this point, I wonder about what else -- this is
    > a very fundamental issue). Something to think about.

    Yes indeed the delete [] is used with arrays of pointers and delete is
    used on single objects, if my understanding is correct.

    > A good textbook. See www.accu.org for peer reviews.
    > I presume that you have at least one textbook already.
    > Which one?

    Well I have a few
    1. Thinking in CPP 2nd Edition Vol1 - our main course work
    2. The mother of all reference books The C++ Language Special Edition -
    used as a reference or if I am feeling particularly brave a reading book
    3. C++ in action, good but can get confusing at time
    4 OO programing with C++ by David Parsons - I like this book alot it
    explains things very well and seems to be good at relating C++ material
    to real life examples, something a mere mortal like myself appreciates
    5. Art of assembly language, perhaps an odd choice of book for someone
    learning C++, but it helped me understand the basic fundamentals of data
    types and how my computer may represent these in binary hex format etc.

    Be warned that there are many bad ones
    > out there, which can do more harm than good.

    Yes and some online courses NETG does not seem to understand what
    ANSI/ISO standards are, with typical code examples like this

    void main()
    or worse just main()

    or how about using int to represent bool types

    This was disapointing, and their arrogance to recognise or accept that
    the code or teaching is not ANSI/ISO was even more frustrating.

    This is one online/CD course to avoid at all costs complete waste of
    time and money!!

    > many older ones that once were considered 'good'
    > are now obsolete. There's only one free online C++
    > book I'm willing to recommend: Bruce Eckel's "Thinking
    > in C++". www.mindview.net

    This is the one I am using now, 2nd Edition Vol1 both book and online, I
    like the fact I can open this in VS.Net 2003 and browse this whiule
    coding :)

    >
    > Even if your course has its own 'standard' textbook(s), it's
    > often useful to supplement them with others. For the C++
    > novice I recommend:

    I agree :) right now I eat sleep and breathe C++
    >
    > New to programming:
    > Francis Glassborow's "You Can Do It!"
    > http://www.spellen.org/youcandoit/
    > (a plus here is that Francis participates
    > in this group and at comp.lang.c++)

    I have heard of this book, but have limited experiece with VB and
    Delphi, I mostly understand the three main building blocks of
    programming afaicr
    Sequence
    Selection
    Repetition/iteration

    It is supposedly possible to write programs using these three bulding
    blocks and nothing else, thus eliminating the infamous goto and gosubs

    So not too sure this book will help me a great deal with C++

    >
    > Experience with programming but new to C++:
    > Koenig & Moo's "Accelerated C++"
    > www.acceleratedcpp.com

    I have heard many many good things about this book, but have never got
    around to reading it or buying it perhaps I should make it my next purcahse

    >
    > You probably won't need it for a while yet, but when the
    > time comes, consensus (with which I agree) is that the
    > best reference for the C++ standard library is this:
    > www.josuttis.com/libbook
    > I have found it invaluable.

    Even better than The C++ Programming Language ?

    >
    > And don't forget the FAQs (which contain much Good Stuff):
    > alt.comp.lang.learn.c-c++ FAQ:
    > http://www.comeaucomputing.com/learn/faq/
    > C++ FAQ: http://www.parashift.com/c++-faq-lite/
    >

    I have heard of comeau don't they make compilers too?

    >
    >>she explained why,
    >
    >
    > I'd certainly like to hear her explanation. :-)
    >

    I am not sure why, perhaps it is because Bruce Eckel says :)
    look at this listing from the online tutorial

    //: C06:Stack3Test.cpp
    //{L} Stack3
    //{T} Stack3Test.cpp
    // Constructors/destructors
    #include "Stack3.h"
    #include "../require.h"
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;

    int main(int argc, char* argv[]) {
       requireArgs(argc, 1); // File name is argument
       ifstream in(argv[1]);
       assure(in, argv[1]);
       Stack textlines;
       string line;
       // Read file and store lines in the stack:
       while(getline(in, line))
         textlines.push(new string(line));
       // Pop the lines from the stack and print them:
       string* s;
       while((s = (string*)textlines.pop()) != 0) {
         cout << *s << endl;
         delete s;
       }
    } ///:~

    My hardcopy book confirms this code too using delete s; in the while
    loop I wonder if it should be s = 0;?

    This is most odd s is declared once but deleted lots of times, but my
    compiler does not complain with this code.

    Any ideas?

    > I'll be the first to admit C++ is neither a small nor
    > simple language.
    Can I be the second to admit this :)

    Just pay attention to detail and persevere
    > (i.e. practice, practice), and you should be fine.

    I try to, I am trying to design a small program myself to test my C++
    knowledge.

    I remeber a game I used to play on my calculator bomber run or
    something, a top game that had me addicted, it was simple in conceptbut
    highly addictive, I replicated this in VB but perhaps I might do the
    same in C++

    the graphics were crude the bomber was repesented as > this I would
    imagine would be a char* const
    the bomb was represented as an v another char* const

    and the buldings were like solid blocks unsure what to use there perhaps
      a **** char* const or even a string const

    maybe I am just getting carried away :)
    And of
    > course whenever you get stuck, you can ask here. I try to
    > help with what I can, and there are also many more folks
    > here who have more skill and experience than I. Be sure to
    > read all the replies you might get, this helps you determine
    > when someone gives you wrong information or makes a mistake,
    > including myself.

    I do, and other peoples posts, it's all knowledge for me

    >
    > More below.
    >

    > OK now I see why the data is represented as 'void*'. You're
    > using the 'C way' of handling generic element types. The
    > 'C++ way' would be to use a template. IMO the major problem
    > with using 'void*' is that the required cast discards any type
    > checking protection the compiler could have provided.

    An argument I have used many times myself, I want to learn standard C++,
    but I am assured this style of learning will be beneficial in my future
    job resume.

    I guess there could be some truth in it, I bet not everyone uses
    standard C++ and one day I may have the horrific assignment of
    deciphering this type of code

    >>using namespace std; //

    the line above shouldn't be in header files should it?

    > You seem to have alluded to it already, but I'll confirm
    > it: that code is awful. :-)
    >
    > Good luck with your studies.
    >
    > -Mike

    Thanks for all your comments and help, they have been really helpful
    Rich


  • Next message: Mike Wahler: "Re: [FAQ] help needed Error when entering wrong data type in do-while loop"

    Relevant Pages

    • Re: [Lit.] Buffer overruns
      ... >> arise in practical programming, ... > Undefined behavior is behavior for which the standard ... > the other cases result in undefined behavior. ... >> See my argument on compilers vs. person above. ...
      (sci.crypt)
    • Re: Doubts about pointers
      ... I would advocate as many C compilers as possible for beginning C ... To find out what is standard ... C programming, which amounts to pretty much the same), by tinkering ... written by Turbo C users" with "the kind of programming Turbo C ...
      (comp.lang.c)
    • Re: Programming help
      ... I have used both compilers, ... For Windows Programming, I am using this tutorial, it has to be with out ... Visual C++ 2003 standard for less than the boxed version of Visual C++ ... > Borland is interesting but it's own tutorials are not complete within ...
      (alt.comp.lang.learn.c-cpp)
    • Re: [Lit.] Buffer overruns
      ... > arise in practical programming, ... Undefined behavior is behavior for which the standard ... > See my argument on compilers vs. person above. ...
      (sci.crypt)
    • Re: Is C99 the final C? (some suggestions)
      ... The C standard specifies what is UB and what's not. ... If you have multithreading, they you have to lock and unlock ... > your heap, or use other strategies to make sure one thread isn't in the middle ... Having a different set of condoned operators at each programming ...
      (comp.lang.c)