CS 240 Programming in C Pointers September 28, 2019 Haoyu Wang - - PowerPoint PPT Presentation

cs 240 programming in c
SMART_READER_LITE
LIVE PREVIEW

CS 240 Programming in C Pointers September 28, 2019 Haoyu Wang - - PowerPoint PPT Presentation

CS 240 Programming in C Pointers September 28, 2019 Haoyu Wang UMass Boston CS 240 September 28, 2019 1 / 10 Runtime Memory Layout Text segment: machine 0xFFFF instructions of program stack Data segment: constants, global variables


slide-1
SLIDE 1

CS 240 Programming in C

Pointers September 28, 2019

Haoyu Wang UMass Boston CS 240 September 28, 2019 1 / 10

slide-2
SLIDE 2

Runtime Memory Layout

Text segment: machine instructions of program Data segment: constants, global variables (static or not) and local static variables Heap: dynamically allocated space, malloc() Stack: runtime call stack

Actual parameters (arguments) for functions Local (automatic) variables Return address Returned value

0x0000 0xFFFF text (program code) data (global variables) heap (malloc’d data) stack Haoyu Wang UMass Boston CS 240 September 28, 2019 2 / 10

slide-3
SLIDE 3

Data Segment

See demo.

Haoyu Wang UMass Boston CS 240 September 28, 2019 3 / 10

slide-4
SLIDE 4

Stack frame

C program’s stack segment is consisted of nested function frames or stack frames. the main frame is the most outside frame, and the starting point of the program. Each function call will start one new frame in the stack, and after exit

  • f function, its stack frame gets destroyed with every thing in it.

Stack frames works like stack structure, the first created frame gets destroyed at last, which is the main, and the last created stack frame gets destroyed first. FILO. Stack frame growing from high address to low address.

Haoyu Wang UMass Boston CS 240 September 28, 2019 4 / 10

slide-5
SLIDE 5

Local variable

See demo.

Haoyu Wang UMass Boston CS 240 September 28, 2019 5 / 10

slide-6
SLIDE 6

Pointer a multi-dimensional array

See demo. Are "array" and "&array" the same thing? See demo.

Haoyu Wang UMass Boston CS 240 September 28, 2019 6 / 10

slide-7
SLIDE 7

Pointer to arrays

See demo.

Haoyu Wang UMass Boston CS 240 September 28, 2019 7 / 10

slide-8
SLIDE 8

malloc and free

The functions malloc and calloc obtain blocks of memory dynamically. void *malloc(size_t n) returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied. void *calloc(size_t n, size_t size) returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is initialized to zero. free(p) frees the space pointed to by p , where p was originally obtained by a call to malloc or calloc .

Haoyu Wang UMass Boston CS 240 September 28, 2019 8 / 10

slide-9
SLIDE 9

Pointer to function

In C, a function itself is not a variable, but it is possible to define pointers to functions, which can be assigned, placed in arrays, passed to functions, returned by functions, and so on.

Haoyu Wang UMass Boston CS 240 September 28, 2019 9 / 10

slide-10
SLIDE 10

Complicated Declarations

See demo.

Haoyu Wang UMass Boston CS 240 September 28, 2019 10 / 10