1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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 and Memory Overview
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Pointers and Arrays Beyond Chapter 16
Dynamic Memory A whole heap of fun….
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation

2 Memory and PointersSIMPLE TYPES When we create a variable in a program, we use the name of the variable to access its value. The computer only knows about memory locations (addresses) and contents (values). The compiler translates our instructions into something meaningful to the computer, int i, j; allocates storage for two integers (sizeof(int), 4 bytes) and remembers their addresses i may be at address 100 and j at 104. i = j; is translated by the compiler into: copy 4 bytes at location 104 to location 100

3 C-MemorySIMPLE TYPES When we include a numeric or character constant in a program: i = 6; i = 6; 6 the compiler places the value '6' into the machine instruction. If we also have j = 6; j = 6; 6 there will be two '6' s in our executable file.

4 C-MemoryARRAYS When we create an array, the compiler allocates as much memory as requested and remembers the address of the first element. char s[80]; allocates 80 bytes starting at 108 *. *that is, continuing with our previous example

5 C-Memory Array name The name 's' is different from 'i‘ 'i' refers to the value stored at address 100 's' refers to the address 108 We are not allowed to say: s = 'a'; To access the value stored at s: s[0] = 'a'; or (*s) = 'a'; The indirection operator * s is an address. The indirection operator * accesses the contents stored at the address.

6 C-Memory ARRAYS s[i] *(s+i) s[i] is a shorthand way of writing *(s+i) We can add an integer to an address – the result is an address. (The integer is first multiplied by the size of the element being pointed to) The result of subtracting two addresses is an integer. The number of elements. We cannot multiply an address with an integer. Adding addresses Subtracting addresses Multiplying addresses

7 C-Memory ARRAY CONSTANTS A string cannot be 'part of an instruction'. 48 Instructions are fixed length – 4 or 8 bytes. The compiler allocates memory for the string and remembers its location. strcpy(s,"Napoleon"); "Napoleon" is stored in memory at address bytes are allocated (8 plus a NULL). The text "Napoleon" is stored in the executable file and is placed at location 188 when the program starts.

8 C-Memory ARRAY CONSTANTS If the constant appears twice in the program: strcpy(s,"Napoleon"); strcpy(t,"Napoleon"); the compiler has the choice of creating two separate memory areas each with the word "Napoleon", or using the same area for both. To save space it will normally do the latter.

9 C-Memory Size of ARRAYS Do we need to say how big arrays are? - Yes, otherwise the compiler does not know how many bytes to allocate. What about when we initialise strings? char s[] = "Napoleon"; We don't need to give the size of the string – but the compiler works it out itself. See AddressArray.cpp

10 C-Memory 2-D ARRAYS What about 2 dimensional arrays? The compiler fools us a little – there is no such thing as a 2D array. Only 1D arrays. char array[20][80]; allocates 1600 bytes of memory. 'array' is the address of the first byte. array[3][5] is converted by the compiler to: *(array + 3*80 + 5) similarly for 3 and 4 dimensional arrays. See AddressArray.cpp

11 C-Memory Specifying the array dimension at run-time Can we specify the dimension at run-time? - Not unless we use pointers. A pointer is a variable that contains an address. char *p; Now we have two addresses! p is a variable, its value is an address. This is the address we normally want. p is somewhere in memory so it has an address associated with its own position. & The & operator gets the address of a variable. char *p; char c; p = &c; p is at location 100 c is at location 104 the contents of p is 104. The indirection operator * accesses the contents of an address

12 C-MemoryUsing the subscripting operator with pointers (*p) = ‘N'; pointers and arrays look very similar! We can use the [ ] operator with pointers p[0] = ‘N'; 0 p points to a single character, so we can only access the 0'th element. fail Writing to p[1] causes the program to fail. If we allocate memory from the heap, p can point to as many bytes as we want: Subscript operator void *malloc(size_t size);

13 C-Memorymalloc p = (char *)malloc(80*sizeof(char)); allocates 80 bytes. p contains the address of the first character. malloc can have a variable as an argument, so the size of the 'string' p can be set at runtime. char *p; int i; printf("How big is the string:"); scanf("%d", &i ); p = (char *)malloc(i*sizeof(char)); void *malloc(size_t size);

