1 / 29

July 11, 2012

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.

kirkan
Download Presentation

July 11, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. July 11, 2012 Dynamic memory

  2. Announcements • Homework #4 due today • HW #3 resubmit due tomorrow

  3. How much memory do variables take up? • Depends on operating system, computer, and compiler, but can find out with sizeof() function.

  4. 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

  5. From yesterday's lab • Shervin used sizeof() for array counting. • Assume intfoo[10]. • What is sizeof(foo)?

  6. Class question • How can we really calculate the size of an array using sizeof()?

  7. Cubbyholes • Remember this?

  8. Revisiting our cubbyholes • Cubbyholes have different widths!

  9. 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.

  10. 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

  11. 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

  12. Dynamically allocating memory with malloc() • malloc() is defined inside <stdlib.h> • Usage: malloc(<size>)

  13. 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));

  14. Malloc • Where do we store the result of malloc()?

  15. Malloc • Where do we store the result of malloc()? • Answer: In a pointer! double*foo; foo = (double *)malloc(sizeof(double));

  16. Free • Just like fopen() and fclose() malloc() always needs an accompanying free() • Example: char *c = (char *)malloc(sizeof(char)); free(c);

  17. 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()

  18. 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.

  19. 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);

  20. …But malloc() isn't for arrays! • That's why we use calloc() • How calloc() works: calloc(<size of array>, <size of data type>);

  21. calloc() example int *array_ptr; array_ptr = (int *)calloc(10, sizeof(int)); for(i = 0; i < 10; i++) { printf("%d", array_ptr[i]); }

  22. 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]); }

  23. Quiz questions

  24. What is the value of "size"? intfoo = 0; int size = sizeof(foo);

  25. What is the value of "size"? char foo[100]; int size = sizeof(foo);

  26. Out of our simple data types, which takes up the most amount of memory?

  27. How would we find the length of a double array? Assume that numbers is of type "double *" (an array)

  28. How much memory does "foo" take up? • intfoo[10][10];

  29. For tomorrow: • Learn how to use dynamic memory to return pointers from functions! • Improve our custom_io functions by using dynamic memory allocation!

More Related