introduction to programming 3d applications ce0056-1 lecture 12 structure pointers and memory...

32
Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Upload: andrew-evans

Post on 04-Jan-2016

221 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Introduction to Programming 3D Applications

CE0056-1

Lecture 12

Structure Pointers and Memory Management in C

Page 2: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Purpose of structures

Computing applications often need data of

different kinds to be associated. A typical place

where this occurs is in a record. For example a

database record for a particular student might

contain the student number, full name, course-

code and year of entry.

Page 3: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Record declaration

struct student { char studno[12]; // or char *studno; char name[50]; char course[7]; int startyear;};

Page 4: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

More examples

struct coord { float x,y; };struct complex { float real,imaginary; };

The data does not have to be of different types. Structures help with the naming of related parts of a record object.

Page 5: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Allocating memory to records

The above declarations don't create any space for data storage, but they provide a template for data of the appropriate types to be allocated. Let's allocate some storage to data variables:

struct coord origin, cursor; /* 2 x-y coordinates called origin and cursor */struct student sdn_year2[50]; /* array for 50 students */

Page 6: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Defining a new data type

Better to have one word e.g. COORD to declare the type of data than 2 words e.g. struct coord. This needs a typedef statement:

struct coord { float x,y; };typedef struct coord COORD; /* COORD now same as struct coord */COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */

Page 7: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Use of the dot structure member access operator

Members x and y of origin and cursor can now be accessed as origin.x, origin.y, cursor.x and

cursor.y e.g.

origin.x=3.5;origin.y=2.0;

Page 8: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Programmer defined data type

We can now declare stuctured data with a single word in a similar way that we use other built-in data types such as float and int. This simplification is useful if the name of the data type is going to appear in many places within our program, e.g. for declaring types of local data within functions or function parameters.

It may not seem the case, but having many smaller functions which communicate cleanly is going to make programming much easier.

Page 9: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Functions which communicate using structure parameters

float distance(COORD a, COORD b){ /* calculate distance between a and b */ float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */ horiz=a.x - b.x; /* the horizontal distance */ vert=a.y - b.y; /* the vertical distance */ /* calc z as the hypotenuse of a right angle triangle */ z=sqrt((horiz*horiz) + (vert*vert)); /* Pythagorus */ return z; /* z*z = x*x + y*y */}

Page 10: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Functions which communicate using structured return values

COORD getcoord(void){ /* prompts for & returns a coordinate */ COORD temp; printf("Enter x and y coordinates \n"); scanf("%f%f",&temp.x,&temp.y); return temp; /* note returned type is COORD */}

Page 11: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

A program using distance and getcoord

#include <stdio.h>#include <math.h> /* needed to use sqrt() square root */ struct coord { float x,y; }; /* declare structure coord as having x and y members */typedef struct coord COORD; /* COORD is now same as struct coord */COORD origin, cursor; /* declare 2 x-y coordinates called origin and cursor */

/* function prototypes */float distance(COORD a, COORD b); COORD getcoord(void);

Page 12: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

A program using distance and getcoord

int main(void){ COORD origin, cursor; float gap; printf("enter details for origin:\n"); origin=getcoord(); printf("enter details for cursor:\n"); cursor=getcoord(); gap=distance(origin,cursor); printf("distance between origin and cursor is %f\n“,gap); return 0;}

Page 13: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Use of structure pointers

Members x and y of coordinates a and b can also be accessed through pointers to a and b If pointer p stores the address of a: p=&a; Then

p->x directly accesses member x of a or a.x

Page 14: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Rewrite previous program using pointers

/* declarations above here of headers , struct coord and typedef COORD same as before so not repeated */float distance(COORD *a, COORD *b); /* a and b are now pointers to COORD */void getcoord(COORD *t); /* inputs coordinate through COORD* pointer t */

Page 15: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Rewrite using pointersint main(void){ COORD origin, cursor, *orig,*curs; orig=&origin; curs=&cursor; /* store addresses in pointers orig and curs */ float gap; printf("enter details for origin:\n"); getcoord(orig); printf("enter details for cursor:\n"); getcoord(curs); gap=distance(orig,curs); printf("the distance between origin and cursor %f\n",gap); getch(); return 0;}

Page 16: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Rewrite using pointers

float distance(COORD *a, COORD *b){ /* calculate distance between a and b */float z,vert,horiz; /* z = distance, vert = y1 - y2, horiz = x1 - x2 */horiz=a->x - b->x; /* horizontal distance note -> pointer syntax */vert=a->y - b->y; /* the vertical distance *//* calculate z as the hypotenuese of a right angle triangle */z=sqrt((horiz*horiz) + (vert*vert)); /* pythagorus theorem: */return z;/* z*z = x*x + y*y */

}

void getcoord(COORD *t){ /* inputs x-y coordinate using pointers */ printf("please enter x and y coordinates\n"); scanf("%f%f",&t->x,&t->y); /* -> has higher precedence than & */}

Page 17: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Structures and arraysHaving individual names for 10 coordinates is very awkward. We can declare the data as an array:

typedef struct coord { float x,y; } COORD;COORD graph[10];

We can then assign members of our structure array like this:

graph[9].x = 12.5; graph[9].y = 7.3; /* assign values to tenth element [9] of graph array */

Page 18: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

input values into all coordinates using a loop

int i; for(i=0;i<10;i++){ printf("Enter x value for coordinate%d\n" ,i+1); scanf("%f",&graph[i].x); printf("Enter y value for coordinate%d\n" ,i+1); scanf("%f",&graph[i].y);}

Page 19: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Or using structure pointer notation

int i;

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

printf("Enter x value for coordinate%d\n" ,i+1);

scanf("%f",&(graph+i)->x);

/* -> has higher precedence than & */

printf("Enter y value for coordinate%d\n" ,i+1);

scanf("%f",&(graph+i)->y);

}

Page 20: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

A structure containing an arraytypedef struct student { char name[30]; float mark;} STUDENT;

STUDENT you;printf ("enter name and mark\n");scanf ("%s%f",you.name,&you.mark);

/* you.name is the address of the name array *//* so no &ampersand needed to get address*//* BUT you.mark is not an array so needs & */

printf ("hello %s your mark is %f\n",you.name,you.mark);printf ("The first letter of your name is %c\n",you.name[0]);

Page 21: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Assigning structuresUnlike arrays, structures can be copied as a complete entity using a single assignment. This savestime. So instead of writing:

strcpy(temp.name,you.name); /* use function strcpy to copy a string */temp.mark = you.mark; /* can assign mark in one go */

we can write:

temp=you; /* copy structure in one go */

Page 22: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Allocation of Static Memory

A declaration such as char name[1000][21], allocates sufficient memory for 1000 names of 21 characters each.

This storage of 21 000 characters is reserved at compilation time, i.e. before the program begins execution.

This wastes space as most names are rather less than 20 letters.

It also restricts names to a maximum of 20 characters. We can resolve this wastage and restriction by allocation of

memory as and when required, during execution of the program.

Page 23: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Dynamic Allocation using malloc function

A standard function, malloc(n) allocates n bytes (for n characters) in memory and returns a pointer to it.

For example: the following code Prompts the user for the size of the string Reads the size Allocates memory for a string of this size Reads the string

Page 24: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Example using malloc

int size;

char *s;

printf(“\nhow many characters?\n”);

scanf(“%d”, &size);

s = malloc(size+1); // one extra for ‘\0’

printf(“type string\n”);

gets(s);

allocates only enough memory for the expected string.

Page 25: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Use of malloc for any data type

malloc can be used to allocate memory for any <data type>.

<data type> *<variable>; // pointer to <data type>

// some other code here

<variable> = (<data type> *)malloc(n * sizeof(<data type>));

allocates sufficient memory for n values of <data type> and returns a pointer which must be casted as the same pointer type as the <variable>.

Page 26: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Using malloc for an array of integers

For example:

To allocate storage for 10 integers

int *p;

p = (int *) malloc (10 * sizeof(int));

This is machine-independent, as sizeof returns

size of an integer for any machine.

Page 27: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Ragged Arrays

Declare 6 variable length names – array of string pointers

char *name[6] =

{“abdul”, “abraham”, “al “, “bill”, “fred”, “jean-pierre”};

We can declare functions on ragged arrays:

void print_list(char *table1[ ], int n)

so that it can deal with strings of any length.

Page 28: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Ragged array example

Read a list of strings (one on each line) into an array wherethe size of each element is precisely the length of eachstring, ie the strings are of variable length - a ragged array.

#include "stdio.h"#include "string.h"#include "stdlib.h" // for malloc// prototypes

void sort(char *[], int);int read_strings(char *[], int);void print_strings(char *[], int);

Page 29: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

Main function

int main(void){char *list[1000];

// array of 1000 string pointers int n; // actual number of strings n = read_strings(list, 1000);

// read n strings, max of 1000 sort(list, n); // sort n strings print_strings(list, n); // print n strings return (0);}

Page 30: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

print_strings function

void print_strings(char *list[], int n)

{

int i;

printf("\nalphabetical order is\n\n");

for (i=0; i<n; i++)

{

printf("%s\n", list[i]);

}

}

Page 31: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

void sort(char *a[], int n) { // array of n strings as pointers

int i, j;char *temp; // string pointerfor (j = n -1; j > 0; j--){ // each pass of j

comparisonsfor (i=0; i < j; i++){ // each comparison

if (strcmp(a[i], a[i+1]) > 0)

{ // swap the POINTERS temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } // else no swap } // end of pass } // end of all passes} // end sort

Page 32: Introduction to Programming 3D Applications CE0056-1 Lecture 12 Structure Pointers and Memory Management in C

int read_strings(char *list[], int max){

int i = 0;char line[81]; // store for each lineprintf("\ntype one string per line\n");while (gets(line) != NULL && i < max) { // while not end of input

list[i] = malloc (strlen(line)+1); // list[i] big enough

// for line + 1 for ‘\0’

strcpy(list[i], line); // copy line into list[i]

i++; // next element}return i; // actual number of strings

}