C Programming Lecture 14 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic memory allocation
An introduction to pointers in c
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming and Data Structure
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.
Pointers in C Rohit Khokher
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Applications
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic memory allocation and Pointers Lecture 4.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic Allocation in C
Stack and Heap Memory Stack resident variables include:
POINTERS.
Programming Languages and Paradigms
Programming and Data Structures
Lecture 18 Arrays and Pointer Arithmetic
Programming with Pointers
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

C Programming Lecture 14 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Pointer Notation vs. Array Notation Topics Use pointer notation to access array elements and strings C allows array data to be accessed using pointer notation as well as array notation.

Pointer Notation vs. Array Notation How to use pointer notation to access the elements of a 1-D character array? Using either array or pointer notation Access the element indicated by aa[16] putchar(aa[16]); putchar(*(aa+16));

Pointer Notation vs. Array Notation What logic underlies this notation? Involves pointer arithmetic in the form of adding an integer to an address. C performs this type of operation by first noting the type of address. aa + 16 aa is the address of the beginning of a 1-D array of characters. Since characters occupy 1 byte of memory, C adds 16 bytes to the address indicated by aa. Gives us the address of the character indicated by aa[16], the 17th character. Using the unary * operator, the expression *(aa+16) gives us the value of the 17th character.

Pointer Notation vs. Array Notation How to use pointer notation to access the elements of a 2- D array? Using either array or pointer notation Access the element indicated by bb[3][5] putchar(bb[3][5]); putchar(*(*(bb+3)+5)); C regards bb[ ][ ] as an array of arrays Five 1-D arrays of size 40 The address expressed as bb is regarded an address of the beginning of one of the rows. An individual element of that address is considered to have a size 40, not 1! When we add bb and 3, we add 3*40 = 120 bytes to the address indicated by bb.

Pointer Notation vs. Array Notation *(bb+3) represents the address (not the value, as occurs when working with 1-D arrays or single pointer variables) of the beginning of the forth row. char bb[5][40]={"We can", "use both", "array and pointer", "notation to access", "one and two-dimensional arrays"}; putchar(*(*(bb+3)+5)); causes the character i to be printed. With a 2-D array, need two unary * operators With a 3-D array, need three unary * operators

Pointer Notation vs. Array Notation What does *(bb+2) represent? Equivalent to bb[2] Represents the address of the beginning of the third row in the bb[ ][ ] array. puts(*(bb+2)) causes the string “array and pointer” to be printed.

Pointer Notation vs. Array Notation With the declaration char *dd, what does dd+125 indicates? The variable dd indicates an address. The 125 causes 125 bytes to be added to the address indicated by dd. dd = &bb[0][0]; putchar(*(dd+125)); Print the character i, which is the 126th character in the bb[ ][ ] array.

Pointer Notation vs. Array Notation Can we use dd = bb, instead of dd = &bb[0][0]? No, because C considers it to be a type mismatch. dd is a pointer variable. Cannot take on the characteristics of an array address bb is the address of a 2-D array. Would putchar(*(bb+125)); print the 126th character of the bb[ ][ ] array? No; first, the statement would not compile since two unary * operators are needed to point to a single character. Pointer arithmetic would not advance 125 bytes but 125x40 = 5000 bytes.

Pointer Notation vs. Array Notation How to use pointer notation to access the element of a 3-D array? For instance, putchar(cc[1][2][3]); putchar(*(*(*(cc+1)+2)+3)); C regards 3-D arrays as arrays of arrays, cc[2][3][20] is considered two 2-D arrays of size [3][20] The address expressed as cc is regarded an address of the beginning of one of the 2-D arrays

Pointer Notation vs. Array Notation *(cc+1) represents the address of the beginning of the second 2-D array. *(cc+1)+2 The address is to the beginning of the next 2-D array using one unary * operator The integer 2 causes the addition of 2*20 = 40 bytes. *(*(cc+1)+2) represents the address of the beginning of the third row of the second 2-D array *(*(*(cc+1)+2)+3) putchar(*(*(*(cc+1)+2)+3)); Print out a single character.

Pointer Notation vs. Array Notation How to use putchar with a single unary * operator to print a character from the cc[ ][ ][ ] array? Assign the address of the first character of the cc[ ][ ][ ] array to the single pointer variable, dd. Use a single unary * operator to access individual values of the cc[ ][ ][ ] array. dd = &cc[0][0][0]; putchar(*(dd+80));

Pointer Notation vs. Array Notation What do cc, *cc, **cc represent? All three represent the address of the first element of the cc[ ][ ][ ] array. cc+1 = address of first element + 60 bytes *cc+1 = address of first element + 20 bytes **cc+1 = address of first element + 1 byte

Pointer Notation

Pointer Arithmetic Have the following declarations for 1-D arrays. int aa[50]; float bb[100]; double cc[77]; DeclarationBytes per elementOperationAction int aa[50]2 aa+20Adds 2*20 = 40 bytes float bb[100]4 bb+12Adds 4*12 = 48 bytes double cc[77]8 cc+9Adds 8*9 = 72 bytes

Direct Correspondence Pointer notationArray notation *(aa+20)aa[20] *(bb+12)bb[12] *(cc+9)cc[9]

