Associated Variables - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Associated Variables

Description:

sd[0].ID=1234567; sd[0].final=76.5; Gibbons | GNG1101-22: 7 ... returns to the caller the address of the first byte of the memory area that has been allocated. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 19
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Associated Variables


1
Associated Variables
  • Now we introduce structures, variable types which
    associate other variables.
  • Consider the rectangle
  • struct rect
  • int top
  • int bottom
  • int left
  • int right

top
left
right
bottom
2
Declaring rect Variables
  • struct rect
  • int top
  • int bottom
  • int left
  • int right
  • door, window
  • door is a rectangle (rect) having four landmarks
    and window is also a rect.

3
Accessing The Landmarks
  • door.top6
  • door.bottom0
  • door.left3
  • door.right4
  • window.top5
  • window.bottom1
  • window.left5
  • window.right7

3
4
5
7
6
5
1
0
4
Calculate Areas
  • da(door.top-door.bottom)
  • (door.right-door.left)
  • wa(window.top-window.bottom)
  • (window.right-window.left)

5
Assigning all Values
  • Copy values from door to window
  • windowdoor
  • Initialize while declaring
  • struct rect
  • int top
  • int bottom
  • int left
  • int right
  • door6, 0, 3, 4, window5, 1, 5, 7

6
Classlist
  • struct StudentData
  • char name
  • long ID
  • float final
  • sd700
  • sd0.name"Jones"
  • sd0.ID1234567
  • sd0.final76.5

7
Dynamic Memory Allocation
  • Dynamic allocation of memory means that a program
    obtains memory elements while it is executing.
  • Memory elements available for dynamic allocation
    are located in a region of memory called the
    heap dynamically allocated memory can only come
    from the heap.

8
  • C standard functions are available to help with
    the management of dynamically allocated memory
    they perform the necessary calls to the operating
    system in order to
  • fill the request for memory, or to
  • release memory that has been allocated
    dynamically when it is no longer needed.

9
  • The functions malloc and free, and the operator
    sizeof are essential to dynamic memory allocation
    (malloc and free are found in stdlib.h).

10
malloc
  • The function malloc obtains memory from the heap
    and makes it available for use in a C program.
  • The function malloc
  • takes one argument which is the number of bytes
    to be allocated
  • returns to the caller the address of the first
    byte of the memory area that has been allocated.
  • The NULL address is returned if the requested
    memory could not be allocated.

11
  • The number of bytes obtained from a call to
    malloc will always be located in contiguous
    memory locations.
  • The memory area allocated through malloc is
    accessible via the address returned, which must
    be assigned into a pointer variable.
  • Before the address is assigned to the pointer
    variable, it must be cast to the correct address
    type, defining in effect the type of the memory
    allocation and ensuring that any subsequent
    pointer arithmetic works properly.

12
Example of malloc
  • int intPtr
  • ?
  • intPtr(int ) malloc(sizeof(int))
  • intPtr9
  • The call to malloc obtains a memory allocation
    large enough for storing an int.
  • malloc returns the address of the first byte
    allocated which is cast to the address of an int
    and then assigned to intPtr, a pointer to an int.
  • The memory allocation can now be used just like
    an int but it must be manipulated via its pointer.

13
free
  • The function free de-allocates memory that has
    been previously allocated dynamically.
  • The function free takes an argument which is a
    pointer to the memory area that is to be
    de-allocated.
  • Based on the previous example
  • free(intPtr)
  • de-allocates the two bytes previously allocated
    by malloc to store an int.
  • The de-allocated bytes are returned to the heap.

14
Dynamically Allocated Arrays
  • Arrays can be created dynamically.
  • A C standard function is invoked to obtained all
    of the required memory elements, and
  • the array is managed using a pointer variable of
    the appropriate type.
  • Not valid
  • int n
  • scanf("d", n)
  • int arrayn

15
calloc
  • The C standard function calloc, defined in
    stdlib.h, is especially well-suited to obtaining
    memory dynamically for the storage of an array.

16
  • The function calloc takes 2 arguments
  • the first is the size of the array to be created,
  • the second is the number of bytes required to
    store a single element of the array this depends
    on the type of the array elements.
  • The function calloc returns to the caller the
    address of the first byte of the allocated memory
    area.
  • The NULL address is returned if the requested
    memory could not be allocated.
  • Always be located in contiguous memory.

17
malloc vs calloc
  • The function malloc can also be used to obtain
    the necessary memory but the argument sent must
    correspond to the total amount of memory needed.
  • The argument must be of the form
    malloc(size_of_arraysizeof(type))
  • Compare with
  • calloc(size_of_array, sizeof(type))

18
Example
int arrayPtr arrayPtr(int )calloc(3,
sizeof(int))
?
(arrayPtr0)
7010 7011 7012 7013 7014 7015
arrayPtr0
(arrayPtr1)
arrayPtr1
(arrayPtr2)
arrayPtr2
?
Write a Comment
User Comments (0)
About PowerShow.com