From the course: Secure Coding in C

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Initializing pointers and buffers

Initializing pointers and buffers - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Initializing pointers and buffers

- [Instructor] The compiler effectively warns against using uninitialized pointers, but what about pointers that reference uninitialized buffers? So, in this code, you have pointer p. It's initialized at line eight, and it references a buffer of 10 integer-sized values. Line 11 loops 10 times to output those values. The values aren't initialized, so the output you see is whatever garbage exists in memory. I'm going to build the code, and you can see this builds without any errors, at least on this compiler. Let's see what happens when we run. And here you see garbage values. Now, they may coincidentally be zero, but the result is uninitialized memory. It's allocated but uninitialized. Here is a much-improved version of the code. First, the pointer returned at line eight is cast as an integer. The malloc function returns a void or untyped memory location. This cast ensures that the buffer's data type matches the…

Contents