c programming dynamic memory allocation structures
Download
Skip this Video
Download Presentation
C Programming : Dynamic memory allocation & Structures

Loading in 2 Seconds...

play fullscreen
1 / 17

C Programming : Dynamic memory allocation & Structures - PowerPoint PPT Presentation


  • 169 Views
  • Uploaded on

C Programming : Dynamic memory allocation & Structures. 2008/11/19 Made by Jimin Hwa ([email protected]) Edited and presented by Souneil Park ([email protected]). Contents . Dynamic memory allocation Structure Linked list. Motivation of Dynamic Allocation.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' C Programming : Dynamic memory allocation & Structures' - ciro


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c programming dynamic memory allocation structures

C Programming :Dynamic memory allocation & Structures

2008/11/19

Made by Jimin Hwa ([email protected])

Edited and presented by Souneil Park ([email protected])

contents
Contents
  • Dynamic memory allocation
  • Structure
  • Linked list
motivation of dynamic allocation
Motivation of Dynamic Allocation
  • The allocated space of an array is not flexible
    • What if more space is required?
    • Data insertion/deletion is difficult

char []

“CC510”

int []

1

2

3

4

5

6

7

8

9

10

dynamic memory allocation
Dynamic Memory Allocation
  • malloc
  • Allocates size bytes and returns a pointer to the allocated memory.

void* malloc(size_t size) /* <stdlib.h> : malloc/calloc/realloc */

int *pi;

int size, i;

scanf(“%d”, &size);

pi = (int *)malloc(sizeof(int) * size); // (type): static type casting

for (i = 0; i < size; i++) // Initialization

pi[i] = -1;

dynamic memory allocation cont d
Dynamic Memory Allocation (Cont’d)
  • calloc – initializes memory before allocation
  • Allocates size bytes, initialize the space with 0, and returns a pointer to the allocated memory

void* calloc(size_t nmeb, size_t size)

int *pi;

int size, i;

scanf(“%d”, &size);

pi = (int *)calloc(size, sizeof(int)); // (type): static type casting

dynamic memory deallocation
Dynamic Memory Deallocation
  • Synopsis
    • Frees the memory space pointed by ptr.
  • Frees(deallocates) the dynamically allocated memory space
  • void free(void *ptr) /* <stdlib.h> : free */

int *pi;

int size, i;

scanf(“%d”, &size);

pi = (int *)malloc(sizeof(int) * size);

if (pi != NULL) {

free(pi);

pi = NULL;

}

structure
Structure
  • A structure is a collection of variables, possibly of different types, grouped together under a single name.
  • Using structure, we can keep together different pieces of information as a single data record
    • Data packing mechanism
    • Example>

structure student

attributes: Name, student ID, and grade

structure cont d
Structure(Cont’d)

struct student { /* struct struct_name{ */

char name[32]; /* definition of members */

int student_id; /* }; */

char grade;

};

struct class {

struct student member[100];

int num_of_students;

int average_grade;

};

int main(){

struct class CC510; /* struct struct_name var_name */

}

structure operators
Structure Operators
  • Operator for member access : “.” (dot)
    • connects the structure name and the member namestructure_name.member
    • Example> struct student john;

john.grade = ‘A’;

john.student_id = 4473;

int id = john.student_id;

  • operator for member access (pointer) : ->
    • Exmple> struct student *pJohn;

pJohn->grade = ‘A’; // (*pJohn).grade = ‘A’;

pJohn->student_id = 4473;

int id = john->student_id;

structure operators cont d
Structure Operators(Cont’d)
  • Assignment between structure variable: “=“
    • Example>

struct student John;

struct student John_clone;

John.name = “John”;

John.grade = ‘A\';

John.student_id = 945433;

John_clone = John; /* John_clone.name = = “John”, John_clone.grade = = ‘A‘, John_clone.student_id = = 945433

*/

structure comparison
Structure Comparison
  • We need to compare all fields one by one explicitly.
  • Example>

if(john == john_clone){ … } /* Wrong */

if(strcmp(John.name, john_clone.name) == 0

&& John.student_id == john_clone.student_id

&& …){…} /*Right */

structure as a function argument
Structure as a Function Argument
  • They are passed to functions by the usual parameter-passing mechanism
    • The entire structure is copied to the function
  • Example>

int fail(struct student of_classA[], int size)

{

inti, count = 0;

for (i=0; i < size; ++i){

if(of_class[i].grade == ‘F’) count ++;

}

return count

}

linked list
Linked List
  • An alternative to array.
  • Data structure in which objects are arranged in a linear order
    • Consists of nodes, each containing arbitrary data fields and link pointing to the next nodes.
    • The size of a linked list would be changed in runtime.

Node

8

2

1

7

7

7

HEAD

X

Data field

Link

linked list implementation
Linked List Implementation
  • An node is represented in a C structure.
  • malloc() is used to dynamically create node structure

typedef struct node_t {

int data;

struct _node_t *next;

} node_t;

node_t head;

head.next = NULL;

/* Linked list is empty */

node_t* create_node(int d)

{

node_t *n =

(node_t*)malloc(

sizeof(node_t));

if (!n) return NULL;

n->data = d;

n->next = NULL;

return n;

}

lab 7 1
LAB #7 - 1
  • Implement a circular list
    • Get input from users (through standard input)
      • Input is an integer
    • Whenever a user enters an positive integer,
      • Store the input at the last
      • First, print the 1st, 3rd, 5th, … element
      • Then, print the 2nd, 4th, 6th, … element
    • If zero or a negative integer is entered, terminate the program.
lab 7 2
LAB #7 - 2
  • Implement a clist_copy function which copies the circular list
  • (struct xxx *) clist_copy (struct xxx *head) {

….

}

  • Which returns the head of the copied circular list
  • In the main function,
    • Print the head address of the original list and the copied list
    • Print the elements of the copied circular list in a sequential order
the end

The End

Any Question?

ad