CIS 2500 Lecture Notes - Lecture 5: Null Character, Buffer Overflow, C Dynamic Memory Allocation

74 views3 pages

Document Summary

Creating an array of strings: create an array of pointers to characters. Put the strings (arrays of characters) at the end of the pointers (this is a 2d array) Code to allocate an array of strings char **sptr; int num = 3; int i; /* declare a pointer-to-pointer variable sptr */ sptr = malloc (sizeof(char *) * num); /* allocate the memory for the char pointers */ /* for each element in the array, allocate space for the character string and point to them from the array of pointers. Copy the string name into the allocated space. for (i = 0; i < num; ++i) { sptr[i] = malloc (sizeof(char) * 5); strcpy(sptr[i], name); /* we"ve created a 3 (strings) by 4 (chars) array */ strcpy is bad though! We don"t know if we"ll have a buffer overflow! Buffer overflow is exploitable with malicious code look into it.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions