Wednesday Week 04
Wednesday Week 04 | 1/16 |
Dynamic Memory Allocation | 2/16 |
So far, we have considered static memory allocation
int x;// 4 bytes containing a 32-bit integer value char *cp;// 4 bytes containing address of a char typedef struct {float x; float y;} Point; Point p;// 8 bytes containing two 32-bit float values char s[20];// array containing space for 20 1-byte chars
... Dynamic Memory Allocation | 3/16 |
In many applications, fixed-size data is ok.
In many other applications, we need flexibility.
Examples:
char name[MAXNAME];// how long is a name? char line[MAXLINE];// what's the longest line? char words[MAXWORDS][MAXWORDLENGTH];// how many words are there? // how long is each word?
With fixed-size data, we need to guess sizes ("large enough").
... Dynamic Memory Allocation | 4/16 |
Fixed-size memory allocation:
Dynamic Data Example | 5/16 |
Problem:
stdin
6 25 -1 999 42 -16 64
How to define the vector?
... Dynamic Data Example | 6/16 |
Suggestion #1: allocate a large vector; use only part of it
#define MAXELEMS 1000// how many elements in the vector int numberOfElems; scanf("%d", &numberOfElems); assert(numberOfElems <= MAXELEMS);// declare vector and fill from stdin int i, vector[MAXELEMS]; for (i = 0; i < numberOfElems; i++) { scanf("%d", &vector[i]); }
Works ok, unless too many numbers; usually wastes space.
assert()
... Dynamic Data Example | 7/16 |
Suggestion #2: use variables to give object sizes
// how many elements in the vector int numberOfElems; scanf("%d", &numberOfElems);// declare vector and fill from stdin int i, vector[numberOfElems]; for (i = 0; i < numberOfElems; i++) { scanf("%d", &vector[i]); }
Produces compiler error (compiler needs to know object sizes)
... Dynamic Data Example | 8/16 |
Suggestion #3: create vector after count read in
// how many elements in the vector int numberOfElems; scanf("%d", &numberOfElems);// declare vector and fill from stdin int i, *vector; size_t numberOfBytes; numberOfBytes = numberOfElems * sizeof(int); vector = (int *)malloc(numberOfBytes); assert(vector != NULL); for (i = 0; i < numberOfElems; i++) { scanf("%d", &vector[i]); }
Works unless the heap is already full (very unlikely)
The malloc() | 9/16 |
Recall memory usage within C programs:
... The malloc() | 10/16 |
malloc()
void *malloc(size_t n);
What the function does:
n
NULL
size_t
unsigend int
... The malloc() | 11/16 |
Example use of malloc
... The malloc() | 12/16 |
Things to note about void *malloc(size_t)
stdlib.h
(void *)
NULL
* sizeof(
)
... The malloc() | 13/16 |
malloc()
Things to note about objects allocated by malloc()
free()
malloc()
... The malloc() | 14/16 |
Usage of malloc()
int *vector, length, i; ... vector = (int *)malloc(length*sizeof(int));// but malloc() might fail to allocate assert(vector != NULL);// now we know its safe to use vector[] for (i = 0; i < length; i++) { ... vector[i] ... }
Alternatively:
int *vector, length, i; ... vector = (int *)malloc(length*sizeof(int));// but malloc() might fail to allocate if (vector == NULL) { fprintf(stderr, "Out of memory\n"); exit(1); }// now we know its safe to use vector[] for (i = 0; i < length; i++) { ... vector[i] ... }
Exercise: Creating Objects via malloc() | 15/16 |
Give variable declarations and statements that on the heap will create:
K
L
Car
typedef struct {char rego[7]; float premium;} Car;
Tips for Next Week's Lab | 16/16 |
Structured data types
3 exercises
boss.salary = 675000.00; strcpy(boss.name, "John Elliot"); printf("Name: %s\n", boss.name);
typedef struct {...} WorkerT; WorkerT worker[1500]; for (i = 0; i < 1500; i++) { ... worker[i].salary ...
malloc()
*** test data file *** 20 Xiang 5 Bin 1 ...
#define NAMLEN 50 typedef struct { char name[NAMELEN]; int colour; } Preferences; Preferences *firstMember; scanf("%d", &number); firstMember = (Preferences *)malloc(number * sizeof(Preferences));
structptr->elem
... firstMember->name ...
Produced: 12 Aug 2016