top of page
Search
lonnyharrerhi

Data Structures With C Using Stl 2nd Edition.pdf: A Complete and In-depth Course on Data Structures



The CMPE 250 course is the last of the sequence of introductory programming courses at Bogazici (CMPE 150-160-250) and is one of the key courses in the entire curriculum. The focus is on algorithm design, reductions and the choice of appropriate data structures for solving computational problems. Programming methodology is based on the widely-used C++ programming language. Emphasis is on good programming style, learning the ecosystem of the C++ language and the standard template library (STL).


Here we made a small error in use1 that will lead to corrupted data or a crash.The (pointer, count)-style interface leaves increment1() with no realistic way of defending itself against out-of-range errors.If we could check subscripts for out of range access, then the error would not be discovered until p[10] was accessed.We could check earlier and improve the code:




Data Structures With C Using Stl 2nd Edition.pdf



Large functions are hard to read, more likely to contain complex code, and more likely to have variables in larger than minimal scopes.Functions with complex control structures are more likely to be long and more likely to hide logical errors


If a class has any private data, a user cannot completely initialize an object without the use of a constructor.Hence, the class definer will provide a constructor and must specify its meaning.This effectively means the definer need to define an invariant.


There is nothing wrong with this code as far as the C++ language rules are concerned,but nearly everything is wrong from a design perspective.The private data is hidden far from the public data.The data is split in different parts of the class declaration.Different parts of the data have different access.All of this decreases readability and complicates maintenance.


protected data is a source of complexity and errors.protected data complicates the statement of invariants.protected data inherently violates the guidance against putting data in base classes, which usually leads to having to deal with virtual inheritance as well.


Protected data often looks tempting to enable arbitrary improvements through derivation.Often, what you get is unprincipled changes and errors.Prefer private data with a well-specified and enforced invariant.Alternative, and often better, keep data out of any class used as an interface.


Not every class is meant to be a base class.Most standard-library classes are examples of that (e.g., std::vector and std::string are not designed to be derived from).This rule is about using final on classes with virtual functions meant to be interfaces for a class hierarchy.


It is undefined behavior to read a union member with a different type from the one with which it was written.Such punning is invisible, or at least harder to spot than using a named cast.Type punning using a union is a source of errors.


owner has no default semantics beyond T*. It can be used without changing any code using it and without affecting ABIs.It is simply an indicator to programmers and analysis tools.For example, if an owner is a member of a class, that class better have a destructor that deletes it.


(Simple) Warn if a function uses a Shared_pointer with an object allocated within the function, but never returns the Shared_pointer or passes it to a function requiring a Shared_pointer&. Suggest using unique_ptr instead.


Code using a library can be much easier to write than code working directly with language features, much shorter, tend to be of a higher level of abstraction, and the library code is presumably already tested.The ISO C++ Standard Library is among the most widely known and best tested libraries.It is available as part of all C++ implementations.


Use gsl::span instead.Pointers should only refer to single objects.Pointer arithmetic is fragile and easy to get wrong, the source of many, many bad bugs and security violations.span is a bounds-checked, safe type for accessing arrays of data.Access into an array with known bounds using a constant as a subscript can be validated by the compiler.


Subscripting with a variable is difficult for both tools and humans to validate as safe.span is a run-time bounds-checked, safe type for accessing arrays of data.at() is another alternative that ensures single accesses are bounds-checked.If iterators are needed to access an array, use the iterators from a span constructed over the array.


We often want our computers to do many tasks at the same time (or at least appear to do them at the same time).The reasons for doing so vary (e.g., waiting for many events using only a single processor, processing many data streams simultaneously, or utilizing many hardware facilities)and so do the basic facilities for expressing concurrency and parallelism.Here, we articulate principles and rules for using the ISO standard C++ facilities for expressing basic concurrency and parallelism.


In a nutshell, if two threads can access the same object concurrently (without synchronization), and at least one is a writer (performing a non-const operation), you have a data race.For further information of how to use synchronization well to eliminate data races, please consult a good book about concurrency (See Carefully study the literature).


Without those consts, we would have to review every asynchronously invoked function for potential data races on surface_readings.Making surface_readings be const (with respect to this function) allow reasoning using only the function body.


This is a reasonable use of a thread, for which detach() is commonly used.There are problems, though.How do we monitor the detached thread to see if it is alive?Something might go wrong with the heartbeat, and losing a heartbeat can be very serious in a system for which it is needed.So, we need to communicate with the heartbeat thread(e.g., through a stream of messages or notification events using a condition_variable).


A small amount of data is cheaper to copy and access than to share it using some locking mechanism.Copying naturally gives unique ownership (simplifies code) and eliminates the possibility of data races.


??? Is std::async worth using in light of future (and even existing, as libraries) parallelism facilities? What should the guidelines recommend if someone wants to parallelize, e.g., std::accumulate (with the additional precondition of commutativity), or merge sort?


Similar to R.12, which tells you to avoid raw owning pointers, you shouldalso avoid raw threads and raw promises where possible. Use a factory function such as std::async,which handles spawning or reusing a thread without exposing raw threads to your own code.


Vectorization is a technique for executing a number of tasks concurrently without introducing explicit synchronization.An operation is simply applied to elements of a data structure (a vector, an array, etc.) in parallel.Vectorization has the interesting property of often requiring no non-local changes to a program.However, vectorization works best with simple data structures and with algorithms specifically crafted to enable it.


A lot of fear of exceptions is misguided.When used for exceptional circumstances in code that is not littered with pointers and complicated control structures,exception handling is almost always affordable (in time and space) and almost always leads to better code.This, of course, assumes a good implementation of the exception handling mechanisms, which is not available on all systems.There are also cases where the problems above do not apply, but exceptions cannot be used for other reasons.Some hard-real-time systems are an example: An operation has to be completed within a fixed time with an error or a correct answer.In the absence of appropriate time estimation tools, this is hard to guarantee for exceptions.Such systems (e.g. flight control software) typically also ban the use of dynamic (heap) memory.


Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates)is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.


  • Here is the source code forData Structures and Algorithm Analysis in C++ (Second Edition),by Mark Allen Weiss.The materials here are copyrighted.I have successfully compiled and tested the programs underBorland 5.0, Visual C++ 5.0 and 6.0, CodeWarrior Pro Release 2 (Windows),g++ 2.7.2 and 2.8.1, and SunPro 4.1.Greg Ozbirn from U.T. Dallas has rewritten the code using ANSI C++.This mostly involves fixing the header files.Click here to obtain the ANSI C++ conversionKnown BugsTestCursorList.cpp does not compile under g++ 2.7.2.I think it is a problem with static template members,which g++ does not understand until g++ 2.8.1.Metrowerks insists on compiling the STL, which causes conflicts forswap and merge inSort.h. Also, it does not understand default templateparameters, making its vector and string incompatible withsome of the code. The easy fix is to add preprocessor macros asfollows: in vector.h, #define vector Vector;similarly for string.h and in Sort.h. This works as long asiostream.h is included prior to the other header files,as is done in the online code.Compilation InstructionsHere are compilation instructionsfor g++,SunPro,Borland 5.0,andVisual 5.0.(How to setup Windows for Visualcommand line compilation.)You can use this to guide you on how togenerate project files for Visual C++.Throughout I am assuming 32-bit ints.All template classes have the header file include the .cppfile, even though this defeats the purpose ofseparate compilation. There are ways around this,but I'd rather keep everything simple for now.Jeffrey Walton has supplied somecommonworkarounds for C++ compilers.Finally, here is azip file that containsCodeWarrior projects.You'll have to get everything in the correct directories.(Does not include some late additions from Chapter 1; check back later).Complete BundleUnix tar gzip

  • Unix tar

(Winzip can read this.)Note to Macintosh users:There is a utility to read zip files.Click here to download.Individual Files 2ff7e9595c


0 views0 comments

Recent Posts

See All

Comments


bottom of page