Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 4.3 First Large Coding Example: calculator (kr76-79): Example of a Stack Machine Description of the problem and approach Pseudo code Flat implementation.

Similar presentations


Presentation on theme: "0 4.3 First Large Coding Example: calculator (kr76-79): Example of a Stack Machine Description of the problem and approach Pseudo code Flat implementation."— Presentation transcript:

1 0 4.3 First Large Coding Example: calculator (kr76-79): Example of a Stack Machine Description of the problem and approach Pseudo code Flat implementation Modular implementation 5.1 Pointers and Addresses + void * 5.2 Pointers and Function Arguments 5.4 & 6.5 malloc(), realloc(), free() A Full Application + Function Frame + Pointers (1 st Contact) + malloc() System-oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/sp, Lecture 5 – 17 March 2015 Calculator (stack machine): flat and modular code: ~30’ + ~30’ + UNIT test (cont.): ~15' + Memory Model III: Function Frame: ~30' + 1 st Pointer Contact in C: ~15' + malloc(): ~15’; Exercises: ~45’

2 1 First Large Coding Example : a calculator (1/3) problem + flat specification and pseudo code Problem : Realize a calculator that provides the operators +, -, *, and /. * - 1234 + Hint: use the reverse polish notation, i.e. an infix expression like (1-2)*(3+4) is entered as the postfix expression 1 2 - 3 4 + * Pseudo C code while (next operator or operand is not end-of-file indicator) if (number) push it else if (operator) pop operands do operation push result else if (newline) pop and print top of stack else error Skeleton Program #include … #define … function declarations main() {…} external variables for push and pop void push(double f) {…} double pop(void) {…} int getop(char s[]) {…}

3 2 First Large Coding Example : a calculator (2/3) modular specification and pseudo code calc.h void push(double); double pop(void); #define NUMBER ‘0’ int getop(char []); int getch(void); void ungetch(int); #include #include "calc.h" #define MAXOP 100 main() {…} calculator.c #include #include "calc.h" int getop(char []) {…} getop.c #include #include "calc.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} getch.c stack.c #include #include "calc.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} calculator.c stack.cgetop.c getch.c Dependency graph (all files share calc.h) calc.h

4 3 First Large Coding Example : a calculator (3/3) modular specification and pseudo code #include #include "my_stack.h" #include "my_special_io.h" #define MAXOP 100 main() {…} calculator.c #include #include "my_io.h" #include "my_special_io.h" int getop(char []) {…} my_special_io.c #include #include "my_io.h" #define BUFSIZE ‘100’ char bufsize[BUFSIZE ]; int bufp = 0; int getch(void) {…} void ungetch(int) {…} my_io.c my_stack.c #include #include "my_stack.h" #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double) {…} double pop(void) {…} my_special_io.h #define NUMBER ‘0’ int getop(char []); my_io.h int getch(void); void ungetch(int); my_stack.h void push(double); double pop(void); calculator.c my_stack.cmy_special_io.c my_io.c Dependency graph my_stack.hmy_special_io.h my_io.h

5 4 A first example main() { int x = 1; int *p; p = &x; printf(“%d”, *p); } *** 5.1 Pointers and Addresses *** Pointer – A pointer p is a variable that contains the address of a variable Address Operator & –&x is the address of the variable x Indirection or Dereferencing Operator * –*p access the object the pointer p points to … … … FF00p&p F000&xx1*p F000 0000 FFFF variables addresses name value Main Memory (with name and value for the variables and addresses) p contains the address of x p is a pointer to int x is a synonym in this case ! if you are lost with pointers, come back to this slide !

6 5 'void' and 'void *' types The type void has no values and no operations ! It is used mainly as the return type of a function, signifying that the function returns no value: void f(); It may also be used in a cast expression when it is desired to explicitly discard a value: (void) scanf(…); // don't check for error The type void *, i.e. a pointer to void : is a generic pointer allowing to be casted back and forth to any other type of pointers; the casting is automatic, but no other operations are defined ! Examples Definitions void *gen_p = NULL; // with: #define NULL (void *)\0 char *char_p; int *int_p; Implicit back and forth casting for generic pointers int_p = gen_p; // ok, equivalent to int_p = (int *)gen_p; gen_p = int_p; // ok, equivalent to gen_p = (void *)int_p Explicit casting between other pointers int_p = char_p; // invalid in ISO C, explicit casting needed int_p = (int *)char_p; // ok

7 6 *** 5.2 Pointers and Function Arguments *** C passes arguments to functions by value ! void swap1(int x, int y) { // WRONG int temp = x; x = y; y = temp; } void swap2(int *px, int *py) { // interchange *px and *py int temp = *px; *px = *py; *py = temp; } main() swap1(a,b) swap1(int x, int y) int x <– a int y <– b … py *py,b *px,a px the values of swap1()'s variables x and y have been swapped, but not the ones of main()'s a and b swap2(&a,&b) main() swap2(int *px, int *py) int *px <- &a int *py <– &b … the values of main()'s variables a and b have been swapped px is a formal parameter of type pointer to int px is a variable of type pointer to int &a is the address of a If you are lost with pointers, come back to this slide !

8 7 5.4 & 6.5 malloc, realloc, free #include void *malloc(size_t size); void *realloc(void *ptr, size_t size) void free(void *ptr); malloc: allocates size bytes of memory in the heap segment and returns a pointer to the allocated memory (a NULL pointer if the memory couldn't be allocated). Example int *ptr; ptr = malloc(sizeof(int)); ptr = realloc(ptr, 2*sizeof(int)); free(ptr); realloc: changes the size of the object pointed to by ptr to size bytes. The content will be unchanged up to the minimum of the old and new sizes. It returns a pointer with the same meaning as the one returned by malloc. free: deallocates the memory pointed by ptr, and previously allocated by malloc or realloc.


Download ppt "0 4.3 First Large Coding Example: calculator (kr76-79): Example of a Stack Machine Description of the problem and approach Pseudo code Flat implementation."

Similar presentations


Ads by Google