Pointers and Basic Structures in C Lab 2

c lab 2 n.w
1 / 18
Embed
Share

Explore the concept of pointers, referencing, dereferencing, memory allocation, and structs in C Lab 2 intermediate level. Learn how to work with pointers, pass by value and reference, utilize void pointers, use malloc function, and create personalized data structures with structs.

  • Pointers
  • Structs
  • Memory Allocation
  • C Programming
  • Data Structures

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. C Lab 2 Intermediate Pointers & Basic Structures

  2. Goals Review Pointers Referencing/Dereferencing free malloc structs

  3. Review: What are Pointers? A pointer is an address on either the stack or heap. EX: double * should address a double in memory. For the pointer to contain data, some other function must create the data it will point to. This is typically a call to malloc.

  4. Getting Pointer/Reference To get pointer to something, use & & allows to pass items by reference To dereference or get item pointed to use * * is the opposite of &

  5. Pass by Value Pass by value: void plus(int num){ num++; } void main(){ int num = 3; plus(num); printf( %d\n , num); } What does main print?

  6. Pass by Reference Pass by reference: void plus(int *num){ (*num)++; } void main(){ int num = 3; plus(&num); printf( %d\n , num); } What does main print now?

  7. Void* void* may point to arbitrary types (i.e. int*, char*, etc.) Can be cast to appropriate types

  8. Malloc Malloc returns a void* pointer The memory it allocated doesn t inherently have any type Malloc returns null if it fails. Always check the return value!

  9. Structs Personalized types, somewhat like classes May contain items of choice Often the basis of (data) structures

  10. Structs typedef struct { int *buffer; int buffersize; int length; } arraylist;

  11. Editing Struct Fields You may declare structs on the stack You may access/edit fields of struct using . Think about why this works (Hint: pointers)

  12. Editing Struct Fields arraylist a; a.buffer = NULL; a.buffer_size = 0; a.length = 0;

  13. Editing Struct Fields You may declare structs on the heap Now you access/edit fields using -> This syntax is more helpful visually

  14. Editing Struct Fields arraylist *a = (arraylist *)malloc(sizeof(arraylist)); a->buffer = NULL; a->buffer_size = 0; a->length = 0;

  15. Default values and null Null is a pointer value going nowhere If a pointer shouldn t be pointing at anything set it to null In C, values aren t initialized to any particular value -They take whatever happened to be in memory

  16. Memory Management You must free what you malloc (heap) Stack manages itself arraylist *a = (arraylist *)malloc(sizeof(arraylist)); free(a); //yaaaaaaaaay

  17. Memory Management Do not free what you did not malloc!!! Do not free address consecutively!!! int *num = malloc(4) free(num); //yaaaayyy free(num); //staaahp int num = 3; free(&num); // :,O

  18. Memory Takeaways Only free what has been malloc d Only free malloc d memory once For more on stack vs. heap: http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html#sec-4

Related


More Related Content