Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1And in Conclusion\dots

C does not automatically handle memory for you, so it’s up to you, the programmer, to allocate, use, and free memory correctly. In each program, an address space is set aside, separated into 2 dynamically changing regions and 2 ‘static’ regions.

There are a number of functions in C that can be used to dynamically allocate memory on the heap. The following are the ones we use in this class:

Be careful when allocating buffers on the stack and heap! The heap is the biggest source of subtle bugs in C code.

Take CS 162 for more!

2Textbook Reading

K&R 7.8.5, 8.7

3Additional References

4Exercises

Check your knowledge!

4.1Conceptual Review

Solution to Exercise 1 #

False. The four major memory sectors, stack, heap, static/data, and text/code for any given process (application) are defined by the operating system and may differ depending on what kind of memory is needed for it to run.

Solution to Exercise 2 #

(Almost any basic deep recursive scheme, since you’re making many new function calls on top of each other without closing the previous ones, and thus, stack frames.)

Solution to Exercise 3 #

(Perhaps a process that is incredibly complicated but has efficient stack usage and does not dynamically allocate memory.)

Solution to Exercise 4 #

(Maybe if you’re using a lot of dynamic memory that the user attempts to access.)