a quick look at c for c++ programmers noah mendelsohn (with updates by mark sheldon) tufts...

43
A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: [email protected] Web: http://www.cs.tufts.edu/~noah COMP 40: Machine Structure and Assembly Language Programming (Fall 2015)

Upload: kelly-gilmore

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

A Quick Look at Cfor

C++ Programmers

Noah Mendelsohn (with updates by Mark Sheldon)Tufts University Email: [email protected]: http://www.cs.tufts.edu/~noah

COMP 40: Machine Structure and

Assembly Language Programming (Fall 2015)

Page 2: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Let’s look at some code

Page 3: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

3

#include <iostream>#include <string>

using namespace std;

int main(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

C++

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C

Page 4: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

4

#include <iostream>#include <string>

using namespace std;

int main(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C++ C

No namespaces in C

Page 5: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

5

#include <iostream>#include <string>

using namespace std;

intmain(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

#include <stdio.h>

intmain(int argc, char *argv[]){

char world[] = "world";

printf("Hello %s\n", world); return 0; }

C++ C

C++: stream I/O w/coutC: stdio with stdout, printf, etc.

Page 6: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

6

#include <iostream>#include <string>

using namespace std;

int main(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C++ C

Format string allows substitution

Page 7: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

7

#include <iostream>#include <string>

using namespace std;

intmain(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C++ C

Format strings:

%s – string%d – integer (decimal, signed)%u – integer (decimal, unsigned)%ld – integer (decimal, long)%x – integer (base 16 hex)%c – single ASCII character%5d – integer (5 chars wide)%-10s – string (10 chars left justified)Etc, etc.

Page 8: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

8

#include <iostream>#include <string>

using namespace std;

int main(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C++ C

\n = new line char\t = tab char\\ = \ charEtc.

Page 9: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Basic Datatypes

Page 10: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C and C++ mostly share basic data types

10

char a single byte, capable of holdingone character in the local character set.

int an integer, typically reflecting thenatural size of integers on the host machine.

short int an integer, possibly smaller than int

long int an integer, possibly longer than intlong long int an integer, possibly longer than long

float single-precision floating point.double double-precision floating point.

Abbreviations: “short” is same as “short int”; “long” same as “long int”

Examples: int x; short int s; short s; double gpa;

Page 11: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Pointers

11

char c; /* a single byte character */char *cp; /* a pointer to a single byte character */

A pointer variable holds a reference to some other variable.

Page 12: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

What does this code do?

12

int x; variable x holds an integerint y; variable y holds an integerint z; variable z holds an integer

int *ip; variable ip holds pointer to an integer

x = 2;y = 3;ip = &z;*ip = x + y;

printf(“First answer is %d\n”, z);

If you’re not sure, try this code yourself. Try changing it!

Page 13: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

What does this code do?

13

int x; variable x holds an integerint y; variable y holds an integerint z; variable z holds an integer

int *ip; variable ip holds pointer to an integer

x = 2;y = 3;ip = &z;*ip = x + y;

printf(“First answer is %d\n”, z);

*ip = *ip + z;printf(“Seconed answer is %d\n”, z);

If you’re not sure, try this code yourself. Try changing it!

Page 14: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Structured Data

Page 15: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Some structured data

16

#include <stdio.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[3] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

};

unsigned int i;

(void)argc; (void)argv;

for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return 0; }

Page 16: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Some structured data

17

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

C has structs, not classes

structs have data only…no methods!

Page 17: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Some structured data

18

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

Unlike C++: keyword struct required when naming a structured type

Page 18: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Some structured data

19

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

Initializers more or less the same as C++

Page 19: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Some structured data

20

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

We can leave out array bound if initializer determines the size –

same as C++

Page 20: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

You saw printf and format strings earlier!

21

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

You will want to learn about

printf and fprintf format specifications

Page 21: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Good Practice:Single Point of Truth

Page 22: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Single point of truth

23

#include <stdio.h>intmain(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12},} ;

unsigned int i;

(void)argc; (void)argv;

for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return 0; }

What’s going on here?

Page 23: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Single point of truth

24

#include <stdio.h>intmain(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22},} ;

unsigned int i;

(void)argc; (void)argv;

for (i=0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return 0; }

What if number of students changes?

Page 24: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Single point of truth

25

#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){ struct student {

char *name; int age; };

struct student students[ ] = { {"mary", 15}, {"bob", 9}, {"tina", 12}, {“albert", 22},

} ;

unsigned int i;

(void)argc; (void)argv;

for (i = 0; i < sizeof(students) / sizeof(struct student); i++) { printf("Student %s is %d years old.\n",

students[i].name, students[i].age);};

return EXIT_SUCCESS; }

There is a single point of truth for the number of students

Try to have single points of truth for anything in your program that’s likely to change, or on which multiple other things depend…if we change this structure, everything just works!

Page 25: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Why we use Hanson in COMP 40

Page 26: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

What about abstract data types like list, or table?

Many modern languages have them built into the standard– Python, Ruby, etc., etc.

– C++ Standard Template Library (STL)

C does not standardize implementation of types like these

Hanson gives us C based ADT implementations that:– Are useful in our programs

– Teach us what’s going on in many of those higher level language implementations!

– Teach us many good techniques for building modular structures in C

28

Page 27: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C vs C++ File I/O

Page 28: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Access to files and inputFeature C C++

Pre-opened streams stdin, stdout, stderr

cin, cout, cerr

Open a file FILE *fp fopen(filename, ”r”)

ifstream myfile(“filename”);

Typical use fprintf(stderr, ”error\n”);fprintf(fp,”Hi!”);

cerr << “error” << endl;myfile << “Hi!”;

30

In both languages:

• The operating system pre-opens the three standard streams

• They can be redirected from the command line:

• myprog < somefile # stdin reads from somefile

• myprog > somefile # stdout writes to somefile

• otherprog | myprog # stdin of myprog is output of otherprog

Page 29: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

More about C Strings

Page 30: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C characters

32

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

(void)argc;(void)argv;

unsigned char var1 = 'N';unsigned char var2 = 'O';unsigned char var3 = 'A';unsigned char var4 = 'H';

/* %c prints as character %u prints unsigned integer */

printf("The number for %c is %u\n", var1, var1);printf("The number for %c is %u\n", var2, var2);printf("The number for %c is %u\n", var3, var3);printf("The number for %c is %u\n", var4, var4);

exit(EXIT_SUCCESS);}

Printing each one twice……in different formats!

Page 31: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C characters

33

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

(void)argc;(void)argv;

unsigned char var1 = 'N';unsigned char var2 = 'O';unsigned char var3 = 'A';unsigned char var4 = 'H';

/* %c prints as character %u prints unsigned integer */

printf("The number for %c is %u\n", var1, var1);printf("The number for %c is %u\n", var2, var2);printf("The number for %c is %u\n", var3, var3);printf("The number for %c is %u\n", var4, var4);

exit(EXIT_SUCCESS);}

