CS计算机代考程序代写 C Lab 2

C Lab 2

C Lab 2
Intermediate Pointers & Basic Structures

How to allocate time for the lab presentation:
Review: 10 mins
Realloc: 5 mins
Structs: 5 mins
Memory: 5 mins
Q&A: 10 mins
Release solution when there are 20 mins left in the second week lab session, so that everyone finishes the assignment

Goals
Review
Pointers
Referencing/Dereferencing
free
malloc
structs

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.

Of course you should elaborate on why the word should is in bold.

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 ‘&’

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?

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?

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

Just bring up why malloc returns a void *, and how we casted it to a char * for the string manipulation last time.

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!

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

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

May instruct the students to open the file, and follow along if you would like. I feel like there is no reason to not include typedef notation. Also, relate the previously bolded should to this buffer. Is this an arraylist of ints? (I know the answer. You should ask the students to make sure they’re awake.)

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)

Be careful about how far you go into this explanation of why. Feel out your section, and don’t go too deep.

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

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

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

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

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

Bolded to denote that it’s a pointer.

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

Mention that the sequence: malloc, free, malloc, free, … , free
is ok.

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

These sections of memory have not been discussed in class. If they have questions on it you can refer them to this link.