CS 24000 Lecture Notes - Lecture 11: C Dynamic Memory Allocation, In C

17 views3 pages

Document Summary

Common errors with pointers: using a pointer without initializing crashes the program with segv int * p; // p value is null (0) or unknown. *p = 10; // program writes to invalid. Initialize pointer: int x; int *p; p = &x; *p= 10; another solution: int *p; p = (int*)malloc(sizeof(int)); Common errors with pointers: not allocating enough memory int *array; int n=20; array=(int *)malloc(n * sizeof(int) ); // 20 * 4 bytes = 80 bytes allocated array[5]=20; C blindly tries to assign 7 to the. // assume a is in location 100 int *p; p = &a[0]; // assume p is 100 p = p + 1; // since *p is of type int the. // p now is 104 and points to a[1]. p = p + 2; Pointers and arrays int a[10]; int a[10]; int *p; p = &a[0]; // p == 100 p = p + 1; // since p is of type int* the.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents