CIS 2500 Lecture Notes - Lecture 5: Null Character, Buffer Overflow, C Dynamic Memory Allocation
74 views3 pages
21 Jan 2017
School
Department
Course
Professor
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+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers