Skip to main content
added 63 characters in body
Source Link
Davislor
  • 8.4k
  • 17
  • 37

This is very easy to do with the initial allocation: just call calloc instead of malloc. For memory that doesn’t come from the heap, there’s memset.

This is very easy to do with the initial allocation: just call calloc instead of malloc.

This is very easy to do with the initial allocation: just call calloc instead of malloc. For memory that doesn’t come from the heap, there’s memset.

added 970 characters in body
Source Link
Davislor
  • 8.4k
  • 17
  • 37

Don’t Report Out-of-Memory Inin-Band

So, a memory allocation failing should be a fatal error, and you should write a handler that aborts the process with a human-readable error message if it happens, and preserves the call stack for debugging. Maybe you really, truly are writing an app that needs to recover gracefully from exhausting all available memory. C might In that case, you know what kind of data the recovery needs, and it’s probably not beto pass the best language for that project; implementingerror code back up the equivalent of try/catch in C gets very complicatedstack.

Don’t Report Out-of-Memory In-Band

So, a memory allocation failing should be a fatal error, and you should write a handler that aborts the process with a human-readable error message if it happens, and preserves the call stack for debugging. Maybe you really, truly are writing an app that needs to recover gracefully from exhausting all available memory. C might not be the best language for that project; implementing the equivalent of try/catch in C gets very complicated.

Don’t Report Out-of-Memory in-Band

So, a memory allocation failing should be a fatal error, and you should write a handler that aborts the process with a human-readable error message if it happens, and preserves the call stack for debugging. Maybe you really, truly are writing an app that needs to recover gracefully from exhausting all available memory. In that case, you know what kind of data the recovery needs, and it’s probably not to pass the error code back up the stack.

added 970 characters in body
Source Link
Davislor
  • 8.4k
  • 17
  • 37

Array Offsets are Never, Ever int

Right now, you have

    for (int i = 0; i < list->capacity; ++i) {
        list->array[i] = 0;
    }

On most modern systems, int is a 32-bit signed value, and size_t is an unsigned value wider than that. So, your int index could—realistically—overflow. This is undefined behavior, which most compilers take as permission to break your code and insert serious bugs. A classic example is that 3U < -1.

Use size_t consistently for indices and offsets, or if you really, truly want signed values that can handle negative indices, ptrdiff_t.

Furthermore, if this code compiled without warning you that comparing an int to a size_t is a serious red flag, you need to turn on more compiler warnings. On GCC, Clang or ICX, I typically use a -std= option plus -Wall -Wextra -Wpedantic -Wconversion -Wdeprecated, where -Wconversion is what ought to enable the warning about this.

Array Offsets are Never, Ever int

Right now, you have

    for (int i = 0; i < list->capacity; ++i) {
        list->array[i] = 0;
    }

On most modern systems, int is a 32-bit signed value, and size_t is an unsigned value wider than that. So, your int index could—realistically—overflow. This is undefined behavior, which most compilers take as permission to break your code and insert serious bugs. A classic example is that 3U < -1.

Use size_t consistently for indices and offsets, or if you really, truly want signed values that can handle negative indices, ptrdiff_t.

Furthermore, if this code compiled without warning you that comparing an int to a size_t is a serious red flag, you need to turn on more compiler warnings. On GCC, Clang or ICX, I typically use a -std= option plus -Wall -Wextra -Wpedantic -Wconversion -Wdeprecated, where -Wconversion is what ought to enable the warning about this.

added 699 characters in body
Source Link
Davislor
  • 8.4k
  • 17
  • 37
Loading
Source Link
Davislor
  • 8.4k
  • 17
  • 37
Loading