banner
Dave Horner's Website - Yet another perspective on things...
Home Tech Talk Programming C / C++ - libraries, tips, tricks and gotchas
If you appreciate the information found on this website, please drop me a line!

Who's Online

We have 32 guests online
Content View Hits : 1157223
moon and stars
How did you find my site?
 
How often do you answer random online questions?
 

Random Quote

The less confident you are, the more serious you have to act. --Tara Ploughman
Las_Vegas_2002_283
Las_Vegas_2002_183
2010-04-04 10.29.08.jpg
P7160053

C / C++ - libraries, tips, tricks and gotchas

Monday, 26 December 2005 20:56
I love programming in C and C++. It is a language which allows for fast close access to the machine's hardware. It was also the first programming language I became proficient in. It is portable in that you can find a C/C++ compiler for most every hardware platform available. Native C/C++ dlls are easily accessed from other languages like .net,java,python,etc.

Programming in C/C++ does require one to give more thought to memory management. You can shoot yourself in the foot if you are not careful. Using safe functions is a must (strncpy,snprintf,etc), buffer overflows abound. Memory leaks can and do happen. But, tools help and defining destructors is not rocket science....but remembering the details is important. :)

Because C/C++ is portable, it doesn't spec out a huge base class library...not to the extent of Java and .NET anyways. You'll usually need to create/use a framework/library for implementing the higher level stuff (gui,graphics,sound,etc). There are many good C/C++ libraries/frameworks available on the net, some of them are linked on this page.

Having said all this. I believe C/C++ will carry on with us for a long time to come.
Woot! Long live C/C++;


Articles about programming - a few on C++, povusers.org
cdecl: C gibberish ↔ English - cool C gibberish to English
Improving and Fixing C Code
Dr. Dobb's | An Interview with Bjarne Stroustrup | March 27, 2008
Eli Bendersky’s website » Blog Archive » The many faces of operator new in C++

Why is it that you haven't heard of boost?
Boost C++ Libraries
Boost C++ Libraries Installers for Windows - BoostPro Computing


Data access for C and C++

RTA - LGPL library which can expose your program's internal arrays and data structures as if they were tables in a database.
MongoDB - MongoDB (from "humongous") is a scalable, high-performance, open source, schema-free, document-oriented database. Written in C++.
The Little MongoDB Book
Learn MongoDB - mongodb interactive tutorial, little mongodb book, mongodb's geospatial interactive tutorial.
The Dynamic Programmer - Using MongoDB from C#
samus's mongodb-csharp at master - GitHub - C# driver for MongoDB.



Wt, C++ Web Toolkit - Introduction

C/C++ development with the Eclipse Platform

Unit Testing in C++

Unit testing is something that I really would like to see more of in C++ code. I love the idea of self documenting / self testing code. However, C++ lacks any sort of unit testing standards...so we are left to define one ourselves again. :(

Exploring the C++ Unit Testing Framework Jungle
CppUnit looks like a pretty good candidate:
CppUnit Wiki
CppUnit Cookbook


Formatting I/O using IOSTREAMS

I'm always forgetting the diff. stream modifiers, here are a few that I use from time to time.
#include <iostream> //cout,cint,etc
#include <iomanip> // setwidth,setfill,setprecision,etc
#include <sstream> // ostringstream
 
using namespace std;
 
int main()
{
    unsigned int number=47;
    cout << "Setting field width is simple.\n";
    cout << "For each variable, simply precede it with setw(n)"<<endl;
    cout << setfill('0') << setw(3) << number << " this results in 047"<<endl;
    cout << setiosflags(ios::right);
    cout << resetiosflags(ios::left);
    double x = 800000.0/81.0;
    cout << setprecision(2) << x;
    cout << x << " in base 8 is \"" << oct << x << "\""
         << " and in base 16 is \"" << hex << x << "\"" << endl;
 
    ostringstream msg;
    msg << "Format output to a string!" << setfill('0') << setw(3) << x  << ends;
    std::string output=msg.str();
    return 0;
}
A really good article on iostreams is "Extending the iostream library", by Horstmann.  It explains how to do everything above, plus it includes a  manipulator to convert printf formatting commands to their stream equivalents.  Very cool.

Static Class Constants

I'm always forgetting the syntax for properly defining static class constants.  The biggest problem is not defining it, but initializing static const data members.  The C++ Standard allows initialization of const static data members of an integral type inside their class.  But I don't suggest it, initialize all your data in one place.  Declare them inside your class header and implement them in your cpp file.
class Car
{
public:
    static const int NUMDOORS; //definition
    static const std::string MSG; //non-integral type; must be defined outside the class body
};
 
// then inside the cpp file.
const int Car::NUMDOORS=4;
const std::string Car::MSG= "hello";

STL Information

STLPort

STL maps


I love STL maps because they allow you to specify an ordering of your keys and the map takes care of it automagically.

A multimap is an associative container that manages a set of ordered key/value pairs. More than one value can be associated with a particular key. The pairs are ordered by key, based on a user-supplied comparator function.
// Include this for map defines.
#include <map>
 
// Define a map.
std::multimap<double /*distance*/, CityPointer* city, std::greater<double> /* order as descending */> cityList;
// You can also order in ascending by using the less<> comparator.
 
// Inserting is a little strange because you've got to use a pair.
cityList.insert(pair<double, CityPointer*>(distance, city));
 
// Return location of first element that is not less than 100
  i = cityList.lower_bound(100);
  cout << "lower bound:\n";
  cout << ( *i ).first << " -> " << ( *i ).second << "\n";
 
// Return location of first element that is greater than 100
  i = cityList.upper_bound( 100 );
  cout << "upper bound:\n";
  cout << ( *i ).first << " -> " << ( *i ).second << "\n";


CodeProject: Casting Basics - Use C++ casts in your VC++.NET programs.
Casting in C++: Bringing Safety and Smartness to Your Programs

Standard include paths

This doesn't relate directly to C++, but it does come up when programming in C++... GCC has a bunch of standard search paths that it moves through to find your libraries and include files. If you want to include a new path to the standard set of paths, just define the env. var for it.
Ex. export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/X11R6/include
Environment Variables Affecting GCC


Code modeling

GCC-XML looks amazing when used with pygccxml

CLARAty Robotic Software - JPL's open robotic framework.
libMesh - C++ finite element library
DOLFIN - C++ ordinary/partial differential equations library.


Static analysis

Unfortunately, static analysis tools are not cheap. Here are a few that I know about. I wish there were some good open source alternatives.
Coverity Incorporated: Products: Coverity Prevent SQS for C/C++: Map the Software DNA
Insure++®: C/C++ Testing Tool, Detect elusive Runtime Memory Errors - Parasoft

Look Coverity for open source projects:
:: scan.coverity.com : Accelerating Open Source Quality :
RATS - Rough Auditing Tool for Security - is a tool for scanning C, C++, Perl, PHP and Python source code and flagging common security related programming errors such as buffer overflows and TOCTOU (Time Of Check, Time Of Use) race conditions.


Managed C++/CLI

Junfeng Zhang's Windows Programming Notes : Conversion between System.String and char *
Junfeng Zhang's Windows Programming Notes : Reverse P/Invoke Marshaling Performance


LLVM Clang

The LLVM Compiler Infrastructure Project - LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
"clang" C Language Family Frontend for LLVM - Clang is an "LLVM native" C/C++/Objective-C compiler, which aims to deliver amazingly fast compiles (e.g. about 3x faster than GCC when compiling Objective-C code in a debug configuration), extremely useful error and warning messages and to provide a platform for building great source level tools.
Last Updated on Thursday, 26 May 2011 17:40