Skip to content

Pointers

Maxwell Talley edited this page Sep 13, 2017 · 1 revision
  • Parameter passing in C

    • It's faster to pass structs by address. A struct passed by value is copied, so changes to its fields "disappear" when the function returns. On the other hand, when passed by address, the changes persist. Arguably, it's safer to pass by value if you make no changes, but you can pass a constant pointer for that and still get the speed advantage.
  • Dynamic allocation

    • Dynamic storage is allocated with malloc(3). Any time you allocate something, you must return it to the system with free(3). In order to use these functions, you need to include stdlib.h. It is possible for malloc() to fail. When it does, it returns NULL. You should check for this!
  • void *malloc(size_t size); malloc() is the function that we use to request heap memory from the operating system.

When should we use malloc()?

  • When we don't statically know how much storage we need. Ask yourself: Can I allocate this statically? If you know how big it has to be, the answer is yes, and so you shouldn't use dynamic allocation. Exception: You know a maximum possible size, but that maximum is much larger than the expected size. In that case, dynamic allocation is probably the better choice.
  • malloc() can fail
    • returns NULL; you need to check for it. If you're an application developer, you're best option is probably to terminate (gracefully). If you're a library developer, you return an error and allow your user to decide what to do about it.
  • malloc() has some unnecessary but useful siblings
    • calloc()

      • allocates arrays. Initializaes the returned storage to zero If we use calloc() to allocate our queue_nodes in queue.c, then we don't need to set next to NULL; calloc() does it for us. Useful for debugging, since you can clearly see what data you have modified, but the overhead is not necessarily a good thing, especially in performance critical code.
    • realloc()

      • reallocate memory takes a pointer to storage previously returned by malloc() Reallocates it to the new size. Pointer returned may point to a new location Output is truncated if new size is smaller.

      Dynamically resizable array example:

      size_t array_size;
      int size
      int *array;
      size = 0;
      array_size = 16;
      array = malloc(array_size * sizeof (*array));
      /* fill up the array... */
      if (size == array_size) {
        /* Remember to test it! */
        array = realloc(array, array_size *= 2);
      }
  • It is an error--and one that corrupts program state!--to call free() on an address which has not been malloc()ed. Double-free bug causes the program to fail in weird ways very difficult to debug
Clone this wiki locally