













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 21
This page cannot be seen from the preview
Don't miss anything!














CSSE 120—Rose Hulman Institute of Technology
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,
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.
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
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.
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.
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.
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.
Check out MallocSample from your SVN Repository
Verify that it runs, get help if it doesn't
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>
**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
sizeof operator: gives the number of bytes needed to store a value
*malloc(
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
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…
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,