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.

Allocating strings

Allocating strings - C Tutorial

From the course: Secure Coding in C

Start my 1-month free trial

Allocating strings

- [Instructor] Strings can be allocated dynamically or statically with both methods having weaknesses for overflow. This code shows dynamic allocation of a string, where memory is allocated as the program runs. Line 11 sets aside 27 bytes of storage. The string is filled in the for loop at line 19. Line 22 caps the end of the string with a null character. That's actually what makes it a string officially, and then line 25 outputs the string. And there is the string dynamically created in memory. Here's the static version of the same program, where a character array is declared at line six. For loop at line 10 fills the array with characters. Line 13 caps the end of the array. Position 27 or the 26th element, if you're counting from zero, that's a null character. That makes it a string. The string is output at line 16. The output should be the same, but this string was allocated statically. This example shows how things can…

Contents