1 / 12

Discussion: Week 3/26

Discussion: Week 3/26. Structs :. Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{ char * name; int number; }. Structs :. Where to define Structs : 1. I n header files 2. top of .c files

nibal
Download Presentation

Discussion: Week 3/26

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. Discussion: Week 3/26

  2. Structs: • Used to hold associated data together • Used to group together different types of variables under the same name structTelephone{ char * name; int number; }

  3. Structs: Where to define Structs: 1. In header files 2. top of .c files Structs are pass by value!

  4. Example of use of Structs: void func(struct Foo foo){ foo.x = 56; foo.y= 55; } void main(){ struct Foo foo; foo.x = 54; foo.y= 9; func(foo); // what are values of foo.x, foo.y? }

  5. Passing Structs through Parameters: To have changes occur in a struct, instead of passing a struct, pass in a pointer to the struct: struct Foo f; f.x = 54; f.y= 9; func(&f); void func(struct Foo * foo){ foo-> x = 56; foo -> y = 55; }

  6. Dereferencing Pointers: • Note that “->” is used to get fields of a pointer to a struct • “->” is equivalent to (*foo).x = 56;

  7. Typedef: • Allows you to create your own datatype • Frequently used with structs typedefint points; points p = 5; typedefstruct Telephone{ char * name; int number; }TELEPHONE; TELEPHONE t; t.name = “John Doe”; t.number = 15;

  8. Dynamic Memory Allocation: • Malloc is used to allocate a specific amount of memory during the execution of a program • If the request is granted, the operating system will reserve that amount of memory • Use the sizeof() function to determine the amount of memory to allocate • Remember, you will need to return that block of memory by calling free!

  9. #include<stdio.h> intmain() { int*ptr_one; ptr_one= (int *)malloc(sizeof(int)); if (ptr_one == 0) { printf("ERROR: Out of memory\n"); return 1; } *ptr_one = 25; printf("%d\n", *ptr_one); free(ptr_one); return 0; }

  10. Malloc Further Explained: • The malloc statement will ask for an amount of memory with the size of an integer (32 bits or 4 bytes). • If there is not enough memory available, the malloc function will return a NULL. If the request is granted a block of memory is allocated (reserved). • The address of the reserved block will be placed into the pointer variable. • The if statement then checks for the return value of NULL. If the return value equals NULL, then a message will be printed and the programs stops. • Remember to cast the return value of malloc to the right type!

  11. Function Prototypes: • Function prototype for Dynamic Allocation: //size is the number of bytes to allocate void *malloc(size_t size); // calloc initializes n elements each with elementSize void *calloc(size_tnelements, size_telementSize); // reallocates the pointer at the location hold a new size void *realloc(void *pointer, size_t size);

  12. Notes about functions: • Malloc does not initialize memory after it allocates it, while calloc initializes each memory location to 0 • For every piece of memory that you allocate, make sure you free, or you will eventually run out of memory!

More Related