C Programming Types & Dynamic memory & bits & others

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Stack and Heap Memory Stack resident variables include:
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic memory allocation and Pointers Lecture 4.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Carnegie Mellon 1 Malloc Recitation Ben Spinelli Recitation 11: November 9, 2015.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Lecture 3 Translation.
CSE 220 – C Programming malloc, calloc, realloc.
Hank Childs, University of Oregon
CS1010 Programming Methodology
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
14 Compilers, Interpreters and Debuggers
A bit of C programming Lecture 3 Uli Raich.
ENEE150 Discussion 07 Section 0101 Adam Wang.
2016.
Dynamic Memory Allocation
Programmazione I a.a. 2017/2018.
Programmazione I a.a. 2017/2018.
CSCI206 - Computer Organization & Programming
Hank Childs, University of Oregon
Dynamic Memory Allocation
Math in C The math blocks you've used in Scratch can all be recreated in C!
Some Basics for Problem Analysis and Solutions
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Programming and Data Structures
Computer Organization & Compilation Process
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Homework Reading Programming Assignments Finish K&R Chapter 1
Malloc Lab CSCI 380: Operating Systems
TUTORIAL 7 CS 137 F18 October 30th.
Dynamic Memory A whole heap of fun….
Computer Organization & Compilation Process
Dynamic Memory – A Review
Module 13 Dynamic Memory.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

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)