BCS2122DCS2192 Data Structure and Algorithm - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

BCS2122DCS2192 Data Structure and Algorithm

Description:

All of the program up to this point have used static variables as we are concerned. ... It is possible, using this techniques, to create as many ... Casting ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 8
Provided by: notesU
Category:

less

Transcript and Presenter's Notes

Title: BCS2122DCS2192 Data Structure and Algorithm


1
BCS2122/DCS2192Data Structure and Algorithm
  • Module 4 Dynamic Allocation
  • Muhammed Ramiza Ramli

2
Introduction
  • All of the program up to this point have used
    static variables as we are concerned.
  • Fixed number and size.
  • It is possible, using this techniques, to create
    as many variables as needed, use them, and
    deallocate their space so that it can be used by
    other dynamic variables.

3
Example
  • Refer to dynalist.c file source.

4
Observation
  • We defining a structure car with a few
    fields.
  • We do not define any variables of this type, only
    pointers.
  • We work with these three pointer which point to
    the defined structure.
  • In order to do anything we need some variables,
    so we will create some dynamically

5
The malloc function
  • car1 (CAR )malloc(sizeof(CAR))
  • malloc simply ask the system for a block of
    memory of the size specified, and gets the block
    with the pointer pointing to the first element of
    the block.

6
Casting
  • The malloc function returns a block with the
    pointer pointing to it being a pointer of type
    char by default.
  • We can define the pointer type with a construct
    to match our pointer.
  • Even if we omit the cast, most compiler will
    return a pointer correctly, give a warning.

7
free
  • Any dynamically memory block allocated by malloc
    can be deallocated by using free function.
Write a Comment
User Comments (0)
About PowerShow.com