COP3502_4_DMA - Dynamic Memory Allocation in C Computer...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Dynamic Memory Allocation in C COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dynamic Memory Allocation in C page 2 Dynamically Allocated Mem. in C Throughout the C course (COP 3223) All variable declarations were statically allocated memory The word “static” means “not changing” The word “dynamic” means “changeable” roughly speaking So we essentially work with two types of memory: Static Dynamic
Background image of page 2
Dynamic Memory Allocation in C page 3 Dynamically Allocated Mem. in C Static Memory in C (review) Memory requirements are known at compile time After a program compiles, we can perfectly predict how much memory will be needed for statically allocated variables Although the program may receive different input on different executions of the code This does NOT affect how much memory is allocated One serious consequence of this: A statically allocated variable can only have its memory reserved while the function, within which it was declared, is running Ex: if you declare an “int x” within function A, once function A has completed, the memory for x is not reserved.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dynamic Memory Allocation in C page 4 Dynamically Allocated Mem. in C Dynamic Memory in C Memory requirements are NOT known (for sure) at compile time Perhaps different amounts of memory are allocated on different executions of the program That is, if the input affects the memory allocation Benefit: memory allocated within a function can be made available outside the function This memory must be allocated within the function And it must be allocated dynamically Caveat: Dynamically allocated memory is NOT “freed” automatically It is the programmers job to free memory This is done with the free function
Background image of page 4
Dynamic Memory Allocation in C page 5 Dynamically Allocated Mem. in C Conceptually, memory is divided into: 1) program memory which is used for main and all called functions, and 2) data memory which is used for global data, constants, local definitions and dynamic memory. Obviously, main must be in memory at all times. Each called function must only be in memory while it or any of its called functions are active. Since multiple copies of a function may be active at one time (recursion) the multiple copies of the variables are maintained on the stack . The heap memory is unused memory allocated to the program and available to be assigned during execution. The next page illustrates the conceptual view of memory.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dynamic Memory Allocation in C page 6 Conceptual View of Memory s Program Memory main called and standard functions Data Memory global program heap system stack Memory
Background image of page 6
Dynamic Memory Allocation in C page 7 Dynamically Allocated Mem. in C Four memory management functions are used with dynamic memory in the C language. malloc
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

COP3502_4_DMA - Dynamic Memory Allocation in C Computer...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online