Memory leaks failure to deallocate memory using free

Info icon This preview shows pages 22–29. Sign up to view the full content.

crash will happen so relying on that can also lead to problems. ` Memory leaks Failure to deallocate memory using free() leads to buildup of non-reusable memory, which is no longer used by the program. This wastes memory resources and can lead to allocation failures when these resources are exhausted. ` Logical errors All allocations must follow the same pattern: allocation using malloc(), usage to store data, deallocation using free. Failures to adhere to this pattern, such as memory usage after a call to free (dangling pointer) or before a call to malloc() (wild pointer), calling free twice ("double free"), etc., usually causes a segmentation fault and results in a crash of the program. These errors can be transient and hard to debug – for example, freed memory is usually not immediately reclaimed by the OS, and thus dangling pointers may persist for a while and appear to work.
Image of page 22

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

` Suppose we request enough space from malloc() or calloc() to store more than one variable of some type, and assign some value to the first element in the allocated memory: int *p; p = malloc (10 * sizeof(int)); *p = 100; /* assigns 100 to the first sizeof (int) bytes */ ` Since malloc() and calloc() return a pointer to the first byte of the allocated memory, how do we access the rest of the space beyond the first element?
Image of page 23
` We can use pointer arithmetic to access the elements beyond the first element (or even the first element). ` If p points to the first integer in our example, p + 1 points to the second integer, p + 2 points to the third, and p + n points to the (n + 1) th . ` When the code is compiled, the compiler can generate instructions to access the appropriate bytes, because we assigned the pointer to the allocated storage to int *p, so the compiler knows the elements are integers, and it also knows sizeof (int) for the system.
Image of page 24

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

` IMPORTANT: When we do arithmetic with pointers, the value added or subtracted is scaled by the compiler using sizeof(type). In other words, if we add or subtract n from the address in a pointer variable ptr , the compiler will generate an instruction which adds or subtracts n * sizeof(*ptr) ` When you use pointer arithmetic, do not scale the integer you add or subtract by the size of the type, or you will get double scaling (this will always cause access errors).
Image of page 25
` If the address of the first integer is 0x1000 (hex 1000), then the address of the second is: (0x1000 + 1 * sizeof(int)) So, if sizeof(int) is 4 bytes on the system, then the address of the second integer is 0x1004. The address of any other element in the allocated storage can be calculated in the same way.
Image of page 26

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

` How can we use pointer arithmetic and the dereference operator to access elements of our dynamically allocated storage? int *p; p = malloc (10 * sizeof(int)); *p = 100; /* assigns 100 to the first (sizeof) int bytes */ ` To assign int values to the next two elements: *(p + 1) = 200; /*Remember, the compiler scales the value added*/ *(p + 2) = 300; ` More generally, for any statically or dynamically allocated array: array[i] accesses the same element as *(array + i)
Image of page 27
` We can treat the allocated memory space as an array, because it is dynamically allocated storage consisting of a number of
Image of page 28

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

Image of page 29
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern