Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Types & Dynamic memory & bits & others

Similar presentations


Presentation on theme: "C Programming Types & Dynamic memory & bits & others"— Presentation transcript:

1 C Programming Types & Dynamic memory & bits & others
Tutorial TEN C Programming Types & Dynamic memory & bits & others CompSci Semester Two 2016

2 types in C There are many types in C: char int Short long size_t float
double …. etc

3 Making your own types Typedef structs

4 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 ?

5 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.

6 stack Int func (int q, int r) { int k, m; //… return k+m; } Main(void){ Int w = 25; Func(w, 10);

7 Stack and heap Think of a stack of dinner plates vs a lolly scramble

8 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; }

9 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

10 Dynamic memory: Pros and cons
Allows us to not have predetermined program sizes Persists even when it goes away Cons: Manual freeing Fragmentation

11 Bits operation Operators Meaning of operators & Bitwise AND |
Bitwise OR ^ Bitwise XOR ~ Bitwise complement << Shift left >> Shift right

12 Bitwise AND operator & AND 1000

13 Bits operation Bitwise OR operator | 12 = (In Binary) 25 = (In Binary) Bitwise OR Operation of 12 and | ________ = 29 (In decimal)

14 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 = (In binary) 212>>2 = (In binary) [Right shift by two bits] 212>>7 = (In binary) 212>>8 = >>0 = (No Shift)

15 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 = (In binary) 212<<1 = (In binary) [Left shift by one bit] 212<<0 = (Shift by 0) 212<<4 = (In binary) =3392(In decimal)

16 Exercise 2: bits operation
How to shift 0x01 to 0x10? How to get high 3 bits from 0x73? How to combine 0b and 0b to 8 bits, such that become 0b ?

17 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.

18 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…

19 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)


Download ppt "C Programming Types & Dynamic memory & bits & others"

Similar presentations


Ads by Google