july 11 2012 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
July 11, 2012 PowerPoint Presentation
Download Presentation
July 11, 2012

Loading in 2 Seconds...

play fullscreen
1 / 29

July 11, 2012 - PowerPoint PPT Presentation


  • 43 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'July 11, 2012' - kirkan


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
july 11 2012

July 11, 2012

Dynamic memory

announcements
Announcements
  • Homework #4 due today
  • HW #3 resubmit due tomorrow
how much memory do variables take up
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
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
From yesterday's lab
  • Shervin used sizeof() for array counting.
  • Assume intfoo[10].
  • What is sizeof(foo)?
class question
Class question
  • How can we really calculate the size of an array using sizeof()?
cubbyholes
Cubbyholes
  • Remember this?
revisiting our cubbyholes
Revisiting our cubbyholes
  • Cubbyholes have different widths!
why does this matter
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
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
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
Dynamically allocating memory with malloc()
  • malloc() is defined inside <stdlib.h>
  • Usage:

malloc(<size>)

malloc
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));

malloc1
Malloc
  • Where do we store the result of malloc()?
malloc2
Malloc
  • Where do we store the result of malloc()?
  • Answer: In a pointer!

double*foo;

foo = (double *)malloc(sizeof(double));

slide16
Free
  • Just like fopen() and fclose() malloc() always needs an accompanying free()
  • Example:

char *c = (char *)malloc(sizeof(char));

free(c);

slide17
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()
slide18
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
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
…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
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
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
What is the value of "size"?

intfoo = 0;

int size = sizeof(foo);

what is the value of size1
What is the value of "size"?

char foo[100];

int size = sizeof(foo);

how would we find the length of a double array
How would we find the length of a double array?

Assume that numbers is of type "double *" (an array)

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