COMP 206 Lecture Notes - Lecture 17: C Dynamic Memory Allocation, Checksum, Makefile

61 views10 pages
COMP 206 LECTURE NOTES
Lectures (9-17)
Dynamic Memory Allocation
void * malloc (size_t size)
-malloc returns a pointer of whatever type its being casted.
-size is the “amount of space” you want.
- Examples:
int * ab = (int *) malloc (5);
int ** ppi = (int **) malloc (sizeof(int *));
char * bc = (char *) malloc (10 * sizeof(char));
void * calloc (size_t num, size_t size_of_each)
-num is the number of elements to be allocated.
-size_of_each is the size of each element.
- Example:
int * ab = (int *) calloc (10, sizeof(int));
The difference between malloc and calloc is that malloc doesn’t initialize the memory for you, so
you shouldn’t use it without setting its values. But calloc initializes the memory as 0.
void * realloc (void * ptr, size_t size)
-realloc just re-allocates memory (if you change your mind)
-size is the new total size (not added on to the old request)
If you’re done with the pointer, free it always!!
- free (void * ptr); // frees the memory from the heap
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in
Debugging C Programs
The Preprocessor : Scans your code before anything happens and reads all the lines with the
# symbol.
- Example Usage:
#ifdef DEBUG; // if DEBUG is defined.
#define debug(x,y) printf(x,y)
// define debug(x,y) as printf(x,y)
#else
#define debug(x,y) /*x, y*/
// if it’s not, then define it.
#endif
// #define = find and replace.
The reason this code is useful is because if you have a function call called debug (x,y), then you
can leave it, compile with gcc -DDEBUG (filename) (which will replace it all with printf) and see
if somethings wrong. If nothings wrong, you can just submit it as is (instead of deleting printfs).
Multi File C Programs
If you have two programs, let’s say: swap.c and main.c
In your main program, write #include swap.h at the top. This will basically “connect” the two
files.
Libraries
Libraries are the “completed” result of compilation: fully-resolved byte-code that’s ready to run
on the system. There are two types of libraries:
- Static (.a)
- Static libraries are copied by the linker into the executable.
- Program now contains the library internally.
- Shared (.so)
- Shared libraries remain in separate files always.
- Loaded dynamically as features are needed.
- All libraries start with “lib”
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in
Structs
Let’s say we create a structure:
struct user {
char userID;
char age;
}
If we want to implement this structure and start creating users, we declare them like this:
struct user Josh; // Josh is the variable name
struct user Sam;
If we want to access individual members of the structure, in this case userID , etc, we use:
Josh.userID = 260773978;
Sam.age = 19;
To turn the struct user into a whole new type, using typedef , it is written like:
typedef struct user {
char userID;
char age;
} STUDENT;
STUDENT Josh; // Now we can declare Josh and Sam like so
STUDENT Sam;
Pointers to Functions
Know the key difference between the following declarations:
- int * fn(); // a function returns an int *
- int (*fn)(); // a pointer to a function that returns an
// integer and takes no arguments
int return5() { return 5;}
int (*fn)() = return5;
// Just giving the function name, without (), means:
// give me the function’s address. Doesn’t call the function
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 10 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers