Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”);

Similar presentations


Presentation on theme: "1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”);"— Presentation transcript:

1 1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”); return 0; } C++ C

2 2 Compile & run Compile: g++ –g -o hello hello.cpp -g: allow debugging Run: hello or./hello

3 3 Intrinsic types byte short int long float double char boolean JavaC/C++ char short int long float double char boolean

4 4 Conditionals bool temp = true; int i = 2; if(temp) cout << “Hello world!” << endl; if(temp==true) cout << “Hello world!” << endl; if(i) cout << “Hello world!” << endl; An non-zero value is true (better to use 1). A zero value is false.

5 5 Control flow for loop: for(i=1; i<=9; i++){…} while loop: while(cond){…} if, else if(cond){…} else{…} do, switch

6 6 File I/O Standard I/O cout and cin are special files cout << “hello.” << endl; (print hello on screen) cin >> a; (read a from the console) File I/O Open file Read/write Close file

7 7 Command-line arguments #include using namespace std; int main(int argc, char * argv[]) { int num; ifstream input; if(argc != 3){ cout " << endl; exit(1); } num = atoi(argv[1]); input.open(argv[2]); input.close(); return 0; }

8 8 Key differences between C/C++ and Java Pointers Assignment Parameter passing Heap & Stack Arrays

9 9 Pointers C/C++ Pointer is just an address Address: location of a piece of data in memory &x: the address of x; *ptr: dereference ptr Dereference pointer to a class Someclass *ptr; //declare a pointer cout a << endl; cout << (*ptr).a << endl;

10 10 Pointers II #include using namespace std; int main(int argc, char * argv[]) { int x; int * ptr; x = 5; ptr = &x; cout << "ptr " << *ptr << endl; //print 5 x=10; cout << "ptr " << *ptr << endl; //print 10 return 0; }

11 11 Pointer exercises Data structure for an item struct myItem{ int val; myItem * ptr; }; Create a linked list with two items Print the value of items in the list Remove the last item in the list

12 12 One way to do it #include struct myItem{ int val; myItem * ptr; }; int List_Items(myItem * head) { myItem *cur_item; cur_item = head; while(cur_item != NULL){ printf("cur_item value %d\n", cur_item->val); cur_item = cur_item -> ptr; } return 1; }

13 13 One way to do it (cont’d I) int main() { myItem *head; myItem *item1, *item2; item1 = new(myItem); item1->val = 1; item1->ptr = NULL; item2 = new(myItem); item2->val = 2; item2->ptr = NULL; //now link the items head = item1; item1->ptr = item2; //list the items List_Items(head);

14 14 One way to do it (cont’d II) //now delete the last item in the list (last item ptr==NULL) myItem *cur_item, *next_item; cur_item = head; next_item = cur_item->ptr; while(next_item->ptr != NULL){ cur_item = next_item; next_item = next_item->ptr; } delete(next_item); cur_item->ptr = NULL; printf("after remove the last item:\n"); List_Items(head); return 1; }

15 15 Assignment Java assignment: makes reference C++ assignment: makes copy SomeClass x, y; Someclass *a; x=y; //this copies y to x; changing x does not change y a = &y; //copies the reference (address) of y to a; a->val += 10; //this changes y too

16 16 Parameter passing: default by value #include using namespace std; void foo(int para){ // para += 10; } int main(int argc, char * argv[]) { int i=5; foo(i); //all parameters copied by default cout << "i " << i << endl; }

17 17 Parameter passing: how to change value? (pass pointer or call by references) #include using namespace std; void foo(int * para){ *para += 10; } void bar(int & para){ para += 10; } int main(int argc, char * argv[]){ int i=5; foo(&i); cout << “after foo: i " << i << endl; bar(i); cout << “after bar: i " << i << endl; return 0;}

18 18 Stack & heap Stack: regions of memory for temporaries Stack pointer pushed in on function entry Stack pointer pushed out on function exit Heap: regions of memory for persistent data C/C++: explicitly managed

19 19 Big stack mistake (never return pointer to a stack!) #include using namespace std; int * foo( ) { int b=10; return &b; } int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1; }

20 20 Return value from heap (Good!) #include using namespace std; int * foo() { int *b = new (int); *b = 10; return b; } int main(int argc, char * argv[]) { int *a; a = foo(); cout << "a: " << *a << endl; //print 10? return 1; }

21 21 Explicit memory management You must delete items (or memory leak) Deleting items too soon – crash Deleting items twice – crash Good practice: int * a = new(int); After deleting a, do a=NULL; if(a!=NULL) delete a;

22 22 Struct: class with everything public #include using namespace std; struct foo { int mem1, mem2; }; int main(int argc, char * argv[]) { foo a, *b; a.mem1 = 1; a.mem2 = 2; b = &a; b->mem1 += 10; cout mem1 << endl; //print ? return 1; }

23 23 Class declaration: similar to Java class IntCell { public: IntCell (int initVal = 0){ storedVal = initVal; } int getVal() {return storedVal;} int setVal(int val) {storedVal = val;} private: int storedVal; } ;

24 24 Arrays Arrays do not have to be allocated with new Array bounds not checked (careful!) Avoid pointer arithmetic item[12]=0; Don’t use: v=12, *(item+v)=0; Multiple dimensional array int item[10][20];

25 25 Other features - I Assert (good style) int *ptr = NULL; … assert(ptr != NULL); Typedef typedef int * p_int; p_int a; //a is a pointer to an integer comments Same syntax as in java

26 26 Other features II #define #define MAX 100 int myArray[MAX]; Static May not mean the same thing as in java Declaring a global variable or function as static means that it is only visible in this file.

27 27 Basics of Linux On linux machine: Login at your home directory Open a “shell” or “terminal” or “xterm” workspace (4) On windows machine Install linux terminals (xmanager, putty, cygwin) Login

28 28 Basic commands of Linux I Command format: [command] [arguments] Search information on a command: man [command] e.g., man pwd Print current directory: pwd List files and directories in a directory: ls ls –l Only list directories: ls -ld Current directory:. upper-level directory:..

29 29 Basics Commands of Linux II File/directory operations: Copy file x to y: cp x y Remove file x: rm x be very careful, rm –i x Move file x to y (rename): mv x y Create new directory x: mkdir x Change directory: Go to upper level: cd.. Go to directory x: cd x Remove directory: rm –r x Rename directory x to y: mv x y

30 30 Basics Commands of Linux III State of processes: ps (process state) ps –aux ps –aux | grep bing Kill First use ps to find the ID of a process kill -9 -1 pid Pipeline command: e.g., ps -aux | more

31 31 Basics Commands of Linux IV Mode of a program: list mode of f: ls –l f 3 components: for owner, group member, others 3 bits in each component: r, w, x e.g., mode of 755 means? Change the mode of a file: chmod E.g., chmod 755 f Change owner of a program: chown

32 32 Programming on Linux Write your code (using e.g., emacs, xemacs, vi, or eclipse) Compile: e.g., g++ –g -o hello hello.cpp Run: e.g., hello or./hello Debug: print messages using gdb( locate segmentation fault) or xxgdb (better interface)


Download ppt "1 Intro to C/C++ #include using namespace std; int main() { cout << “Hello world!” << endl; return 0; } #include int main() { printf(“Hello world!\n”);"

Similar presentations


Ads by Google