5
CSC 253 Lecture 9

CSC 253

Embed Size (px)

DESCRIPTION

CSC 253. Lecture 9. malloc and calloc. Let’s take Jason’s ptr4.c and change it to allocate regions larger than 1 word to incorporate a printing function like we wrote last week to allocate something other than int s. Let’s print the start address and ending address of each array. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 253

CSC 253CSC 253

Lecture 9Lecture 9

Page 2: CSC 253

malloc and callocmalloc and calloc

Let’s take Jason’s ptr4.c and change it to allocate regions larger than 1 word to incorporate a printing function like

we wrote last week to allocate something other than ints.

Let’s print the start address and ending address of each array.

Let’s take Jason’s ptr4.c and change it to allocate regions larger than 1 word to incorporate a printing function like

we wrote last week to allocate something other than ints.

Let’s print the start address and ending address of each array.

Page 3: CSC 253

Why malloc is dangerous …

Why malloc is dangerous …

If our array gets out of bounds, we can overwrite an element of another array.

How can we calculate the subscript of one array that will smash the zeroth element of the next array?

If our array gets out of bounds, we can overwrite an element of another array.

How can we calculate the subscript of one array that will smash the zeroth element of the next array?

Page 4: CSC 253

Binary file I/OBinary file I/O

Let’s try the program for writing a file, ptr6.c.

Let’s try the program for reading a file, ptr5.c.

Let’s try to put the output in a more general format, like a memory dump.

Let’s try the program for writing a file, ptr6.c.

Let’s try the program for reading a file, ptr5.c.

Let’s try to put the output in a more general format, like a memory dump.

Page 5: CSC 253

File manipulationFile manipulation

Let’s write a program that determines the length of a binary file.

Let’s change the program to set the position to somewhere else in the file.

Let’s change the program to set the position to an arbitrary place in the file, and then report how far it is from the end.

Let’s write a program that determines the length of a binary file.

Let’s change the program to set the position to somewhere else in the file.

Let’s change the program to set the position to an arbitrary place in the file, and then report how far it is from the end.