120 likes | 204 Views
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
E N D
Structs: • Used to hold associated data together • Used to group together different types of variables under the same name structTelephone{ char * name; int number; }
Structs: Where to define Structs: 1. In header files 2. top of .c files Structs are pass by value!
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? }
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; }
Dereferencing Pointers: • Note that “->” is used to get fields of a pointer to a struct • “->” is equivalent to (*foo).x = 56;
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;
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!
#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; }
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!
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);
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!