Concept
What other memory allocation functions and pointer cast options does C provide?
ComputerScienceOne / Dynamic Memory
"There are other functions that can be used to allocate memory in C. For example, calloc() is similar to malloc() but initializes the contents to zero (null bytes). realloc() can be used to resize an existing memory space (though it may fail if it cannot be expanded).\n\nPerforming an explicit pointer cast is actually not necessary in C as the type system will do an implicit cast for us. Whether explicit or implicit types casts are “better” can evoke debates akin to nerd holy wars. Though there are advantages and disadvantages to both [15], we perform explicit pointer casts in this book as clarity and intent is more important than brevity. Even more important is that explicit pointer casts are necessary in C++ so doing so in our C code makes our code more portable."
Related Ideas
- How does malloc() allocate memory for a dynamic array?ComputerScienceOne · Dynamic Memory
- How do you deallocate dynamically allocated arrays?ComputerScienceOne · Using Arrays with Functions
- How can pointer manipulation create a contiguous two-dimensional array?ComputerScienceOne · Contiguous 2-D Arrays
- Why use a contiguous memory block for a two-dimensional array?ComputerScienceOne · Contiguous 2-D Arrays
- How do you dynamically allocate and initialize a structure?ComputerScienceOne · Selection Operators
- What is a function pointer and how is it declared?ComputerScienceOne · Function Pointers
- How can structures be dynamically allocated?ComputerScienceOne · Declaration & Initialization
- How do you create and use an array of structures in C?ComputerScienceOne · Arrays of Structures