1 CS 416- Fall 2008 Session 02 TA: Tuan Phan 732-445-6450 (ext 9644) : Just leaving msg( prefer using ,

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CSE : Programming in C Instructor: Lei Wang Office: Dreese Lab 474 Office Hour: Friday.
C FAQ’S Collected from the students who attended technical round in TCS recruitment.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Topic 2d High-Level languages and Systems Software
1 CS 352- Spring 2007 TA: Tuan Phan (ext 9644) : Just leaving msg( prefer using ) Recitation:
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
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.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
C Primer.
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
Introduction to C Topics Compilation Using the gcc Compiler
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Chapter 11 Introduction to Programming in C
Pointers and dynamic memory
System Structure and Process Model
Memory Allocation CS 217.
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
Program Execution in Linux
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
C Programming Lecture-8 Pointers and Memory Management
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Chapter 11 Programming in C
15213 C Primer 17 September 2002.
Chapter 11 Introduction to Programming in C
Presentation transcript:

1 CS 416- Fall 2008 Session 02 TA: Tuan Phan (ext 9644) : Just leaving msg( prefer using , with HEADER: [CS416] …) Recitation: TH :3:35pm – 4:30 SEC 202 Office Hour: TH 5:00 – 6:00 Hill 367 Another place to find me: PANIC LAB, CORE 340 Extra: to setup appointment on Monday afternoon. TA’s Web Site: Slides for recitation, Useful Links

2 What will be in recitations Summary of lectures Projects Sample Questions / Home work More technical stuff

3 Recommend Tools for CS416 PuTTy : ssh client without GUI XManager: ssh client with GUI WinSCP: to upload files to CEREAL clusters IDE: emacs, vi …

4 CEREAL cluster / iLab Link: –Use Linux machines; DO NOT use Sun machines (including cereal.rutgers.edu) iLab: Hill –Use Rutgers ID –Transfer students: use temporary ID –Problem: meet Robert CoRE 232 Homework: Create/Activate an account on iLab

Today’s topics Communication Channels with TA Quick Introduction about C Prepare for project 1 –General Information –Calling Stack –Pointers in C –Dynamic Memory Allocation –Fork() –Others… 5

C Programming Links: – – 6

Example of Process Creation Using Fork The UNIX shell is command-line interpreter whose basic purpose is for user to run applications on a UNIX system cmd arg1 arg2... argn

Creating, Compiling and Running Your C Program Create:myprog.c Compile (1) cc myprog.c (2) cc -o myprog myprog.c Running (1)./a.out (2)./myprog 8

Compiler Options -llibrary Link with object libraries. cc calc.c -o calc -lm -Ldirectory Add directory to the list of directories containing object- library routines. cc prog.c -L/home/myname/mylibs mylib.a -Ipathname Add pathname to the list of directories in which to search for #include files with relative filenames cc prog.c -I/home/myname/myheaders -g invoke debugging option. 9

A Simple C Program #include #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ int startPoint; /* starting point for countdown */ /* prompt user for input */ printf("Enter a positive number: "); scanf("%d", &startPoint); /* read into startPoint */ /* count down and print count */ for (counter=startPoint; counter >= STOP; counter--) printf("%d\n", counter); } 10

Fred Kuhns (2/18/2016)CSE332– Object Oriented Programming Lab Another Simple C Program int main (int argc, char **argv) { int i; printf(“There are %d arguments\n”, argc); for (i = 0; i < argc; i++) printf(“Arg %d = %s\n”, i, argv[i]); return 0; } Notice that the syntax is similar to Java What’s new in the above simple program? –of course you will have to learn the new interfaces and utility functions defined by the C standard and UNIX –Pointers will give you the most trouble

What Happens When There Is More Than One Running Process? OS Code Globals Stack Heap P0 P1 P2

Run-Time Stack main() foo() Memory Before callDuring callAfter call Notes: Each box represents an activation record of a function.

Activation Record Activation Record: –information about each function –stored in run-time stack int foo(int a, int b) { int w, x, y;. return y; } b a Return value Return Address Dynamic link w x y parameter s Local variables bookeepin g

Passing pointers into a function (1) void Swap(int a, int b) { int tmp; tmp=a; a=b; b=tmp; printf(“Inside SWAP(): a=%d b=%d”, a, b); } main() { int x=2,y=3; Swap(x,y); printf(“x=%d y=%d”, x,y); } 15

Pointers in C int I; int *ptr; i=4;// Store the value 4 into the memory location associated with i. ptr= &i; // Store the address of I into the memory location associated with ptr *ptr = *ptr + 1; int a[10], x; int *pa; pa = &a[0]; /* pa pointer to address of a[0] */ x = *pa; /* x = contents of pa (a[0] in this case) */ // &a[i] ~ a + ia[i] ~ *(a+i) 16

Passing pointers into a function (2) void Swap(int *a, int *b) { int tmp; tmp=*a; *a=*b; *b=tmp; printf(“Inside SWAP(): a=%d b=%d\n”, *a, *b); } main() { int x=2,y=3; Swap(&x,&y); printf(“x=%d y=%d”, x,y); } 17

Dynamic Memory Allocation void *malloc(size_t number_of_bytes) Ex: char *cp; cp = malloc(100); int *ip; ip = (int *) malloc(100*sizeof(int)); void *calloc(size_t num_elements, size_t element_size}; int *ip; ip = (int *) calloc(100, sizeof(int)); Malloc does not initialise memory (to zero) in any way. If you wish to initialise memory then use calloc. free(pointer). kmalloc() & kfree() void * kmalloc (size_t size, int priority); void kfree (void * __ptr); 18