For example to request enough bytes for 4 integer

Info icon This preview shows pages 17–23. Sign up to view the full content.

View Full Document Right Arrow Icon
` For example, to request enough bytes for 4 integer values, we could use: int *p; p = malloc ( 4 * sizeof(int) ); /* malloc (4 * 4) is not portable! */ ` If we only needed the number of bytes for one int, we could use: int *p; p = malloc ( sizeof(int) ); ` The memory returned by malloc is uninitialized (contains garbage values), so be sure to initialize it before you use it!
Image of page 17

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

View Full Document Right Arrow Icon
` Returns a pointer to void (i.e., void *), which points to the address of the first byte of the allocated memory space on the heap. ` This function takes two parameters - which may be expressions - which specify the number of elements for which storage is being requested, and the size of each element in bytes (Use sizeof() for portability!). ` So, to request memory space for 4 integer values, we could use: int *p; p = calloc ( 4, sizeof(int) ); /* calloc (4, 4) is not portable! */ ` If we only needed space for one int, we could use: int *p; p = calloc ( 1, sizeof(int) ); ` The memory returned by calloc is initialized to 0, so if you do not plan to initialize the values before using them, use calloc, and not malloc! This means that calloc() will take more CPU time to execute than will malloc(). It may or may not be an issue, but worth thinking about depending upon how much space you are requesting.
Image of page 18
` If the requested memory cannot be allocated, both malloc() and calloc() return the null pointer (defined in stdlib.h), which has a value of 0. ` Therefore, before using the pointer to access any of the allocated memory, you should check to make sure that the pointer returned was not null. For example: int *p; p = calloc (10, sizeof(int) ); if (p != 0) { /*Also (if p != NULL), NULL is #defined in stdlib.h */ . . . . . . . . /* OK to access values in allocated storage */ } else { . . . . /* Some code to handle the allocation failure*/ }
Image of page 19

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

View Full Document Right Arrow Icon
` If your program uses storage which has been allocated dynamically, then you should free it (return it to the operating system) once it is no longer being used. ` The C library function free() is used for this; it returns void, and has a single parameter, which is a pointer to the first byte of the allocated storage to be freed, and this pointer MUST be pointing to the 1 st byte of some dynamically allocated storage! ` free() is also declared in stdlib.h.
Image of page 20
` Here’s an example of how to free dynamically allocated storage: int *p; p = calloc (10, sizeof (int) ); …… free (p); /* releases storage to which p points back to the OS */ p = NULL; ` The pointer which was passed to free should also be set to NULL or 0 after the call to free(), to ensure that you do not attempt to access it inadvertently. To do so can cause a segmentation fault.
Image of page 21

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

View Full Document Right Arrow Icon
The improper use of dynamic memory allocation can frequently be a source of bugs. These can include security bugs or program crashes, most often due to segmentation faults. The most common errors are as follows: ` Not checking for allocation failures Memory allocation is not guaranteed to succeed, and may instead return a null pointer. Using the returned value, without checking if the allocation is successful, invokes undefined behavior. This usually leads to a crash (due to the resulting segmentation fault on the null pointer dereference), but there is no guarantee that a crash will happen so relying on that can also lead to problems.
Image of page 22
Image of page 23
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