[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci202] / 17
[Text Version] [Syllabus] [Schedule] [Glossary] [Resources] [Grading] [Contact] [Question] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] <17> [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Jun 3 14:03:12 PDT 2009

Contents


    Computer Science II Session 17 Bits and Pieces


      (Previous): Linked Data Structures [ 16.html ]

      Preparation

      All data is bits

        Table of Binary numbers

        Here is a table of numbers in decimal and binary notation
        Table
        DecimalBinary
        00000
        10001
        20010
        30011
        40100
        50101
        60110
        70111
        81000
        91001
        101010
        111011
        121100
        13????
        14????
        15????

        (Close Table)
        Can you can find a pattern in the above table.

        Can you guess what the last three binary codes are?

        A Binary Adding Machine using marbles

        Look for the video on this page: [ http://woodgears.ca/marbleadd/ ]

      Chapter 21 and Appendix E.4

    1. 21.1 Introduction 1005
    2. 21.2 Structure Definitions 1005
    3. A class with everything public by default.
    4. Useful to define the inner workings of a data structure in side a class.
    5. 21.3 Initializing Structures 1008
    6. I didn't know this! Wow.... I could of used it.
    7. 21.4 Using Structures with Functions 1008
    8. 21.5 typedef 1008
    9. Used to save typing, reduce errors. Also simple.
    10. 21.6 Example: High-Performance Card Shuffling and Dealing Simulation 1009
    11. Line 49 has a subtle bug, I believe.
    12. 21.7 Bitwise Operators & | ~ ^ 1012
    13. Fun.
    14. 21.8 Bit Fields 1021
    15. Interesting but only useful to people writing device drivers...
    16. 21.9 Character-Handling Library 1025
    17. cctype is a very useful library.
    18. 21.10 Pointer-Based String-Conversion Functions 1031
    19. Very handy for converting X_to_Y.
    20. 21.11 Search Functions of the Pointer-Based String-Handling Library 1036
    21. Dangerous. Not in CS202.
    22. 21.12 Memory Functions of the Pointer-Based String-Handling Library 1041
    23. Be very careful with these. Only use when speed is vital to a project.
    24. 21.13 Wrap-Up 1046
    25. Appendix E.4 1290
    26. Important for command line interfaces.

      The Arguments to Main -- Vital Tool for Command Line Interfaces

      See Appendix E.4 in the book.

      In the laboratory (lab09) you will need to understand how a program is given information on a command line. For example

       		add dick password
       		del dick

      In the final I have given you programs like [ puzzle.cpp ] and [ puzzle2.cpp ] to see if you can figure out what they do when compiled and run.

      Assigned work -- study and submit a question on the above reading - Chapter 21 & Appendix E.4

      Chapter 21 pages 1005 -- Legacy C++ Code

      Both chapter 21 and the appendix section speak of working with legacy C and C++ code, do you think that this is important to learn and why?

      Yes.

      Absolutely.

      Most ( > 70% ) programming work is modifying existing programs -- so called "legacy code".

      Programs stay in use for decades. Someone has to fix them and improve them. This is called maintenance and is a popular task to give to new hires in the computer business.

      Even when starting from scratch (so-called Green space projects) you may find that you are using and old version of language -- for example C rather than C++.

      Also -- in CSCI320 we study the history of languages and you again need to work with some pretty old stuff. This is the only way to understand why things are the way they are!

      So -- yes -- you need to know how old code works.

      Chapter 21 pages 1006-1007 -- Struct

      Could you please go in more detail with "struct" because the book's explanations seems vague.

      See next.

      Chapter 21 pages 1005... -- Structs

      What exactly is a struct?

      In modern terms -- a struct is a class with most of its members public. To be precise a struct defaults to public and a class defaults

      Chapter 21 page 1005 structs

      Do structures have any modern use?

      Good question. You can get almost exactly the same effect as "struct...{...};" by writing "class...{public:...};".

      A common use is to declare a private struct inside a class to handle a linked data structure. Suppose I have a problem where I have a class of Group that needs an internal set of People (in the group) then I can write

       	class Group {
       	   private:
       		struct List { People * person; List * next; List * previous } list;
       		List * last;
       ...
       			for(People* p = list; p!=NULL; p=p->next ) ...
       ...
       			for(People* p = last; p!=NULL; p=p->previous ) ...
       ...
       	};
      So I have a hidden (from my clients) internal double linked list....

      Here is another example of creating an ad hoc structure -- it records then names of the months and the number of days in each month
      (ignoring leap years): [ struct.cpp ]

      Exercise: why am i not worried about making the structure of 'months' public?

      So structs are useful for quickly setting up a data structure.

      Chapter 21 page 1008 structs and and parameter passing to functions

      What is the difference between passing structs to functions by value and passing structs to functions by reference?

      When you pass something by value a copy is made inside the function. If the structure is large and the function called many times this wastes time.

      When you pass something by reference the function is given the starting address and accesses the thing directly. If the structure is large this is much faster.

      Pass by reference also means that the function has access to the parts of the passed object and can do what it wants with them. This can be risky.

      Constant reference -- fast and safer.

      Chapter 21 page 1008 typedef

      What does the text mean by saying that "...typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types..."?

      If you declare a variable:

    27. .... variable ......
       		char a[10]; //an array of 10 characters.
      then a is the name of some storage.

      The typedef version:

    28. typedef .... typename ......
                typedef char C10 [10];
      creates a new name that can only be used as a type:
       		C10 a, b, c; // 3 arrays of ten characters.

      Chapter 21 page 1008 -- typedef

      How useful is typedef and can you show an example of how this can be useful?

      It's main function is to make code shorter.

      In the next two sessions you will meet the "iterators" in the STL. A typical declaration without typedef would be

       		vector < Person * > :: iterator p;
      and you might have a dozen for loops using this declaration...

      So write

       		typedef vector < Person * > :: iterator VPI;
      and then you only have to type
       		for( VPI p = start; p!=end; p++)...
       ...
       		for( VPI p = somewhere; p!=middle; p++)...
      and so on.

      By typing less you make fewer errors.

      And the code is easier to read as well as write.

      Chapter 21 Page 1012 demo bitwise

      Can you demonstrate those bitwise operators? Can you go over the bitwise operators and how they are used?

      If we have time in class -- yes.

      Programs: [ bitzi.cpp ] (print binary for small ints), and [ unfixed.misc.cpp ] [ misc.cpp ] (struct, union, bits, bitfields, bitwise, and a do-while).

      Would many of the operators be used for checking for correct data entries from users

      Not really. Mostly for checking the status of devices.

      Chapter 21 Page 1021 - 21.8 Bit Fields

      Can you explain what Bit Fields are and how they work?

      The let you describe a detailed, bit by bit, a mapping of a piece of storage.

      Mostly used to handle hardware and character codes.

      Chapter 21.8 1023 bit Fields

      why do bit fields with same data type takes less size than with mixed data types.

      Why not? The size of int, char, double is largely determined by the hardware. The size of a bit-field by the programmer.

      As a result bitfields can be slower than the normal data. The program has to use bit operations to extract bit fields.

      By the way -- I don't think you can have float or double bit-fields seeing the format and arithmetic is not defined.

      Chapter 21 pages 1041 -- String-Handling

      Is there a performance difference between void functions
       	void *memcpy( void *s1, const void *s2, size_t n )
      and
       	void *memmove( void *s1, const void *s2, size_t n )
      ? I guess that memmove will be a little bit slower. Both are O(n) but the move won't be more than twice as long and the simple copy.

      Normally these are only used when you want to copy data quickly.

      The important thing is to NEVER use memcpy when the two memory areas overlap. The copy starts copying the data it has already copied.

      Next -- STL Containers -- Data structures with fewer tears

      [ 18.html ]

    . . . . . . . . . ( end of section Computer Science II Session 17 Bits and Pieces) <<Contents | End>>

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. class::="A description of a set of similar objects that have similar data plus the functions needed to manipulate the data".
  3. Data_Structure::=A small data base.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. KDE::="Kommon Desktop Environment".
  7. object::="A little bit of knowledge -- some data and some know how", and instance of a class".
  8. OOP::="Object-Oriented Programming", Current paradigm for programming.
  9. Semantics::=Rules determining the meaning of correct statements in a language.
  10. SP::="Structured Programming", a previous paradigm for programming.
  11. STL::="The standard C++ library of classes and functions" -- also called the "Standard Template Library" because many of the classes and functions will work with any kind of data.
  12. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  13. Q::software="A program I wrote to make software easier to develop",
  14. TBA::="To Be Announced", something I should do.
  15. TBD::="To Be Done", something you have to do.
  16. UML::="Unified Modeling Language".
  17. void::C++Keyword="Indicates a function that has no return".

End