Computer Systems and Networks
|
|
- Evelyn Williamson
- 6 years ago
- Views:
Transcription
1 University of the Pacific LECTURE 5: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu)
2 Today s Class o Pointer basics o Pointers and mul;- dimensional arrays o malloc, calloc, free o Pointers and data structures o Structures o linked lists o File I/O in C o C challenge o Deadline: Friday 15 th Sept, :59 PM
3 Pointer ArithmeEc Only addition and subtraction are allowed with pointers. All pointers increase and decrease by the length of the data-type they point to. Example: If an integer pointer, iptr holds address 32, then after the expression iptr++, iptr will hold 36 (assuming integer is 4 bytes).
4 Problem 1 The name of the array is actually a pointer pointing to the first element of the array. printf( \n %u:array+3); //prints? printf( \n %u:*(array+3)); //prints?
5 Pointers and FuncEons: Call by value vs. Call by reference Call by value main() a5,b6; update(a,b); printf( %d,a); update(int a, int b) aa-b; these are just copies. No change to original variables modification to actual variable Call by reference (pointer) main() a5,b6; update(&a,&b); printf( %d,a); update(int *a,int *b) *a*a-*b;
6 Malloc 1D int *array; //array of integers array (int *)malloc(sizeof(int)*5); address: value: array[0] array[1] array[2] array[3] array[4] array (pointer variable) value: 60 pointer s addr: 32
7 Malloc 2D Allocate 4x5 integers (important for lab 4) int **array; //a double pointer array (int **)malloc(sizeof(int *)*4); for(i0;i<4;i++) array[i] (int *)malloc(sizeof(int)*5); an array of integer pointers array of ints array of ints array of ints array of ints
8 Malloc 3D int ***array; //a triple pointer an array of double pointers a matrix of single pointers a cuboid of integers
9 Problem 2 Dynamically allocate space for a 3-D color image of width, w; height, h; color channel, c. Any pixel is accessed as image[height][width][c].
10 Calloc() void * calloc(int count, int size) Basically the same as malloc! Imagine you want an array of elements Argument 1: # of elements to allocate Argument 2: Size of each element in bytes Return value: Pointer to the region
11 Realloc() void * realloc(void *ptr, int size); Resize a dynamic region of memory Note that it might move to a new address! Argument: Pointer to the original region Argument 2: Desired size in bytes of new region Return value: Pointer to the new region It might be at the same address if you made it smaller It might be at a new address if you made it larger
12 #include <stdlib.h> Include this library to use malloc, realloc, and calloc!
13 free() Why do we need to call free() a?er calling malloc()? Memory leak malloc() cannot re- use that space ever, because its internal bookkeeping s;ll thinks that region is used Will only be recovered upon termina;ng program Opera;ng system wipes out all the memory allocated to your process (stack, heap, etc )
14 C Structures Structures are a nice way to bring certain related items together struct database int id_number; int age; float salary; ; int main() struct database employee; //an object employee.age 22; employee.id_number 1; employee.salary ;
15 Problem 3 Declare a structure called coordinate that contains two integers denoting the (height,width) location of a pixel in an image matrix. Inside main, create a structure object called and set it s values to (220,360).
16 Pointers and Structures Singly Linked Lists are a bunch of dynamically allocated structures (Nodes) connected to each other Node link stores the address of the node data link addr: Head Tail
17 Doubly Linked Lists prev. link Node data link data Head 243 data data 437 Tail
18 Example CreaEng a doubly linked list for the data: struct coordinate Write a function called: create_list to create a doubly linked list for the structure, coordinate, containing height and width information for a pixel. This function should be called to add a new coordinate to the list. The list should be doubly linked. Additionally, the list should have a head (first node) and a tail (last node) for easy traversal.
19 struct double_list struct coordinate coord; struct double_list *,*prev;; main() //Something something struct coordinate ; struct double_list *head,*tail; head; tail; No List yet
20 struct double_list struct coordinate coord; struct double_list *,*prev; main() //Something something struct coordinate ; struct double_list *head,*tail; head; tail; //something something //a loop to push random s to list 3 times for(i0;i<3;i++) //something something creates s create_list(&head, &tail, );
21 void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp;
22 Let s execute the three iteraeons
23 void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 0 nothing yet
24 void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 0 nothing yet
25 void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 0 nothing yet
26 void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 0 nothing yet
27 head tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 0 nothing yet
28 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
29 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
30 head temp tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
31 head temp tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
32 head temp tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
33 head tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 1 one item on the list
34 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
35 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
36 head tail t temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
37 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
38 head tail temp void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
39 head tail void create_list(struct double_list **head, struct double_list **tail, struct coordinate ) //call by reference if(*head) //nothing on the list yet (*head)(struct double_list *)malloc(sizeof(struct double_list)); (*head)->; (*head)->prev; (*head)->coord; (*tail)(*head); //tail and head are same when only 1 item else temp (struct double_list *)malloc(sizeof(struct double_list)); temp-> ; temp->previous *tail; temp->coord; (*tail)-> temp; (*tail)temp; iteration 2 two items on the list
40 and so on!
41 Problem 4 Write a C function that traverses the doubly linked list you just created and prints the data. Start the traversal from the head.
42 void print_list(struct double_list *head) //call by value. No change to list temphead; while(temp!) printf( \n %d %d,(temp->coord).height, (temp->coord).width; temptemp->;
43 Problem 5 Let us simulate a queue (FIFO list). The linked list function we wrote already creates a queue. Write a C function that returns the first element (struct coordinate type), removes the node, and adjusts the linked list.
44 struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ;
45 head tail struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; Let s call this function three times
46 head temp tail struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
47 head tail temp struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
48 temp head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
49 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
50 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
51 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
52 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; first call
53 temp head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
54 temp head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
55 temp struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; head if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
56 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
57 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
58 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
59 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; second call
60 temp head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
61 temp head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
62 temp struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; head if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
63 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
64 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
65 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; third call
66 head struct coordinate exit_queue(struct double_list **head) //call by reference struct coordinate ; temp*head; if(temp) //nothing on the list yet printf( \n Nothing to exit.. ); exit(0); else temp->coord; (*head)(*head)->; free(temp); if((*head)!) (*head)->prev; return ; what will happen on the fourth call?
67 File I/O
68 #include<stdio.h> int main() FILE *ptr_file; char buf[1000]; ptr_file fopen("input.txt","r"); if (!ptr_file) return 1; while (fgets(buf,1000, ptr_file)!) printf("%s",buf); fclose(ptr_file); return 0;
69 Problem 6: File I/O FuncEons o fopen opens a text file. o fclose closes a text file. o feof o fgets reads a string from a file. o fwrite o fgetc reads a character from a file. o fputc prints a character to a file.
Computer Systems and Networks
LECTURE 6: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific Today s Class o Recap from last class o File I/O o What happens during
More informationComputer Systems and Networks
LECTURE 7: PERFORMANCE MEASUREMENT Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) University of the Pacific Lab Schedule Today Lab 5 Performance Measurement is open Work
More informationComputer Systems and Networks
University of the Pacific LECTURE 7: PERFORMANCE MEASUREMENT 13 TH FEB, 2018 Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Lab Schedule Today Lab 5 Performance Measurement
More informationComputer Systems and Networks
University of the Pacific LECTURE 6: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Today s Class o What happens during a malloc and free o File I/O o String
More informationSolution for Data Structure
Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the
More informationModule III. Linear Data Structures and their Linked Storage Representation
Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail
More informationELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition
ELEC / COMP 177 Fall 2012 Some slides from Kurose and Ross, Computer Networking, 5 th Edition Prior experience in programming languages C++ programming? Java programming? C programming? Other languages?
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationAdvanced C Programming and Introduction to Data Structures
FYBCA Semester II (Advanced C Programming and Introduction to Data Structures) Question Bank Multiple Choice Questions Unit-1 1. Which operator is used with a pointer to access the value of the variable
More informationArrays, Pointers and Memory Management
Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by
More informationCSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs
Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who
More informationENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review
ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27 Midterm #2 Review 1 Time and Location The midterm will be given in class during normal class hours, 11:00a.m.-12:15p.m.,
More informationDynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to
More informationDynamic Memory Allocation
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as memory management
More informationPointers (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 informationCS 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 information19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)
Lecture 29 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 29: Linked Lists 19-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vlado Keselj Previous
More informationHeap 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 informationCP2 Revision. theme: dynamic datatypes & data structures
CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;
More informationCS 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 informationCOP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME
CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find.
More informationComputer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. C Programming
ECPE 170 Jeff Shafer University of the Pacific C Programming 2 Lab Schedule Ac>vi>es Assignments Due Today Today Friday Intro to C Programming Intro to Build Tools and Makefiles Lab Report for Lab 2 due
More informationMemory 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 informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationDynamic 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 informationThe combination of pointers, structs, and dynamic memory allocation allow for creation of data structures
Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation
More informationLinked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Dynamic memory allocation: review typedef struct { int hitemp;
More informationo Code, executable, and process o Main memory vs. virtual memory
Goals for Today s Lecture Memory Allocation Prof. David August COS 217 Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory Memory layout for UNIX processes,
More informationAssist. Prof. Dr. Caner ÖZCAN
Assist. Prof. Dr. Caner ÖZCAN Memory Structure When a variable defined it is stored somewhere in memory. Memory can be thought as block consist of cells. When a variable defined, required number of cell
More informationLinked List. April 2, 2007 Programming and Data Structure 1
Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element
More informationCS 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 informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationCpt S 122 Data Structures. Data Structures
Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation
More informationIn 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 informationWhat is recursion. WAP to find sum of n natural numbers using recursion (5)
DEC 2014 Q1 a What is recursion. WAP to find sum of n natural numbers using recursion (5) Recursion is a phenomenon in which a function calls itself. A function which calls itself is called recursive function.
More informationLinked Lists and other Dynamic Data Structures
Linked Lists and other Dynamic Data Structures Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy) Dynamic Data
More informationSingly linked lists in C.
Singly linked lists in C http://www.cprogramming.com/tutorial/c/lesson15.html By Alex Allain Linked lists are a way to store data with structures so that the programmer can automatically create a new place
More informationKurt 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 informationECE551 Midterm Version 2
Name: ECE551 Midterm Version 2 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual
More informationActually, C provides another type of variable which allows us to do just that. These are called dynamic variables.
When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called
More informationCT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015
Solutions Q.1 a. What is a pointer? Explain how it is declared and initialized. (4) Different from other normal variables which can store values, pointers are special variables that can hold the address
More informationCS 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 informationMODULE 5: Pointers, Preprocessor Directives and Data Structures
MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two
More informationWeek 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 informationLecture 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 informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex
More informationProcedural 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 informationLinked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra
Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];
More informationStructures, Pointers, Linked Lists
Lecture 6: Structures, Pointers, Linked Lists Reminder: Pointers and structures Pointers to structures Dynamic memory allocation: malloc Linked lists Ordered linked lists Double linked lists, Trees Reminder:
More informationFOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;
CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has
More informationAshish Gupta, Data JUET, Guna
Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationSYSC 2006 CD. Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.
It is most beneficial to you to write this mock midterm UNDER EXAM CONDITIONS. This means: Complete the mock final in 180 minutes. Work on your own. Keep your notes and textbook closed. Attempt every question.
More informationStructures 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 informationClass 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 informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationECE551 Midterm Version 1
Name: ECE551 Midterm Version 1 NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual
More information211: Computer Architecture Summer 2016
211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:
More informationData Structure Series
Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya sourangshu@gmail.com h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering
More informationC 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 informationWrite a program that creates in the main function two linked lists of characters and fills them with the following values:
Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The
More informationHigh 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 informationArrays 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 informationRecitation #11 Malloc Lab. November 7th, 2017
18-600 Recitation #11 Malloc Lab November 7th, 2017 1 2 Important Notes about Malloc Lab Malloc lab has been updated from previous years Supports a full 64 bit address space rather than 32 bit Encourages
More informationAnnouncements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1
Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic
More informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Eight: Math Functions, Linked Lists, and Binary Trees Name: First Name: Tutor:
More informationadvanced 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 informationPointers and File Handling
1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY
More informationReg. No. : Question Paper Code : 27157
WK 3 Reg. No. : Question Paper Code : 27157 B.E./B.Tech. DEGREE EXAMINATION, NOVEMBER/DECEMBER 2015. Time : Three hours Second Semester Computer Science and Engineering CS 6202 PROGRAMMING AND DATA STRUCTURES
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationAPS105. 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 informationQuick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.
1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions
More informationA. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS
Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization
More informationSYSC 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 informationMODULE V: POINTERS & PREPROCESSORS
MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which
More informationa data type is Types
Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental
More informationThis document can be downloaded from with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS
1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS 4.1 MEMORY MANAGEMENT 4.1.1 Basics about Memory When a C program is compiled, the compiler translates the source code into machine code. Now the
More informationArrays 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 informationIntermediate 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 informationCS 241 Data Organization Binary Trees
CS 241 Data Organization Binary Trees Brooke Chenoweth University of New Mexico Fall 2017 Binary Tree: Kernighan and Ritchie 6.5 Read a file and count the occurrences of each word. now is the time for
More informationProgramming in C. Lecture Tiina Niklander. Faculty of Science
Programming in C Lecture 3 17.9.2018 Tiina Niklander Faculty of Science Department of Computer Science 17.9.2018 1 Week 2 covers Pointer basics operators and address arithmetics as function argument Precedence
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example
More informationPrograms in memory. The layout of memory is roughly:
Memory 1 Programs in memory 2 The layout of memory is roughly: Virtual memory means that memory is allocated in pages or segments, accessed as if adjacent - the platform looks after this, so your program
More informationLinked Lists in C and C++
Linked Lists in C and C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by
More informationESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists
ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 Introduction to Linked Lists Each bead connected to the next through a
More informationQuick review pointer basics (KR ch )
1 Plan for today Quick review pointer basics (KR ch5.1 5.5) Related questions in midterm Continue on pointers (KR 5.6 -- ) Array of pointers Command line arguments Dynamic memory allocation (malloc) 1
More informationDynamic Allocation in C
Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,
More informationBIL 104E Introduction to Scientific and Engineering Computing. Lecture 12
BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents
More informationCSC 1600 Memory Layout for Unix Processes"
CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant
More informationC mini reference. 5 Binary numbers 12
C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char
More informationDynamic 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 informationM.CS201 Programming language
Power Engineering School M.CS201 Programming language Lecture 16 Lecturer: Prof. Dr. T.Uranchimeg Agenda Opening a File Errors with open files Writing and Reading File Data Formatted File Input Direct
More informationAlgorithms, Data Structures, and Problem Solving
Algorithms, Data Structures, and Problem Solving Masoumeh Taromirad Hamlstad University DT4002, Fall 2016 Container Concepts containers store data container operations: insertion retrieval removal iteration
More informationDynamic memory. EECS 211 Winter 2019
Dynamic memory EECS 211 Winter 2019 2 Initial code setup $ cd eecs211 $ curl $URL211/lec/06dynamic.tgz tar zx $ cd 06dynamic 3 Oops! I made a mistake. In C, the declaration struct circle read_circle();
More informationCA341 - Comparative Programming Languages
CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data
More informationFORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE
TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)
More information