From the course: Secure Coding in C
Unlock the full course today
Join today to access over 23,500 courses taught by industry experts.
Allocating strings - C Tutorial
From the course: Secure Coding in C
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â¦
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Allocating strings2m 40s
-
(Locked)
Avoiding bad string assignment1m 28s
-
(Locked)
Working with string literals2m
-
(Locked)
Minding string functions3m 47s
-
(Locked)
Storing passwords and codes1m 15s
-
(Locked)
Clearing data after use1m 13s
-
(Locked)
Challenge: The secret code40s
-
(Locked)
Solution: The secret code1m 43s
-
(Locked)
-
-