This program prints:

The number for N is 78The number for O is 79The number for A is 65The number for H is 72

Page 32: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C characters are integers!

34

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

(void)argc;(void)argv;

unsigned char var1 = 'N';unsigned char var2 = 'O';unsigned char var3 = 'A';unsigned char var4 = 'H';

/* %c prints as character %u prints unsigned integer */

printf("The number for %c is %u\n", var1, var1);printf("The number for %c is %u\n", var2, var2);printf("The number for %c is %u\n", var3, var3);printf("The number for %c is %u\n", var4, var4);

exit(EXIT_SUCCESS);}

This program prints:

The number for N is 78The number for O is 79The number for A is 65The number for H is 72

Interesting…Characters are also numbers!

Page 33: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Hello world compared

35

C++: string type

#include <iostream>#include <string>

using namespace std;

intmain(int argc, char *argv[]){ string world = "world"; cout << "Hello " << world << endl;}

C: arrays of characters

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char world[] = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

C++ C

Page 34: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Our first hello world

36

#include <stdio.h>#inlcude <stdlib.h>

int main(int argc, char *argv[]){

char world[] = "world";

printf("Hello %s\n", world); return EXIT_SUCCESS; }

String literal gives array of characters

Page 35: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C arrays addressed by pointer to first element

37