14 C-Memory Declaring an ARRAY vs. Declaring a Pointer char s[80]; char *p; p = (char *)malloc(80*sizeof(char)); p and s behave very much the same. p[12] = 'a'; s[12] = 'b'; strcpy(p,"Napoleon"); strcpy(s,"Napoleon"); printf("%s",p); printf("%s",s); There is a difference: char s[80]; char *p; char c; p = &c; this is certainly ok s = &c; this is NOT s s is not variable. Its value cannot change.

15 C-Memory ARRAYS Be very careful when 'initialising' a pointer char *p = "Napoleon"; If "Napoleon" is a constant, used in many places in the program then p[0] = 'X'; Will change all “Napoleon"s into "Xapoleon".

16 C-Memory 2D arrays on the heap? Several possibilities, but the syntax becomes very difficult. char (*p)[80]; p = (char (*)[80])malloc(10*80*sizeof(char)); p[0][0] = 'a'; is correct (and the round brackets are required!) typedef char string[80]; string *q; q = (string *)malloc(10*sizeof(string)); is equivalent and looks better, although the typedef is a bit strange. char (*p)[80]; //pointer to a char array char *(p[80]); //array of pointers to char only the very brave or foolish use these.

17 C-Memory POINTER TO A POINTER A third method is more common and uses a pointer to a pointer. char **p; We have to do more work to set this up as a 2D array, but we have a lot more flexibility in the size of the array. The number of rows in the array is set first, by making p point to an array of pointers. p = (char **)malloc(rows*sizeof(char *)); each pointer is then set to point to an array of characters. for (i=0; i<row; i++) { p[i] = (char *)malloc(cols*sizeof(char)); } p is a pointer to a pointer p[i] is a string p[i][j] is a character

18 C-Memory 2-D Array doesn’t have to be square We can still use the [] operator to access elements of each array. We do not need to allocate the same number of characters to each string – the array does not have to be square. for (i=0;i<row;i++) { (i+1) p[i] = (char *)malloc((i+1)*sizeof(char)); } When we have finished with memory allocated from the heap, we must give it back. char *p; p = (char *)malloc(80*sizeof(char)); … free(p); In the case of pointers to pointers, we must be careful to free memory in the correct order. for (i=0;i<row;i++) { free(p[i]); } free(p); Do not access heap memory after it has been free'd.

19 C-Memory How does f ff free know how much memory to release? malloc has to remember how many bytes were allocated. How it does this is not specified, but one common way is to allocate an extra 4 bytes, at the start, that contain the size. If we write to p[-1] at any time, then our program will explode when we free(p) stack Local variables are kept on a 'stack'. This is created each time a function is called. The stack is destroyed when a return is executed. The stack is not set to zero each time it is created (too expensive) – and so local variables contain arbitrary data. Local variables need to be initialised.

20 C-Memory POINTERS A pointer may be on the stack – but what it points to may be on the heap. Failing to free heap memory before exiting a function will leave memory on the heap that no longer has a pointer pointing to it. memory leak This memory can never be recovered – it is called a memory leak. Get into the habit of always freeing memory. Even when the program exits.

21 C-Memory POINTERS When a function wants to change the value of an actual parameter, an address must be passed to the function. For strings the syntax looks ok, because the name is itself an address: strcpy(s,“Napoleon"); … void strcpy(char *p, char *q) { inside strcpy we access the original using p[i]. To alter an integer the syntax is more clumsy twice(&i); … void twice(int *ip) { (*ip) = (*ip)*2; }

22 C-Memory POINTERS We have to be brave to alter a pointer! char *s; allocate_and_check(&s); … void allocate_and_check(char **p) { (*p) = (char *)malloc(80); if ((*p) == NULL) { … It is a lot easier to use return values. newdelete C++ tidies up the problems associated with malloc and free by introducing 2 new keywords, new and delete. Reference parameters also are a new feature, to make it easier to alter arguments.

23 C-Memory POINTERS & MULTIDIMENSIONAL ARRAYS int (*x)[20]; int x[10][20]; rather than … … … x (x +1) (x +2) First array Second array Third array … … *(x +2) *(x +2)+5 *(*(x +2) + 5) 2D INTEGER ARRAY (x+1) automatically takes into account the number of elements in an array See AddressArray2.cpp