According to Hoyle...
http://www.jonhoyle.com Copyright ©2007 Jonathan Hoyle
Cross-Platform Software Development from a Macintosh Perspective: Multi-Compiler Strategies with C/C++ (Part III) - The ANSI C/C++ Libraries
by Jonathan Hoyle
jhoyle at macCompanion dot com
macCompanion
February 2007
- REALbasic: http://www.maccompanion.com/macc/archives/january2006/Columns/AccordingtoHoyle.htm
- Runtime Revolution: http://www.maccompanion.com/macc/archives/february2006/Columns/AccordingtoHoyle.htm
- AMPC: http://www.maccompanion.com/macc/archives/march2006/Columns/AccordingtoHoyle.htm
- Java compilers: http://www.maccompanion.com/macc/archives/april2006/Columns/AccordingtoHoyle.htm
- Basic compilers (Part I): http://www.maccompanion.com/macc/archives/may2006/Columns/AccordingtoHoyle.htm
- Basic compilers (Part II): http://www.maccompanion.com/macc/archives/june2006/Columns/AccordingtoHoyle.htm
- Converting Legacy Frameworks: http://www.maccompanion.com/macc/archives/july2006/Columns/AccordingtoHoyle.htm
- Basic compilers (Part III): http://www.maccompanion.com/macc/archives/october2006/Columns/AccordingtoHoyle.htm
- C++ Application Programming with REALbasic: http://www.maccompanion.com/macc/archives/november2006/Columns/AccordingtoHoyle.htm
- Multi-Compiler strategies with C/C++ (Part I): http://www.maccompanion.com/macc/archives/december2006/Columns/AccordingtoHoyle.htm
- Multi-Compiler strategies with C/C++ (Part II): http://www.maccompanion.com/macc/archives/january2007/Columns/AccordingtoHoyle.htm
The ANSI C Library
- #include <stddef.h>: size_t, wchar_t, NULL, etc. // standard types
- #include <stdio.h>: printf(), scanf(), fopen(), etc. // I/O & file functions
- #include <stdlib.h>: malloc(), free(), atoi(), etc. // allocation related functions
- #include <string.h>: memcpy(), strcpy(), strcat(), etc. // string & memory functions
- #include <math.h>: sin(), log(), floor(), ceil(), etc. // mathematical functions
- #include <stdbool.h>: bool, true, false, etc. // boolean types
- #include <stdint.h>: int16_t, int32_t, int64_t, etc. // integral types
- // etc.
The ANSI C Library within C++
#include <string.h>
memcpy(destinationArray, sourceArray, numItems * sizeof(long));
#include <cstring>
std::memcpy(destinationArray, sourceArray, numItems * std::sizeof(long));
cout & Other <iostream> Classes
- float radius;
- double pi = 3.14159;
- printf("Enter the radius of a circle:\n");
- scanf("%f", &radius);
- if (radius <= 0.0)
- fprintf(stderr, "Improper value returned for radius: %2.3f", radius);
- else
- printf("Circumference=%2.3f, Volume=%2.3f\n", (float) (2.0*pi*radius), (float) (pi*radius*radius));
- float radius;
- double pi = 3.14159;
- cout << "Enter the radius of a circle:" << endl;
- cin >> radius;
- cout.precision(5);
- if (radius <= 0.0)
- cerr << "Improper value returned for radius: " << radius << endl;
- else
- cout << "Circumference=" << 2.0*pi*radius << ", Volume=" << pi*radius*radius << endl;
ANSI string Class & Other Useful Objects
auto_ptr<>'s point only to one object at a time. When handling arrays of data, the ANSI Standard Library has a number of objects to choose from. For numerical data arrays, the valarray<> templated type is perhaps the best, as it allows the array of data to be operated upon as if it were a single value. For example, if val1 and val2 are two valarray's then val3 = val1 * val2 would be an array of the products of the entries of val1 and val2 for each index. All manner of arithmetic can be performed on valarray<>, including standard math functions: val2 = sin(val1) creates a valarray<> which is a collection of the results of applying the sin() function upon each element of val1. "Slices" of valarray<>'s can also be extracted. valarray<>'s are highly optimized for mathematical calculations and are best suited for numerical types.
STL Containers
STL consists of these components: Containers, Iterators, Algorithms & Functions, and Adapters. Containers are different data structures used to hold objects, such as vector (a one dimensional array), list (a doubly-linked list), queue (a FIFO model), stack (a LIFO model), deque (a double-ended queue), and others. iterator's are generalizations of pointers, including input iterators, output iterators, forward iterators, bidirectional iterators, and more. (Not all iterators are available to all containers.) Algorithms involve optimized sorting, searching, swapping, etc. The important point here is that algorithms are container-independent.
STL is designed for performance, not for object design. This is why there is no common base class amongst the containers and why each container has its own iterator type. No special range checking is done, as target performance is the priority.
Coming Up: With this 16th installment, we will stop here in our cross-platform series and move onto more Macintosh-specific areas of interest. I hope you have enjoyed reading this series as much as I have in writing it. With Mac OS X 10.5 Leopard just around the corner, there are a number of exciting new topics that we will be delving into in the coming months. Please feel free to send your feedback and suggest and topics for the future. See you in 30!









