Presentation is loading. Please wait.

Presentation is loading. Please wait.

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{

Similar presentations


Presentation on theme: "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{"— 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 struct Telephone{ 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 typedef int points; points p = 5; typedef struct 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 int main() { 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_t nelements, size_t elementSize); // 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!


Download ppt "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{"

Similar presentations


Ads by Google