1 / 42

Advanced Structs Handling in C Programming Explained

Explore struct nesting, pointers, returning, and internal representation in C programming for better memory handling. Discover common issues and solutions.

mrosenthal
Download Presentation

Advanced Structs Handling in C Programming Explained

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 9 Part 2 • Kyle Dewey

  2. Overview • Announcement • More with structs and memory • Assertions • Exam #2 • Course review

  3. Next “Lab” • No pre-lab • The lab will be entirely review • It will be graded

  4. More with Structs

  5. Nesting Structs • We can also nest structs, like so: struct Point { int x; int y; }; struct Circle { struct Point center; int radius; };

  6. struct Address { int streetNumber; char* street; int zip; int state; }; struct Date { int month; int day; int year; }; struct Person { struct Address address; char* name; struct Date birthday; };

  7. Pointers to Structs • Remember, we can also have pointers to other structs in a struct, or even pointers to a struct of the same type struct IntegerList { int integer; struct IntegerList* nextInteger; };

  8. Returning Structs • Structs can be returned just like any other data • The same rules about structs versus pointers to structs apply

  9. Returning Structs • This code is perfectly valid: struct Foo { int x; int y; }; struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; }

  10. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

  11. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

  12. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

  13. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef retval struct Foo x: 1 y: 2

  14. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef retval struct Foo x: 1 y: 2

  15. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: 1 y: 2 retval struct Foo x: 1 y: 2 copy

  16. Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: 1 y: 2

  17. Returning Structs • This code has an issue: struct Foo { int x; int y; }; struct Foo* doSomething( int a ) { struct Foo retval = { a, a + 1 }; return &retval; } int main() { struct Foo* f; f = doSomething( 1 ); return 0; }

  18. Problem struct Foo retval = { a, a + 1 }; Stack Heap retval struct Foo x: 1 y: 2

  19. Problem return &retval; retval Stack Heap retval struct Foo x: 1 y: 2

  20. Problem <<doSomething returns without copy>> retval Stack Heap ????????????????????????????????????????????????

  21. Problem struct Foo* f = doSomething( 1 ); retval Stack Heap ????????????????????????????????????????????????

  22. Returning Structs struct Foo { int x; int y; }; struct Foo* doSomething( int a ) { struct Foo* retval = malloc( sizeof( struct Foo ) ); retval->x = a; retval->y = a + 1; return retval; } int main() { struct Foo* f = doSomething( 1 ); free( f ); return 0; } • This code is ok:

  23. Why it’s Ok struct Foo* retval = malloc( sizeof( struct Foo ) ); Stack Heap retval struct Foo x: undef y: undef

  24. Why it’s Ok retval->x = a; retval->y = a + 1; Stack Heap retval struct Foo x: 1 y: 2

  25. Why it’s Ok return retval; retval Stack Heap struct Foo x: 1 y: 2

  26. Why it’s Ok struct Foo* f = doSomething( 1 ); retval Stack Heap copy struct Foo x: 1 y: 2 f

  27. Why it’s Ok struct Foo* f = doSomething( 1 ); Stack Heap f struct Foo x: 1 y: 2

  28. Why it’s Ok free( f ); Stack Heap f ????????????????????????????????????

  29. Recall • The stack is automatically managed • Allocation: variable / struct / array declaration • Deallocation: leave a block • The heap is manually managed • Allocation: malloc / calloc / realloc • Deallocation: free

  30. Assertions

  31. Assertions • Used to assert that something is true at a given point • If it is true, the program goes on • If it is not true, then the program terminates

  32. Using Assertions #include <assert.h> int main() { assert( 3 * 2 > 7 ); return 0; } Assertion failed: (3 * 2 > 7), function main, file assert.c, line 4.Abort trap: 6

  33. Usefulness • Great for debugging • Can put assumptions into code • Acts as executable documentation void doSomething( int* pointer ) { // assume pointer isn’t NULL assert( pointer != NULL ); printf( “%i\n”, *pointer ); }

  34. Caveats • They are intended as a debugging tool • They can be shut off like so: #define NDEBUG #include <assert.h> int main() { assert( 3 * 2 > 7 ); return 0; }

  35. Question #1 #include <assert.h> int main() { int x = 0; assert( x = 3 ); // what does x equal? return 0; }

  36. Question #1 #include <assert.h> int main() { int x = 0; assert( x = 3 ); // x == 3 return 0; }

  37. Question #2 #define NDEBUG #include <assert.h> int main() { int x = 0; assert( x = 3 ); // what does x equal? return 0; }

  38. Question #2 #define NDEBUG #include <assert.h> int main() { int x = 0; assert( x = 3 ); // x = 0 return 0; }

  39. The Point • No work should be done in an assertion

  40. Exam #2

  41. Statistics • Average: 78 • Min: 52 • Max: 97 • A’s: 8 • B’s: 12 • C’s: 15 • D’s: 4 • F’s: 2

  42. Course Review

More Related