/* Program for Lesson 7_9*/ #include void main(void) { char aa[35]={"This is a one-dimensional array"}; char bb[5][40]={"We can","use both","array and pointer", "notation to access","one and two-dimensional arrays"}; char cc[2][3][20]={"A three ","dimensional ","array ","is ","shown ","also"}; char *dd; printf("**************** Section 1 1-D array ****************\n"); putchar(aa[0]); putchar(*aa); putchar('\n'); putchar(aa[16]); putchar(*(aa+16)); putchar ('\n'); printf("**************** Section 2 2-D array ****************\n"); putchar(bb[3 ][5] ); putchar(*(*(bb+3)+5)); putchar('\n');

puts(*(bb+2)); dd=&bb[0][0 ]; putchar(*(dd+125)); putchar('\n'); printf("**************** Section 3 3-D array ****************\n"); putchar(cc[1][2 ][3]); putchar(*(*(*(cc+1)+2)+3)); putchar('\n'); puts(*(*(cc+1)+2)); dd=&cc[0][0][0]; putchar(*(dd+80)); putchar('\n'); puts(dd+80); printf("Address of cc[0][0][0]=%p, cc+1=%p, *cc+1=%p, **cc+1=%p\n", &cc[0][0][0],cc+1,*cc+1,**cc+1); }

Dynamic Memory Allocation Topics Reserving memory during execution Using calloc, malloc, and realloc Large and small amounts of memory Reserve memory based on the size of problem Illustrate the functions (calloc, malloc, realloc, and free) Utilize dynamic memory features A 1-D character array, aa[19], and a 1-D pointer array, bb[22] A fixed-size 2-D array, cc[22][19] Specify how much memory is to be reserved. calloc() and malloc()

The Function calloc( ) What does the function calloc do? The function calloc() reserves memory during program execution. The amount of memory is determined by the arguments passed to calloc(). calloc (number_of_elements, bytes_per_element) The product of the integers number_of_elements and bytes_per_element. calloc (xx, sizeof(char)); causes memory for xx elements of sizeof(char) (which is 1 byte) to be reserved.

The Functions calloc( ) and malloc( ) The function calloc() returns the address of the beginning of the first element reserved. bb[0] = (char *) calloc (xx, sizeof(char)); Causes the address of the beginning of the memory reserved to be stored in the first element of the pointer array, bb[ ]. The cast operator (int *) or (double *) can be used with calloc(). What does the function malloc( ) do? The amount of memory reserved is determined by the single argument passed to malloc( ). malloc (number_of_bytes);

The Function malloc( ) The amount of memory reserved is equal to number_of_bytes. yy = xx*sizeof(char); malloc (yy); bb[1] = (char *) malloc(yy); Causes the address of the beginning of the memory reserved to be stored in the second element of the pointer array, bb[ ]. The cast operator (int *) or (double *) can be used with malloc().

The Function realloc( ) The function realloc( ) modifies the amount of memory previously reserved by a call to either malloc or calloc. realloc (pointer, number_of_bytes); The amount of memory reserved is equal to number_of_bytes. The location of the memory reserved is indicated by pointer. The argument pointer must have been returned by a previous call to either calloc or malloc. realloc(bb[1], yy); Reserve different block of memory bb[1] = (char *) realloc(bb[1], yy);

The function free( ) What does the function free( ) do? Cancels the reservation for memory previously made by calloc, malloc, or realloc. free (pointer); Cannot reserve the memory as requested? Return a null pointer. Check the returned null pointer to make sure that memory has been successfully allocated.

Conceptual Image of Regions of Memory Where in memory is the space reserved? A region called the stack. A region called the heap. A region called the variables. A region called the instructions.

A fixed-size 2-D Array Storage

Array Storage Using Dynamic Method

Using strlen and calloc to create dynamic storage strcpy (aa, cc[0]); xx = strlen(aa); bb[0] = (char *) calloc (xx, sizeof(char)); strcpy (bb[0], aa); Thus, we have been able to use C’s dynamic memory management capabilities.

/* Program for Lesson 7_10*/ #include void main(void) { char aa[19]; char *bb[22]; char cc[22][19]={"Example", " String 1 ", "Words"}; int xx, yy; printf("*********** Section 1 - Using calloc *************\n"); strcpy(aa,cc[0]); xx=strlen(aa); bb[0]=(char *)calloc(xx,sizeof(char)); strcpy(bb[0],aa); puts(bb[0]);

printf("*********** Section 2 - Using malloc *************\n"); strcpy(aa,cc[1]) ; xx=strlen(aa); yy=xx*sizeof(char); bb[1]=(char *)malloc(yy) ; strcpy(bb[1],aa); puts(bb[1]) ; printf("*********** Section 3 - Using realloc *************\n"); strcpy(aa,cc[2]); xx=strlen(aa); yy=xx*sizeof(char); bb[1]=(char *)realloc(bb[1],yy); strcpy(bb[1],aa); puts(bb[1]) ; free(bb[1]); }

Program Development Methodology Assemble relevant equations and background information. Work a specific example. Decide on the major data structure to be used. Develop an algorithm, structure charts, and data flow diagrams. Write source code.