




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Tips for C Programmers cont'd. Use inline functions and parameterized types instead of preprocessor macros, e.g.,. C. Macros de ne MAX A,B A = B ? A : ...
Typology: Slides
1 / 8
This page cannot be seen from the preview
Don't miss anything!





1
#de ne PI 3. #de ne MAX INT 0x7FFFFFFF #de ne MAX UNSIGNED 0xFFFFFFFF { C++
const double PI = 3.14159; const int MAX INT = 0x7FFFFFFF; const unsigned MAX UNSIGNED = 0xFFFFFFFF;
{ In contrast, names declared with const are typ ed and follow C++ scop e rules
e.g., consts have static linkage: : :
2
Macros
#de ne MAX(A,B) (((A) >= (B))? (A) : (B)) /* : : : / MAX (a++, b++); / Trouble! */ Using a typ e as a parameter:
#de ne DECLARE MAX(TYPE)
TYPE MAX (TYPE a, TYPE b)
f return a >= b? a : b; g DECLARE MAX (int) DECLARE MAX (double) DECLARE MAX (char) { C++
inline int MAX (int a, int b) freturn a >= b? a : b;g /* : : : / MAX (a++, b++); / No problem! */ template
{ Wrapping headers and commenting out co de blo cks:
#ifndef FOOBAR H #de ne FOOBAR H : : : #endif { Stringizing and token pasting
#de ne name2(A,B) A##B { File inclusion
#include <iostream.h>
unsigned
#include <iostream.h> inline void f (int) f cout << "f (int) called\n"; g inline void f (unsigned) f cout << "f (unsigned) called\n"; g int main (void) f f (1); // calls f (int) f (1U); // calls f (unsigned) g
5
void screen size (unsigned *height, unsigned width); / : : : */ unsigned height, width; screen size (&height, &width); { C++
void screen size (unsigned &height, unsigned &width); // : : : unsigned height, width; screen size (height, width);
6
struct Big Struct f int array[ 100000 ], int size; g;
void fo o (struct Big Struct *bs); // passed as p ointer for eciency
int strlen (char *str);
void fo o (const Big Struct &bs);
int strlen (const char *str);
int abs (int x); double fabs (double x); long labs (long x); { C++
int abs (int x); double abs (double x); long abs (long x);
#include <math.h> enum Shap e Typ e f TRIANGLE, RECTANGLE, CIRCLE g;
struct Triangle f oat x1, y1, x2, y2, x3, y3; g; struct Rectange f oat x1, y1, x2, y2; g; struct Circle f oat x, y, r; g;
struct Shap e f enum Shap e Typ e shap e; union f struct Triange t; struct Rectange r; struct Circle c; g u; g;
13
oat area (struct Shap e *s) f switch (s->shap e) f case TRIANGLE: struct Triangle *p = &s->u.t; return fabs ( (p->x1 * p->y2 p->x2 * p->y1) + (p->x2 * p->y3 p->x3 * p->y2) + (p->x3 * p->y1 p->x1 * p->y3)) / 2; case RECTANGLE: struct Rectange *p = &s->u.r; return fabs ((p->x1 p->x2) * (p->y1 p->y2)); case CIRCLE: struct Circle *p = &s->u.c; return M PI * p->r * p->r; default: fprintf (stderr, "Invalid shap e\n"); exit (1); g g
14
#include <iostream.h> #include <math.h> class Shap e f public: Shap e () fg virtual oat area (void) const = 0; g; class Triangle : public Shap e f public: Triangle ( oat x1, oat x2, oat x3, oat y1, oat y2, oat y3); virtual oat area (void) const; private: oat x1, y1, x2, y2, x3, y3; g; oat Triangle::area (void) const f return fabs ((x1 * y2 x2 * y1) + (x2 * y3 x3 * y2) + (x3 * y1 x1 * y3)) / 2; g
class Rectange : public Shap e f public: Rectangle ( oat x1, oat y1, oat x2, oat y2); virtual oat area (void) const; private: oat x1, y1, x2, y2; g; oat Rectangle::area (void) const f return fabs ((x1 x2) * (y1 y2)); g class Circle : public Shap e f public: Circle ( oat x, oat y, oat r); virtual oat area (void) const; private: oat x, y, r; g; oat Circle::area (void) const f return M PI * r * r; g
#include <stdio.h> enum Color Typ e f RED, GREEN, BLUE g; enum Color Typ e color = RED; unsigned char even parity (void); int main (void) f color = GREEN; printf ("%.2x\n", even parity ('Z')); g
17
#include <iostream.h> class My Lib f public: enum Color Typ e f RED, GREEN, BLUE g; static Color Typ e color; static unsigned char even parity (char c); g; My Lib::Color Typ e My Lib::color = My Lib::RED; int main (void) f My Lib::color = My Lib::GREEN; cout << hex (int (My Lib::even parity ('Z'))) << "\n"; g
18
unsigned hash (double val) f static union f unsigned asint[ 2 ]; double asdouble; g u; u.asdouble = val; return u.asint[ 0 ] ^ u.asint[ 1 ]; g { C++
unsigned hash (double val) f static union f unsigned asint[ 2 ]; double asdouble; g; asdouble = val; return asint[ 0 ] ^ asint[ 1 ]; g
#de ne private public #de ne const #de ne class struct
{ e.g., in op erator overloaded expressions that create temp oraries
class Fo o f public: Fo o (void); int bar (void); g; int main (void) f Fo o f; Fo o (); // declares a function returning Fo o! f.bar (); // call metho d f.bar; // a no-op .bar (); // error! g
24
extern "C" int printf (const char *, : : : );
class Base f public: virtual void f (char *name = "Base") f printf ("base = %s\n", name); g g;
class Derived : public Base f public: virtual void f (char *name = "Derived") f printf ("derived = %s\n", name); g g;
int main (void) f Derived dp = new Derived; dp->f (); / prints "derived = Derived" */
Base bp = dp; bp->f (); / prints "derived = Base" */ return 0; g 25
int b = a //* divided by 4 /4; -a; / C++ prepro cessing and parsing / int b = a -a; / C prepro cessing and parsing */ int b = a/4; -a;
char x; int fo o (char * = x); // OK int bar (char=x); // Error
{ Use of inlines in small programs can help p er- formance, extensive use of inlines in large projects can actually hurt p erformance by enlarging co de, bringing on paging problems, and forcing many recompilations
{ Sometimes it's go o d practice to turn-o inlin- ing to set a worst case p erformance base for your application, then go back an inline as part of p erformance tuning
{ Passing C++ objects by reference instead of value is a go o d practice
It's rarely to your advantage to replicate data and re o constructors and destructors un- necessarily
{ Use go o d memory (heap) management strate- gies
{ Develop go o d utility classes (for strings, in par- ticular)
{ Go o d object and proto col design (particularly, really isolating large-grained objects)
{ Give attention to paging and other ways your application uses system resources
{ In fact, as the size and complexity of software increases, such comparisons aren't evenrele- vant since C fails to b e a practical approach whereas C++ comes into its own