Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.

Slides:



Advertisements
Similar presentations
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Advertisements

C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
Dynamic memory allocation
C Language.
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.
Chapter 6 Data Types
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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.
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. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Informática II Prof. Dr. Gustavo Patiño MJ
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’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
Programming in C Advanced Pointers. Pointers to Pointers Since a pointer is a variable type, a pointer may point to another pointer.Since a pointer is.
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 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
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.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
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.
Stack and Heap Memory Stack resident variables include:
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Programming Languages and Paradigms
Dynamic Memory Allocation
Programming with Pointers
CS111 Computer Programming
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Variables in C Topics Naming Variables Declaring Variables
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data of a single type. Union numbers { char letter; int number; float dec_number; double precise_num; }; union numbers num1; num1.letter = 'A'; /* num1 stores 'A' */ num1.number = 5529; /* num1 stores 5529 */ num1.precise_num = 2.8; /* num1 stores 2.8 */

Union Example #include main() { union char_float { float x; char c[4]; }; union char_float buff; int i; float y = ; buff.x = y; for(i = 0; i < 4; i++) printf("%x ", buff.c[i]); } prints : fffffff4 2d 4e ffffffc4

Bit Fields Bit field is a way of saving storage space, such that each field occupies a given number of bits. Example: struct example1 { unsigned int f1 :3; unsigned int f2 :8; unsigned int f3 :4; }; struct example1 a,b c; a.f1 = 2; a.f2 = 100; a.f3 = 3; f1 takes 3 bits, f2 takes 8 bits, f3 takes 4 bits. a as a whole takes 16 or 32 bits depending on computer.

Enumerated Types An enumerated type is a data type with user-specified values (symbols): –keyword enum, followed by –an optional identifier of the enumerated type (tag), followed by –A list of names that are permissible value for this data type. Example: enum marital_status { single, married, divorced, widowed}; enum marital_status a, b, c; a = married;

Enumerated Type, Example #include main() { enum marital_status { single, married, divorced, widowed }; typedef enum marital_status MARITAL; MARITAL s1, s2; s1 = divorced; s2 = single; printf("\n%d\n", s1); printf("\n%d\n", s2); } the program prints: 2 0

Compile-Time and Run-Time Storage Allocation When we define a variable int count; the size of memory storage is already known before the program is actually run - count occupies one cell of type int. It is possible to defer the memory allocation until the program is run. This is called run-time storage allocation, or dynamics memory allocation.

Dynamic Memory Allocation Suppose that we want to use an array to storage a series of input data but the program does not known the size of the array until the user gives some inputs. (a) constant size array. This works if the input elements is less than 100, but wasteful if the input data is much less than 100. int a[100]; (b) dynamic memory allocation. Ask the user to enter the array size n, allocate exactly n cells of type int. int n, *a; scanf("%d", &n); a = malloc( n*sizeof(int) );

malloc() function The library function malloc() (in ) asks for memory from the computer system of required size in units of byte. If the allocation is successful, it returns a pointer to the newly allocated memory. If malloc is unable to allocate the requested storage, it returns NULL. #include... int *ptr; ptr = malloc(sizeof(int));

calloc() function The library function calloc() is like malloc() except that it takes two arguments, first for the number of cells and second argument for the byte size of each of the cells. #include... int *ptr; ptr = malloc(100 * sizeof(int)); or ptr = calloc(100, sizeof(int)); With calloc(), all cells are initialized to zero. With malloc() the cells are uninitialized (contain cabbage values).

Accessing Run- Time Storage If we allocate a cell by defining a variable, we can access it by using the variable's name or by dereferencing a pointer to the cell. If we allocate cells at run time, we can access it only through a pointer. int num, *p1, *p2; num = 10; /* access by naming */ p1 = &num; *p1 = 1000; /* dereferencing */ p2 = malloc(sizeof(int)); *p2 = 99999; /* only way is dereferencing */

Release Run-Time Storage The function free() releases (gives back) memory to the system. Memory should be released when it is no long used. #include... int *a;... a = calloc(5, sizeof(int)); a[2] =...;... free(a);

''Garbage'' Example #include main() { int *ptr; ptr = malloc(sizeof(int)); *ptr = 999; ptr = malloc(sizeof(int)); *ptr = -777; printf("\n%d\n", *ptr); free(ptr); } ptr 999

Dynamical Memory Allocations of Arrays Dynamical allocation of one-dimensional array is easy: double *a; /* define a pointer */ a = calloc(100, sizeof(double)); /* memory for 100 elements of type double created */ a[5] = 4.5; /* use a as an array */ Dynamical allocation for two dimensional array, say b[100][30]?

Two-dimensional array with calloc Define a pointer to pointer to double: double **b; Allocate a one-dimensional array of pointers to double b = (double **) calloc(100, sizeof(double*)); For each of the pointer to double, allocate an array of double, let the pointer pointing to it. for(i = 0; i < 100; ++i) { b[i] = (double *) calloc(30, sizeof(double)); } After this, you can use b as if it was declared as double b[100][30]; b[79][21] = 2.1;

Pointer to Pointer to Double b b[0] b[1] b[2] b[3] b[4] b[0][0] b[0][1] b[0][2] b[2][0] b[2][1]

Reading/Home Working Read Chapter 10/11, page 534 to 544, 550 to 565. Work on Problems –Section 10.8, page 537, exercise 1, 3. –Section 10.9, page 541, exercise 1, 5. –Section 11.1, page 557, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.