Hacker News new | past | comments | ask | show | jobs | submit login

There are some dialects of C which provide additional guarantees through verification, but I don't think there is a coding strategy for standard C which minimizes uses of uninitialized memory. One can and should use static and dynamic program analysis, but that's not a coding strategy.

This is why many people say that standard C is unsafe and using if for writing robust software is a losing battle.




The coding strategy for not using uninitialized memory in C is to initialize it before you use it ;)

And I'm fairly cretin that all the major compilers have a check and warning for it in various forms.

If you make a global simple type it is initialized to zero.

If you use calloc it initializes to what ever digit you want (ie) 0.

If you use malloc etc then you set it with memset.

if you use automatic variables you set them on creation, int i = 0;

If you make an automatic aggregate type you set it like so, struct bob = {0};

and so on.

if you want to make sure your pointer is not uninitialized you set them to NULL on creation and set them to NULL on un-assignment or free. And then always test them for NULL.

All of that is very beginner to intermediate level knowledge and anyone who is going to use something like CERT is expected to know all the basics.

There are valid reasons why you can claim that C memory management is unsafe or not as safe as other languages but its not related to initialization.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: