C Programming Types & Dynamic memory & bits & others Tutorial TEN C Programming Types & Dynamic memory & bits & others CompSci 210 - Semester Two 2016
types in C There are many types in C: char int Short long size_t float double …. etc
Making your own types Typedef structs
Exercise 1: the size of the type How can I know the size of a type (eg, short variable)? Hint: Try sizeof() Ps: do you know the difference between sizeof and strlen ?
Stack vs Heap For now, you’ve been working with what we call the ‘stack’. This is memory allocated to your program at time of execution. This memory are fixed during the compiling time and cannot be changed during the running time. The heap is used at Run time, and can grow.
stack Int func (int q, int r) { int k, m; //… return k+m; } Main(void){ Int w = 25; Func(w, 10);
Stack and heap Think of a stack of dinner plates vs a lolly scramble
An example for heap #include<stdio.h> 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; }
Allocating heap memory There are a couple of ways to do this: malloc allocates the specified number of bytes realloc increases or decreases the size of the specified block of memory. Reallocates it if needed calloc allocates the specified number of bytes and initializes them to zero free releases the specified block of memory back to the system
Dynamic memory: Pros and cons Allows us to not have predetermined program sizes Persists even when it goes away Cons: Manual freeing Fragmentation
Bits operation Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise XOR ~ Bitwise complement << Shift left >> Shift right
Bitwise AND operator & 1010 1100 ------- AND 1000
Bits operation Bitwise OR operator | 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal)
Right Shift Operator Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >> Char c = 0x1A; C = 212 = 11010100 (In binary) 212>>2 = 00110101 (In binary) [Right shift by two bits] 212>>7 = 00000001 (In binary) 212>>8 = 00000000 212>>0 = 11010100 (No Shift)
Left Shift Operator Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<. Char c = 0x1A; C =212 = 11010100 (In binary) 212<<1 = 10101000 (In binary) [Left shift by one bit] 212<<0 = 11010100 (Shift by 0) 212<<4 = 01000000 (In binary) =3392(In decimal)
Exercise 2: bits operation How to shift 0x01 to 0x10? How to get high 3 bits from 0x73? How to combine 0b00000001 and 0b0000110 to 8 bits, such that become 0b00011100?
Tips for programming Build an programming environment (Editor/debug process) you like, not hate. Sooner or later, you will face this again. Start from small snippet codes, not a whole solution. Continuously improve/enhance your program(agile development) Finish the task first, optimize the code later. Check the result of your every single enhancement/modification. It is much much better than removing each line to trace a segment fault issue. Print, as much as you can accept. You can remove them before the submission. Other students may already have met the same problem. So,… Good name (for variables or functions)can make programming easier. Save! Check your files’ directory carefully.
How to Ask programming questions The more information you provide, the easier for others to get involve or think it seriously. Don’t just say it failed. Please provide the detail error information. If you want the answer for you specific code issues, please include the code. Sometimes pasting a big block of code helps. Sometimes code is better than any explanation. Look at the questions on stackoverflow website. Good questions can also help or benefit others. Bad questions, ehh…
Good questions Good questions usually contain: Your smallest target/task or expected output. The process of your solution or what excatly you have done The detail error message or your debug print Your code (or command) Your programming environment (optional)