Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 */

2 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

3 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.

4 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;

5 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

6 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.

7 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) );

8 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));

9 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).

10 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 = # *p1 = 1000; /* dereferencing */ p2 = malloc(sizeof(int)); *p2 = 99999; /* only way is dereferencing */

11 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);

12 ''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

13 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]?

14 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;

15 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]

16 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.


Download ppt "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."

Similar presentations


Ads by Google