When to Malloc

When to Malloc

When should we use malloc ()?

This statement used malloc to set aside memory for an array of 10 integers. As sizes can change between computers, it’s important to use the sizeof() function to calculate the size on the current computer. free( arrayPtr ); This statement will deallocate the memory previously allocated.

When should I use free and malloc?

To allocate a memory block you use malloc() To reallocate a memory block with specific size you use realloc() To de-allocate previously allocated memory you use free()

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.