#include <stdio.h>#include <stdlib.h>

int main(int argc, char *argv[]){

char *world = "world";char universe[] = “universe”;

printf("Hello %s\n", world);printf("Hello %s\n", universe);

return EXIT_SUCCESS; }

These do almost the same thing

The relationship between arrays and pointers is subtle & important!

This one you need to research using K&R or Harbison & Steele

Page 36: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

A trickier hello

38

#include <stdio.h>#include <stdlib.h>#include <string.h>

int main(int argc, char *argv[]){

char world[] = "world";

printf("Hello %s\n", world);

world[1] = '\0';

printf("Hello %s your string is %d bytes long!\n", world, strlen(world));

return EXIT_SUCCESS; }

What does this print?

Page 37: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

If you understand this, you’re well on your way!

39

#include <stdio.h>#include <stdlib.h>#include <string.h>

int main(int argc, char *argv[]){

char world[] = "world";printf("Hello %s\n", world);world[1] = '\0';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world));world[3] = 'm';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world));world[1] = 'o';world[4] = '\0';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world)); return EXIT_SUCCESS; }

What does this print?

Page 38: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

If you understand this, you’re well on your way!

40

#include <stdio.h>#include <stdlib.h>#include <string.h>

int main(int argc, char *argv[]){

char *world = "world";printf("Hello %s\n", world);world[1] = '\0';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world));world[3] = 'm';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world));world[1] = 'o';world[4] = '\0';printf("Hello %s your string is %d bytes long!\n",

world, strlen(world)); return EXIT_SUCCESS; }

These examples show that:

1) The logical length of a C string is determined by the terminating null character ‘\0’2) Printing using %s and checking length with strlen() respect this3) In a correct program, there should be at least enough space for the string, but you may have allocated more4) In buggy programs, you fail to null terminate or index off the end of the allocated space

Page 39: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

Memory allocation

Page 40: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

There is no new in C! C++ new allocates and initializes objects:

– Car *myCar = new Car(V8, Blue); // Create a new car • The above allocates space and initializes all the data for mycar

– delete myCar;• Runs destructors and releases memory

– Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array

– Also: std:vector // truly dynamic array

42

Page 41: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

There is no new in C! C++ new builds objects:

– Car *myCar = new Car(V8, Blue); // Create a new car • The above allocates space and initializes all the data for car

– delete myCar;• Runs destructors and releases memory

– Also: int *ip = new int[len]; // allocate array delete[] ip; // delete array

– Also: std:vector // truly dynamic array

C malloc/free allocate and free bytes:– struct car { ….members here… };struct car *car_p = malloc(sizeof struct car);• allocate unitialized bytes

– struct car *car_p = malloc(sizeof *carp);• Same, but keeps working if structure name changes: single point of truth!• You must check the return value to make sure it worked!

– free(car_p); /* frees the bytes */

43

Page 42: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

A Bit of History

Page 43: A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noah

© 2010 Noah Mendelsohn

C++ is an extension to C

C was invented many years earlier

C is a quite small language; C++ is much more complicated

C is much closer to the hardware, why we’re using it!– Good programmers know how each C construct compiles– No single C expression compiles to huge amounts of code

Most of C survives in C++– Primitive data types: int, float, double, etc– Control structures (if, while, function call)– Source structures (#include and preprocessor)– Much more

C does not have:– Classes, methods, namespaces, dynamic arrays, dynamic strings, IO

streams (in the C++ sense), inheritance, built in higher level ADTs (list, vector, deque, table, etc.)

45