简体   繁体   中英

Using calloc or malloc for Game of Life (in C)

I am using a text file where the user inputs parameters for Game of Life. In the first line I will ask the user to write two values, with the first value being the number of grid rows and the second value being the number of grid columns. The maximum grid can be 30 by 30. The second line will contain the initial occupied regions. This can vary from 1 to 900 (all occupied). For this reason I was planning to use malloc or calloc instead of a fixed element array. Which from these two is best for this scenario?

malloc() followed by assignment of 0 to all bytes allocated is exactly like calloc() , except perhaps for (minor) performance.

In other words... doing calloc() and then using data in a text file to assign values to the allocated memory writes twice to that memory.

Use malloc() unless you know the contents of the bytes will be 0 .

Don't forget to use free() when you're done with the memory, whether you obtained it with malloc() or calloc() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM