C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Size: px
Start display at page:

Download "C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc."

Transcription

1 C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker)

2 C for Java Programmers 2 Overview Memory, a Program s Perspective More on Variables Memory Management Simple Pointers, Pointer Types and Casting Pointers and Arrays Function Pointers

3 C for Java Programmers 3 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes...

4 C for Java Programmers 4 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes Each byte has some value,

5 C for Java Programmers 5 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes Each byte has some value, and a location in the memory

6 C for Java Programmers 6 Memory, a Program s Perspective (2) When you define variables: int count; unsigned char c;... two things happen:

7 C for Java Programmers 7 Memory, a Program s Perspective (3) Memory is reserved to store the variables

8 C for Java Programmers 8 Memory, a Program s Perspective (3) Memory is reserved to store the variables And the compiler remembers their location "c" starts at 28 "count" starts at 24

9 C for Java Programmers 9 More on variables As a result, each variable has two values: The value stored in the variable The location of the memory used to store this value The variable name is simply shorthand for the location The location is also called the address of the variable

10 C for Java Programmers 10 Pointers (1) A pointer is a variable that contains an address as it s value; it points to something Pointers have the type pointer to... Pointers can be declared using the * character int ptr; / Pointer to int / unsigned char ch; / Pointer to unsigned char / struct ComplexNumber c; / Pointer to struct ComplexNumber / int pp; / Pointer to pointer to int /

11 C for Java Programmers 11 Pointers (2) An address of a variable can be obtained using an & The address returned is a pointer to type int i = 8; int p = &i; / OK, &i returns int p now points to i / double d = &i; / ERROR, wrong pointer type /

12 C for Java Programmers 12 Pointers (3) A pointer can be chased using a * Returns the value in the location that is pointed to Can be use to write a value in the location int i = 8; int p = &i; / p now points to i / int j = p; / j now cointains 8 / p = 12; / i now contains 12 /

13 C for Java Programmers 13 Pointers (4)

14 C for Java Programmers 14 Pointers (4) int i = 8

15 C for Java Programmers 15 Pointers (4) "i" int i = 8 &i = 20

16 C for Java Programmers 16 Pointers (4) "i" int i = 8 int *p &i = 20

17 C for Java Programmers 17 Pointers (4) ???? "i" "p" int i = 8 int *p &i = 20 &p = 28

18 C for Java Programmers 18 Pointers (4) ???? "i" "p" int i = 8 int *p = &i &i = 20 &p = 28

19 C for Java Programmers 19 Pointers (4) "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28

20 C for Java Programmers 20 Pointers (4) "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28

21 C for Java Programmers 21 Pointers (4) "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28 int j = *p (= 8)

22 C for Java Programmers 22 Pointers (4) "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28 int j = *p (= 8) *p = 12

23 C for Java Programmers 23 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

24 C for Java Programmers 24 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

25 C for Java Programmers 25 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

26 C for Java Programmers 26 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

27 C for Java Programmers 27 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

28 C for Java Programmers 28 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; > y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

29 C for Java Programmers 29 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; > y = temp; int main(void) { int x = 9; int y = 5; > swap(&x, &y); printf("x = %d, y = %d\n", x, y); return 0;

30 C for Java Programmers 30 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(&x, &y); printf("x = %d, y = %d\n", x, y); return 0;

31 C for Java Programmers 31 Pointers (6) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; void add(struct data d, double value) { d.counter++; d.value += value; int main(void) { struct data d = { 0, 0.0 ; add(d, 1.0); add(d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

32 C for Java Programmers 32 Pointers (7) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; > void add(struct data d, double value) { > ( d).counter++; > ( d).value += value; int main(void) { struct data d = { 0, 0.0 ; > add(&d, 1.0); > add(&d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

33 C for Java Programmers 33 Pointers (7) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; void add(struct data d, double value) { > d >counter++; > d >value += value; int main(void) { struct data d = { 0, 0.0 ; add(&d, 1.0); add(&d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

34 C for Java Programmers 34 Arrays and Pointers (1) Special relationsship between arrays and pointers The C specifications says that &array[0] is equivalent to array

35 C for Java Programmers 35 Arrays and Pointers (2) So the following statements are equivalent: pointer = &array[0] pointer = array So an array is simply a pointer to the first element! But it is constant however! array = pointer; /* ERROR */

36 C for Java Programmers 36 Arrays and Pointers (3) You can use pointers instead of arrays as parameters #include <stdio.h> void func1(int p[], int size) { void func2(int p, int size) { int main(void) { int array[5]; func1(array, 5); func2(array, 5); return 0;

37 C for Java Programmers 37 Arrays and Pointers (4) You even use array indexing on pointers! #include <stdio.h> void clear(int p, int size) { int i; for (i=0;i<size;i++) { p[i] = 0; int main(void) { int array[5]; clear(array, 5); return 0;

38 C for Java Programmers 38 Pointer Arithmetic (1) You can do calculations on pointers (pointer arithmetic) The semantics depend on the size of a type! You can determine the size of a type using sizeof sizeof(int) / returns 4 / sizeof(double) / returns 8 / sizeof(struct ComplexNumber) / returns 16 /

39 C for Java Programmers 39 Pointer Arithmetic (2) Pointer arithmetic works with the size of types! int i = 8; int p = &i; p++; / increases p with sizeof(int) / struct ComplexNumber c = { 1.5, 0.5 ; struct ComplexNumber cp = &c; cp++; / increases p with sizeof(struct ComplexNumber) /

40 C for Java Programmers 40 Pointer Arithmetic (3) This is obvious when using pointers as arrays: int i; int array[5]; int p = array; for (i=0;i<5;i++) { p = 0; p++;

41 C for Java Programmers 41 Pointer Arithmetic (4)

42 C for Java Programmers 42 Pointer Arithmetic (4) int array[5];

43 C for Java Programmers 43 Pointer Arithmetic (4) int array[5];

44 C for Java Programmers 44 Pointer Arithmetic (4) int array[5]; int *p

45 C for Java Programmers 45 Pointer Arithmetic (4) int array[5]; int *p = array;

46 C for Java Programmers 46 Pointer Arithmetic (4) int array[5]; int *p = array; p++;

47 C for Java Programmers 47 Pointer Arithmetic (4) int array[5]; int *p = array; p++; p++;

48 C for Java Programmers 48 Pointer Arithmetic (4) int array[5]; int *p = array; p++; p++; p++;

49 C for Java Programmers 49 Pointer Arithmetic (4) int array[5]; int *p = array; p++; p++; p++; p++;

50 C for Java Programmers 50 Pointer Arithmetic (4) char array[5]; char *p = array;

51 C for Java Programmers 51 Pointer Arithmetic (4) char array[5]; char *p = array; p++;

52 C for Java Programmers 52 Pointer Arithmetic (4) char array[5]; char *p = array; p++; p++;

53 C for Java Programmers 53 Pointer Arithmetic (4) char array[5]; char *p = array; p++; p++; p++;

54 C for Java Programmers 54 Pointer Arithmetic (5) It is very important that the pointer type is correct! int array[5]; char p = (char )array; p++; / increases p with sizeof(char) / int x = p;

55 C for Java Programmers 55 Pointer Arithmetic (6) int array[5]; char *p = (char *) array;

56 C for Java Programmers 56 Pointer Arithmetic (6) int array[5]; char *p = (char *) array; p++;

57 C for Java Programmers 57 Pointer Arithmetic (6) int array[5]; char *p = (char *) array; p++; int x = *p;

58 C for Java Programmers 58 Arrays and Pointers (5) An array is really a pointer, so array indexing is pointer arithmetic array[i] is equivalent to *(array+i)

59 C for Java Programmers 59 Arrays and Pointers (6) This allows efficient implementations (e.g., strcpy): void copy(char to[], char from[]) { int i = 0; while (from[i]!= \0 ) { to[i] = from[i]; i++; to[i] = \0 ; void copy(char to, char from) { while ( from!= \0 ) { to++ = from++; to = \0 ;

60 C for Java Programmers 60 Memory Management (1) Until now, all data has been static Memory reserved at compile time You can also dynamically use memory at runtime malloc reserves memory free releases memory exported by stdlib.h

61 C for Java Programmers 61 Memory Management (2) Prototypes: void malloc(size_t size); void free(void ptr); Both use void *, a pointer without a type! You can assign this to any other pointer type Do NOT directly use void *

62 C for Java Programmers 62 #include <stdlib.h> int main(void) { int i; Memory Management (3) int arr = malloc(10 sizeof(int)); for (i=0;i<10;i++) { arr[i] = 7; free(arr); return 0;

63 C for Java Programmers 63 Memory Management (4) Unlike arrays, dynamically allocated memory can be returned from a function. #include <stdlib.h> int createintarray(int size) { return malloc(size sizeof(int)); int main(void) { int arr = createintarray(10); free(arr); return 0;

64 C for Java Programmers 64 Memory Management (5) You must always keep a pointer to allocated memory You need this to use it, and free it later If you don t, you ve got a memory leak Memory leaks will slowly reserve all the machine memory, causing the program (or the machine) to crash eventually!

65 C for Java Programmers 65 Memory Management (6) #include <stdlib.h> int main(void) { while (1) { malloc(10); / LEAK!! / return 0;

66 C for Java Programmers 66 Memory Management (7) If you run out of memory, malloc will return NULL #include <stdio.h> #include <stdlib.h> int main(void) { int array = malloc(10 sizeof(int)); if (array == NULL) { printf("out of memory!\n"); exit(1); / do something useful here / return 0;

67 C for Java Programmers 67 Memory Management (8) What happens if you mix malloc and structs? #include <stdlib.h> struct complex { int i; double d; char string[10]; int main(void) { struct complex c = malloc(sizeof(struct complex)); / do something useful here / return 0;

68 C for Java Programmers 68 Memory Management (9)

69 C for Java Programmers 69 Memory Management (9) malloc(sizeof(struct complex));

70 C for Java Programmers 70 Memory Management (9) struct complex int i; double d; char string[10]; struct complex *p = malloc(sizeof(struct complex));

71 C for Java Programmers 71 Multidimensional Arrays (1) Static multidimensional arrays can be declared like this: short array[row ][COL]; The memory is reserved at compile time The compiler needs to know the exact size Unlike Java, this is a single block of memory, NOT an array of arrays!!!

72 C for Java Programmers 72 Multidimensional Arrays (2)

73 C for Java Programmers 73 Multidimensional Arrays (2) short array[2][3];

74 C for Java Programmers 74 Multidimensional Arrays (2) short array[2][3]; create array[2] of element short[3]

75 C for Java Programmers 75 Multidimensional Arrays (2) short array[2][3]; create array[2] of element short[3]

76 C for Java Programmers 76 Multidimensional Arrays (2) short array[2][3] == short array[6]!!!

77 C for Java Programmers 77 Multidimensional Arrays (3) How can you malloc multidimensional arrays? malloc a block of data and convince C its a multidimensional array malloc an array of pointers that point to arrays (like Java does) very big difference between these approa- There is a ches!!!

78 C for Java Programmers 78 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (4) int i,j; short ( a)[cols] = malloc(rows COLS sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

79 C for Java Programmers 79 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (4) int i,j; > short ( a)[cols] = malloc(rows COLS sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { > a[i][j] = 42; / etc. /

80 C for Java Programmers 80 Multidimensional Arrays (5)

81 C for Java Programmers 81 Multidimensional Arrays (5) short array[2][3] (or short array[6])

82 C for Java Programmers 82 Multidimensional Arrays (5) short array[2][3] (or short array[6]) short (*array)[3] = malloc(... )

83 C for Java Programmers 83 Multidimensional Arrays (5) short array[2][3] (or short array[6]) short (*array)[3] = malloc(... )

84 C for Java Programmers 84 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; short a = malloc(rows sizeof(short )); for (i=0;i<rows;i++) { a[i] = malloc(cols sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

85 C for Java Programmers 85 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); for (i=0;i<rows;i++) { a[i] = malloc(cols sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

86 C for Java Programmers 86 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); > for (i=0;i<rows;i++) { > a[i] = malloc(cols sizeof(short)); > for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

87 C for Java Programmers 87 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); > for (i=0;i<rows;i++) { > a[i] = malloc(cols sizeof(short)); > for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { > a[i][j] = 42; / etc. /

88 C for Java Programmers 88 Multidimensional Arrays (7) short array[2][3] (or short array[6])

89 C for Java Programmers 89 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

90 C for Java Programmers 90 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

91 C for Java Programmers 91 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

92 C for Java Programmers 92 Multidimensional Arrays (8) Arrays of arrays: contains more data!! rows may be of different length do not have to contiguous in memory rows can be swapped or replaced The re very similar to Java s arrays of arrays!

93 C for Java Programmers 93 Function Pointers (1) C also supports pointers to functions They have the somewhat complex syntax: type (*identifier) (parameter-list);

94 C for Java Programmers 94 Function Pointers (2) For example, a pointer that can refer to a function with two int parameters and a double result: double (*func) (int p1, int p2); Note that func is the name of the pointer variable Function pointers cab be used as variables, parameters, and return types

95 C for Java Programmers 95 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); int mul(int x, int y) { return x y; Function Pointers (3) int main(void) { int result; int ( func) (int p1, int p2); func = max; result = func(1, 2); printf("%d\n", result); func = mul; result = func(3,4); printf("%d\n", result); return 0;

96 C for Java Programmers 96 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); Function Pointers (4) int foo(int a, int b, int ( func) (int p1, int p2)) { return func(a, b); int main(void) { int result = foo(11, 22, max); printf("%d\n", result); return 0;

97 C for Java Programmers 97 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); Function Pointers (5) int ( faa(void)) (int p1, int p2) { return max; int main(void) { int result; int ( func) (int p1, int p2); func = faa(); result = func(11, 22); printf("%d\n", result); return 0;

98 C for Java Programmers 98 Conclusion (1) Pointers are variable that contain a address Pointers can point to data or functions Pointers and arrays are closely related The type of the pointer used in pointer arithmetic

99 C for Java Programmers 99 Conclusion (2) malloc and free can be used for memory allocation Always assign the result of malloc to a non-void pointer and check for NULL

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size APS105 Malloc and 2D Arrays Textbook Chapters 6.4, 10.2 Datatype Size Datatypes have varying size: char: 1B int: 4B double: 8B int sizeof(): a builtin function that returns size of a type int x =

More information

First of all, it is a variable, just like other variables you studied

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

CS 11 C track: lecture 5

CS 11 C track: lecture 5 CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

Heap Arrays. Steven R. Bagley

Heap Arrays. Steven R. Bagley Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

More information

Memory, Arrays & Pointers

Memory, Arrays & Pointers 1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator

More information

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Data Structures. CSCI 112: Programming in C Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable

More information

CS 222: Pointers and Manual Memory Management

CS 222: Pointers and Manual Memory Management CS 222: Pointers and Manual Memory Management Chris Kauffman Week 4-1 Logistics Reading Ch 8 (pointers) Review 6-7 as well Exam 1 Back Today Get it in class or during office hours later HW 3 due tonight

More information

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1 MPATE-GE 2618: C Programming for Music Technology Unit 5.1 Review: automatic vs. static variables Variables declared and passed to functions are automatic variables. As soon as you leave the function,

More information

Content. In this chapter, you will learn:

Content. In this chapter, you will learn: ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Arrays in C. Data and File Structures Laboratory.   DFS Lab (ISI) Arrays in C 1 / 1 Arrays in C Data and File Structures Laboratory http://wwwisicalacin/~dfslab/2017/indexhtml DFS Lab (ISI) Arrays in C 1 / 1 What is an array? A A[0] A[1] A[2] A[n 1] Syntax Sequence of n contiguous memory

More information

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265 Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C. Alan L. Cox Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

Pointers and Memory Management

Pointers and Memory Management Pointers and Memory Management Timo Karvi 2013 Timo Karvi () Pointers and Memory Management 2013 1 / 58 Memory and C I In most modern computers, main memory is divided into bytes, with each byte capable

More information

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018 CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

More information

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6 CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers

More information

Arrays and Memory Management

Arrays and Memory Management Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted

More information

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh Remember! Practice will make you perfect!!! :D Learning C Language For BEGINNERS The 6 th week / May 24 th, 26 Su-Jin Oh sujinohkor@gmail.com 1 Index Basics Operator Precedence Table and ASCII Code Table

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 15 1 malloc + Lists Andrew Bennett Overview 2 after this lecture, you should be able to have a better

More information

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1. Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria October 23, 2002 Version: 1.00 11 1 Programming in C (1.00) dmgerman@uvic.ca Parameter passing C implements

More information

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Programming. Pointers, Multi-dimensional Arrays and Memory Management Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship

More information

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows: Pointers and Arrays Part II We will continue with our discussion on the relationship between pointers and arrays, and in particular, discuss how arrays with dynamical length can be created at run-time

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Structure: - header files - global / local variables - main() - macro Basic Units: - basic data types - arithmetic / logical / bit operators

More information

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016 CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable

More information

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Fall 2011 Name: Page Points Score 1 5 2 10 3 10 4 7 5 8 6 15 7 4 8 7 9 16 10 18 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. For each of

More information

CS 261 Data Structures. Introduction to C Programming

CS 261 Data Structures. Introduction to C Programming CS 261 Data Structures Introduction to C Programming Why C? C is a lower level, imperative language C makes it easier to focus on important concepts for this class, including memory allocation execution

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24 Memory Management a C view Dr Alun Moon Computer Science KF5010 Dr Alun Moon (Computer Science) Memory Management KF5010 1 / 24 The Von Neumann model Memory Architecture One continuous address space Program

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

CSC 270 Survey of Programming Languages. What is a Pointer?

CSC 270 Survey of Programming Languages. What is a Pointer? CSC 270 Survey of Programming Languages C Lecture 6 Pointers and Dynamic Arrays What is a Pointer? A pointer is the address in memory of a variable. We call it a pointer because we envision the address

More information

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1 C Pointers 1. Objective... 2 2. Introduction... 2 3. Pointer Variable Declarations and Initialization... 3 4. Reference operator (&) and Dereference operator (*) 6 5. Relation between Arrays and Pointers...

More information

Fall 2018 Discussion 2: September 3, 2018

Fall 2018 Discussion 2: September 3, 2018 CS 61C C Basics Fall 2018 Discussion 2: September 3, 2018 1 C C is syntactically similar to Java, but there are a few key differences: 1. C is function-oriented, not object-oriented; there are no objects.

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

Lecture 8 Dynamic Memory Allocation

Lecture 8 Dynamic Memory Allocation Lecture 8 Dynamic Memory Allocation CS240 1 Memory Computer programs manipulate an abstraction of the computer s memory subsystem Memory: on the hardware side 3 @ http://computer.howstuffworks.com/computer-memory.htm/printable

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()

More information

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science Memory Allocation in C C Programming and Software Tools N.C. State Department of Computer Science The Easy Way Java (JVM) automatically allocates and reclaims memory for you, e.g... Removed object is implicitly

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

Basic and Practice in Programming Lab7

Basic and Practice in Programming Lab7 Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;

More information

Lecture 2: C Programm

Lecture 2: C Programm 0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class. Midterm Written Exam Practice Midterm will cover all concepts covered up to the midterm exam. Concepts of arrays, LL s, pointers (*,**,***), malloc, calloc, realloc, function pointers, Hash tables will

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

Memory Management. CSC215 Lecture

Memory Management. CSC215 Lecture Memory Management CSC215 Lecture Outline Static vs Dynamic Allocation Dynamic allocation functions malloc, realloc, calloc, free Implementation Common errors Static Allocation Allocation of memory at compile-time

More information

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313. NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Programming in C! Advanced Pointers! Reminder! You can t use a pointer until it points

More information

Memory Allocation. General Questions

Memory Allocation. General Questions General Questions 1 Memory Allocation 1. Which header file should be included to use functions like malloc() and calloc()? A. memory.h B. stdlib.h C. string.h D. dos.h 2. What function should be used to

More information

Contents of Lecture 3

Contents of Lecture 3 Contents of Lecture 3 Repetition of matrices double a[3][4]; double* b; double** c; Terminology Linkage Types Conversions Jonas Skeppstedt (js@cs.lth.se) Lecture 3 2014 1 / 33 A global matrix: double a[3][4]

More information

Structures and Pointers

Structures and Pointers Structures and Pointers Comp-206 : Introduction to Software Systems Lecture 11 Alexandre Denault Computer Science McGill University Fall 2006 Note on Assignment 1 Please note that handin does not allow

More information

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures CS113: Lecture 9 Topics: Dynamic Allocation Dynamic Data Structures 1 What s wrong with this? char *big_array( char fill ) { char a[1000]; int i; for( i = 0; i < 1000; i++ ) a[i] = fill; return a; void

More information

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017 CS 137 Part 5 Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors October 25th, 2017 Exam Wrapper Silently answer the following questions on paper (for yourself) Do you think that the problems on

More information

Lecture 04 Introduction to pointers

Lecture 04 Introduction to pointers Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable

More information

Dynamic memory allocation (malloc)

Dynamic memory allocation (malloc) 1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration

More information

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7 Computer Programming & Problem Solving ( CPPS ) Chapter No 7 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

C - Func(on, Pointer, Array. Zhaoguo Wang

C - Func(on, Pointer, Array. Zhaoguo Wang C - Func(on, Pointer, Array Zhaoguo Wang A gigantic main loop https://github.com/qemu/qemu/blob/master/vl.c Function Readability Reusability Function int add(int a, int b) { int r = a + b; return r; name

More information

ntroduction to C CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 1 ntroduction to C CS 2022, Fall 2011, Lecture 1 History of C Writing code in

More information

Binghamton University. CS-211 Fall Dynamic Memory

Binghamton University. CS-211 Fall Dynamic Memory Dynamic Memory Static Memory Define variables when we write code When we write the code we decide What the type of the variable is How big array sizes will be etc. These cannot change when we run the code!

More information

1d: tests knowing about bitwise fields and union/struct differences.

1d: tests knowing about bitwise fields and union/struct differences. Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true

More information

Dynamically Allocated Memory in C

Dynamically Allocated Memory in C Dynamically Allocated Memory in C All throughout the C course (COP 3223), all examples of variable declarations were statically allocated memory. The word static means not changing while the word dynamic

More information

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions Fall 2011 Name: Page Points Score 1 7 2 10 3 8 4 13 6 17 7 4 8 16 9 15 10 10 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. For each of the

More information

High Performance Programming Programming in C part 1

High Performance Programming Programming in C part 1 High Performance Programming Programming in C part 1 Anastasia Kruchinina Uppsala University, Sweden April 18, 2017 HPP 1 / 53 C is designed on a way to provide a full control of the computer. C is the

More information

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory Pointers A pointer is simply a reference to a variable/object Compilers automatically generate code to store/retrieve variables from memory It is automatically generating internal pointers We don t have

More information

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 { Memory A bit is a binary digit, either 0 or 1. A byte is eight bits, and can thus represent 256 unique values, such as 00000000 and 10010110. Computer scientists often think in terms of hexadecimal, rather

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space

More information

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018 Hacking in C Memory layout Radboud University, Nijmegen, The Netherlands Spring 2018 A short recap The & operator gives us the address of data Inverse of & is the * operator (dereferencing) 2 A short recap

More information

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists. Steven R. Bagley Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

More information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1 4: Pointers Computer Architecture and Systems Programming

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

More information

Memory Management I. two kinds of memory: stack and heap

Memory Management I. two kinds of memory: stack and heap Memory Management I two kinds of memory: stack and heap stack memory: essentially all non-pointer (why not pointers? there s a caveat) variables and pre-declared arrays of fixed (i.e. fixed before compilation)

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

More information

return return else return

return return else return compare0.c 1 // Compares two strings' addresses 4 #include 5 6 int main(void) 7 { 8 // get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // compare strings'

More information

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12 Week 9 Part 1 Kyle Dewey Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2 Dynamic Allocation Recall... Dynamic memory allocation allows us to request memory on the fly

More information

POINTER AND ARRAY SUNU WIBIRAMA

POINTER AND ARRAY SUNU WIBIRAMA POINTER AND ARRAY SUNU WIBIRAMA Presentation Outline Basic Pointer Arrays Dynamic Memory Allocation Basic Pointer 3 Pointers A pointer is a reference to another variable (memory location) in a program

More information

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation

More information

CSE 333 Lecture 2 Memory

CSE 333 Lecture 2 Memory CSE 333 Lecture 2 Memory John Zahorjan Department of Computer Science & Engineering University of Washington Today s goals - some terminology - review of memory resources - reserving memory - type checking

More information

Memory (Stack and Heap)

Memory (Stack and Heap) Memory (Stack and Heap) Praktikum C-Programmierung Nathanael Hübbe, Eugen Betke, Michael Kuhn, Jakob Lüttgau, Jannek Squar Wissenschaftliches Rechnen Fachbereich Informatik Universität Hamburg 2018-12-03

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 =

More information