C Programming: Dynamic Memory Allocation and Pointers to Structs Exam Review, Exams of Software Engineering

Important details about the upcoming final exam for the csse 120 course at rose hulman institute of technology. It covers topics such as dynamic memory allocation, pointers to structs, and the use of malloc and free in c. The exam will consist of both a paper and a computer part, with the paper part focusing on c materials and comparing and contrasting c and python. Students are allowed to bring two sheets of paper during the exam. The computer part will be worth less than 50% of the total. The document also includes examples of how to use malloc and free to dynamically allocate memory for variables and structures in c.

Typology: Exams

Pre 2010

Uploaded on 08/19/2009

koofers-user-5iq-2
koofers-user-5iq-2 🇺🇸

1

(1)

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DYNAMIC MEMORY
ALLOCATION,
POINTERS TO STRUCTS
CSSE 120Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download C Programming: Dynamic Memory Allocation and Pointers to Structs Exam Review 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: Wednesday, Feb 25, 2009

 Time: 8:00AM to 12:00 PM

 Venue: O267, O269, O231, O

 Chapters: Zelle chapters 1 to 12, Assigned C readings from Schildt plus Web resources linked from ANGEL Resources page

 You may bring two sheets of paper this time.

Q1,

Memory Requirements

 Any variable requires a certain amount of memory.

 Primitives, such an int , double , and char ,

typically may require between 1 and 8 bytes, depending on the desired precision, architecture, and Operating System’s support.

 Complex variables such as structs , arrays , and strings typically require as many bytes as their components.

How large is this?

sizeof operator gives the number bytes needed to store a value

 sizeof(char)

 sizeof(int)

 sizeof(double)

 sizeof(char *)

 sizeof(student)

 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;

Q

Memory Allocation

 In many programming languages, memory gets dynamically allocated as the need arises.

 Example: Lists in Python grow and shrink as we add or remove items from them.

 In Python, memory gets allocated as the need arises.

 Memory gets freed up when it is no longer needed.

Memory Allocation

 In C, we have the ability to manually allocate memory.

 We typically do this when we know ahead of time the storage needs of a complex data-structure.

 We have seen this last time, when we did this:

char string[10];

 We allocated ten bytes to store a string.

 In some of the examples, we used all of the allocated bytes, in some, we did not.

Memory allocation in C

 We use the malloc command to allocate memory.

 The syntax is:

malloc();

 The command returns a pointer to a memory location.

 We typically want to store that pointer.

Memory allocation in C - Example

 Suppose we want to reserve space for 10 doubles.

 We would do: *double result; result = ( **double *** ) malloc(count * sizeof(double));

 Memory is returned to as typeless.

 We give it a type by typecasting.

Sample Project for Today

 Check out MallocSample from your SVN Repository

 Verify that it runs, get help if it doesn't

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 recycled when function is done!

 If we want samples to persist beyond the function’s lifetime , 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); (^) Don't forget to free the memory

that was previously "malloc-ed". Q

Recap: sizeof, malloc and free

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

 *malloc(): returns a pointer to space for an object of size amount , 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 point to memory that was previously dynamically allocated.

Descriptions from K&R, p. 252

Dynamically Allocating Structs

 Can use malloc to dynamically allocate struct s

 We'll use this to create an Array data type soon that's "smarter" than the basic C version

 Will need to use pointers to structs

 student *zeb;

 Accessing elements of structs is different with pointers…

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 aaron->gpa is shorthand for (*aaron).gpa Q9,