290 likes | 350 Views
July 11, 2012. Dynamic memory. Announcements. Homework #4 due today HW #3 resubmit due tomorrow. How much memory do variables take up?. Depends on operating system, computer, and compiler, but can find out with sizeof () function. On my system, variables take up the following.
E N D
July 11, 2012 Dynamic memory
Announcements • Homework #4 due today • HW #3 resubmit due tomorrow
How much memory do variables take up? • Depends on operating system, computer, and compiler, but can find out with sizeof() function.
On my system, variables take up the following • sizeof(char) = 8 bits / 1 byte • sizeof(int) = 32 bits / 4 bytes • sizeof(double) = 64 bits / 8 bytes
From yesterday's lab • Shervin used sizeof() for array counting. • Assume intfoo[10]. • What is sizeof(foo)?
Class question • How can we really calculate the size of an array using sizeof()?
Cubbyholes • Remember this?
Revisiting our cubbyholes • Cubbyholes have different widths!
Why does this matter? • As we begin to delve into dynamic memory allocation, we need to be keenly aware of the amount of memory that we need.
Dynamic Memory • Often, we don't know how large our arrays should be • What we've done thus far is to vastly over-estimate our array sizes
Drawback of fixed-size arrays • Consider the following array of strings: • char words[1024][1024]; • We've just taken up 1MB! • Most of this won't be used
Dynamically allocating memory with malloc() • malloc() is defined inside <stdlib.h> • Usage: malloc(<size>)
Malloc • malloc returns a "void" pointer. • In this case, void kind of means wildcard. As such, we have to cast the return value into something that we want. • Ex: (int *)malloc(sizeof(int));
Malloc • Where do we store the result of malloc()?
Malloc • Where do we store the result of malloc()? • Answer: In a pointer! double*foo; foo = (double *)malloc(sizeof(double));
Free • Just like fopen() and fclose() malloc() always needs an accompanying free() • Example: char *c = (char *)malloc(sizeof(char)); free(c);
Free • Calling free() returns the memory allocated by malloc() back to someone else that might need it. • VERY IMPORTANT that we give back memory that we ask for. • Class demonstration of what happens when you don't call free()
Free • Once we call free() we lose access to whatever data was stored inside the variable that we freed up. • However, we can use the variable to store new values in the future.
Putting it all together double dynamic_num; printf("%d", dynamic_num); dynamic_num = (double *)malloc(sizeof(double)); printf("%d", dynamic_num); *dynamic_num = 3.25; printf("%lf", *dynamic_num); free(dynamic_num); printf("%d", dynamic_num);
…But malloc() isn't for arrays! • That's why we use calloc() • How calloc() works: calloc(<size of array>, <size of data type>);
calloc() example int *array_ptr; array_ptr = (int *)calloc(10, sizeof(int)); for(i = 0; i < 10; i++) { printf("%d", array_ptr[i]); }
But we're still hard coding array sizes! • Another example, this time completely dynamic: int *array_ptr; intarray_size; array_size = read_int("Enter a number: "); array_ptr = (int *)calloc(array_size, sizeof(int)); for(i = 0; i < array_size; i++) { printf("%d", array_ptr[i]); }
What is the value of "size"? intfoo = 0; int size = sizeof(foo);
What is the value of "size"? char foo[100]; int size = sizeof(foo);
Out of our simple data types, which takes up the most amount of memory?
How would we find the length of a double array? Assume that numbers is of type "double *" (an array)
How much memory does "foo" take up? • intfoo[10][10];
For tomorrow: • Learn how to use dynamic memory to return pointers from functions! • Improve our custom_io functions by using dynamic memory allocation!