Class Notes (837,609)
Canada (510,370)
CSC209H1 (41)
Karen Reid (20)
Lecture

feb06c.docx

2 Pages
69 Views
Unlock Document

Department
Computer Science
Course
CSC209H1
Professor
Karen Reid
Semester
Winter

Description
 ex.c 28. int main(){ 1. #include 29. User u; 30. User u1; 2. 31. User *u2; 3. struct user { /* defines a struct and give 32. char *name = "Karen"; /* string literal */ 4. the type a different name */ 5. char *name; 33. double balance = 5.25; 6. double b; 34. 7. struct user *next; 35. /* dereference the return value of create_user 8. }; 36. b/c can copy struct by simple assignment */ 9. 37. u = *(create_user(name, balance)); 10. typedef struct user User; /* give a new name 38. name = "Reid"; 39. u1 = *(create_user(name, balance)); 11. to the type */ 40. u2 = create_user(“Test”, balance); 12. 41. printf("User: %s b: %f\n", u.name, u.b); 13. 14. /* returns pointer to struct */ 42. printf("User: %s b: %f\n", u1.name, u.b); 15. User *create_user(char *n, double b) { 43. printf("User: %s b: %f\n", u2->name, u2->b); 16. /* User new_user; this won’t last b/c after 44. 17. function return, the memory will be 45. return 0; 18. reallocated, and modified new_user will 46. } 19. be gone */ o output 20. User *new_user = malloc(sizeof(User)); User: Karen b: 5.250000 21. new_user->name = n; 22. /* (*new_user).name = n; */ User: Reid b: 5.250000 23. new_user->balance = b; User: Test b: 5.250000 24. /* (*new_user).balance = b; */ 25. o size of struct user is 16: 26. return new_user;  4 from pointer-to-char name 27. }  4 from pointer-to-struct user next  8 from double balance  Double pointers o Most frequently used is for array of strings Memory Allocation  Static Allocation o Can malloc fail?  YES o Static allocation happens at compile time based on variable definition  A function that returns a block of memory might fail to do so, in o The compiler allocates memory for symbols at compile time, or when which case it returns a NULL pointer load a stack into a frame  infinitely alloc memory/run out of memory to allocate o ex. in the main function or in the program, when:  heap is finite  malloc cannot allocate infinitely  declare an int, compiler reserves 4 bytes  amount of memory potientially allocate depends on the system  declare an array of 4 ints, compiler reserves 16 bytes  ex. 32bit machine can only allocate 0 to 2^32-1 memory  declare a pointer-to- slots, may not use it all up anything, compiler reserves  NULL is a pre-processor variable defined in iolib.h (included 4 bytes from stdio.h) and other places int x = 2;  It is usually defined to be 0 b/c no program allocates int a[4]; anything at address 0x0 int *b;  check if it fails or not  check return of malloc int main() { int *a = malloc(sizeof(int)); } if (a == NULL) { /* allocation failed */ SYMBOL TABLE: } main 0x804837c .text f9 /* continue */ x 0x8049588 .data 04 b 0x8049688 .bss 04 o De-allocating memory a 0x804968c .bss 10 int *a = malloc(10*sizeof(int)); o Variables declared outside any functions are global variables int b[10];  Generally bad
More Less

Related notes for CSC209H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit