CS 162 Lecture Notes - Lecture 22: Memory Management, C Dynamic Memory Allocation, Memory Address
![](https://new-preview-html.oneclass.com/rakV67y5LKMpNOwpbyD9Nw8XEJWevPqo/bg1.png)
CS 162 – Lecture 22 – Introduction to C (Short Lecture, Group Activity)
C vs. C++
o & means address of – always
o All libraries that will be included end in .h
o Variables must be declared at the top of the function
o “tructs ust hae the ord struct i frot of the heeer declared uless it
has been typedef
o File I/O works off a file pointer
o Compile with gcc
C vs. C++: Input/Output
o Get input via scanf()
▪ Takes a format specifier and an address to store input into
▪ Ep: scaf%d, &_u;
• & correspods to address
o Produce output using printf()
▪ Prints a string which may have format specifiers
▪ If format specifiers are provided, the variables should be produced in the
order of appearance
▪ Ep: pritfDa: %d Moth: %s Year: %d \, da, oth, ear;
C vs. C++ Dynamic Memory
o Use malloc()
o Takes the size of memory in bytes
o Returns a memory address as a void*
o Exp: int* array = (int*) malloc(sizeof(int)*5);
o Use free() to delete
▪ Name of the dynamic memory in parentheses
• Free(array)
find more resources at oneclass.com
find more resources at oneclass.com