Docsity

Dynamic Memory Allocation, Pointers to Structures | CSSE 120, Exams of Software Engineering

Partial preview of the text

Download Dynamic Memory Allocation, Pointers to Structures | CSSE 120 and more Exams Software Engineering in PDF only on Docsity!

DYNAMIC MEMORY

ALLOCATION,

POINTERS TO STRUCTS

CSSE 120—Rose Hulman Institute of Technology

Final Exam Facts

 Date: Monday, February 18, 2008

 Time: 6:00 to 10:00 PM

 Venue: Section 1 (Curt) O

Section 2 (Matt) O Section 3 (Delvin) A-K in O201, L-Z in O

 Chapters: Zelle chapters 1 to 12 & C material (Angel  Course Resources  C Programming)

 Organization: A paper part and a computer part (in C only) , just as on the first 2 exams. Same resources allowed.

How large is this?

 sizeof operator: gives the number bytes needed to store a value

 sizeof(char)

 sizeof(int)

 sizeof(double)

 sizeof(student)

 sizeof(firstName)

 sizeof(jose)

 printf("size of char is %d bytes.\n", sizeof(char));

*char firstName; int terms; double scores; student jose;

*typedef struct { char name; int year; double gpa; } student;

Returning Arrays from Functions

 In maf-main.c , remove the exit() call near the beginning.

 Run the program:

 What happens?  Why?

 Original version of getSamples() just creates local storage that is reused when function is done!

 If we want samples to persist, we need to allocate memory using "malloc".  Also need to #include <stdlib.h>

Using Dynamically Allocated Array

**double sampleA; double sampleB; int sampleCount = 5;

sampleA = getSamples(sampleCount); sampleB = getSamples(sampleCount);

for (i=0; i<sampleCount; i++) { printf%0.1lf\n", sampleA[i] + sampleB[i]); }

free(sampleA); free(sampleB);

Recap: sizeof, malloc and free

 sizeof operator: gives the number bytes needed to store a value

 void *malloc(size_t size): returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized.

 void free(void *p): deallocates the space pointed to by p; does nothing if p is NULL. p must be a space previously allocated.

Descriptions from K&R, p. 252

Cousins of malloc

 void *calloc(size_t n, size_t el_size): returns a pointer to contiguous space in memory allocated for an array of n elements, each element requiring el_size bytes. Returns NULL if the request cannot be satisfied. The space is initialized with all bits set to zero.  Example: samples = calloc(count, sizeof(double));

 void *realloc(void *ptr, size_t size): changes the size of the block pointed to by ptr to size bytes. The content of the space will be unchanged up to the lesser of the old and new size. Any new space is not initialized.  Example: str = realloc(str, strlen(str) + 1);

Break—90's Flowchart, xkcd.com

Freestyle rapping is basically applied Markov chains.

Pointers to Structs

 Direct reference student debby = {"Deb", 2011, 2.9}; debby.gpa = 3.2; printf("%s, Class of %d\n", debby.name, debby.year);

 Use dot when you have the struct directly

 Pointer reference **student aaron; aaron = (student ) malloc(sizeof(student)); aaron->name = "Aaron"; aaron->year = 2009; aaron->gpa = 3.1; printf("%s, Class of %d\n", aaron->name,aaron->year);

 Use "arrow" when you have a pointer to it

Stop, Practice Time

 Problem:  One nice feature of lists in Python is that they "know" their own length  Suppose we want that in C

 Solution:  Make our own Array type and helper functions!

 Homework:  Check out SmarterArrays from your SVN repository  See homework description linked from ANGEL