Miguel Garzon CrUise Lab - SITE. #include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical.

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

Programming Languages and Paradigms The C Programming Language.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
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.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Kernighan/Ritchie: Kelley/Pohl:
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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{
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
C Language Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini- computers In 1974, Unix was rewritten in C C++ and C Advantages.
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.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C Slides and captured lecture (video and sound) are available at:
Class 2 CSI2172
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 Lab 2: The Unix environment, Using vi, C programming SITE, uOttawa.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
(language, compilation and debugging) David 09/16/2011.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C LANGUAGE Characteristics of C · Small size
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Instructions for test_function
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Advanced Programming with C
C/C++ Tutorial.
An Introduction to C Programming
Checking Memory Management
C programming language
C Basics.
Computer Systems and Networks
Instructor: Ioannis A. Vetsikas
Dynamic Memory Allocation
14th September IIT Kanpur
Lecture 5: Process Creation
Pointers and dynamic memory
Dynamic Memory Allocation
Memory Allocation CS 217.
Introduction to C Topics Compilation Using the gcc Compiler
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Introduction to C Topics Compilation Using the gcc Compiler
C Programming Lecture-8 Pointers and Memory Management
C By Example 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.
Debugging.
Dynamic Memory – A Review
Makefiles, GDB, Valgrind
C Tutorial Adapted from Wei Qian’s C tutorial slides.
Module 13 Dynamic Memory.
Presentation transcript:

Miguel Garzon CrUise Lab - SITE

#include dynamic memory allocationdynamic memory allocation Input/output String handlingString handling Mathematical functionsMathematical functions Characters

Variables: Storage Duration and Scope Local variable –Local scope, store in the call stack Static local variable –Local scope, single and statically allocated Global variable –Accessible in every scope Example: vars.cvars.c

IO Functions Standard file IO: –FILE *fopen(const char *filename, const char *mode); –size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); –size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream); –int fseek(FILE *stream, long int offset, int whence); –int fclose(FILE *stream); Example: file.c from C file input/output file.cC file input/output

IO Functions – printf(“the int value is %d”, var); printf(“the string value is %s”, var); printf(“the charactr and double values are %c and %e”, var); printf(“the double value is %e”, var); scanf(“enter an int: %d”, var); scanf(“enter a string: %s”, var); scanf(“enter a character and a double: %c %e”, var); scanf(“enter a string: %s”, var);

struct.c typedef struct{ int i; char c; float f; char cc[3]; } aaa; int len = sizeof(aaa);

sort.c void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );... int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) {…} qsort (values, 6, sizeof(int), compare);

Pointers pointer.c int a[10]; int b[] = {1, 2, 3}; int *pb = &b[1]; char *arr[] = {"aaa", "bbb", "ccc", "ddd"}; char **p = arr; char *q = arr[0]; char **r = &arr[0];

dmem.c void *malloc(size_t size); void free(void *pointer); void *realloc(void *pointer,size_t size); Parameters pointer Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function. size New size for the memory block, in bytes. If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned. malloccallocrealloc

Processes fork() creates a child process –On success, the PID of the child process is returned in the parent. and a 0 is returned in the child. –On failure, a -1 will be returned in the parent's context, fork.c int x = 1; /* Parent and child will get private copies*/ pid_t pid = fork(); if (pid == 0) printf("in child (%d), x = %d\n", getpid(), ++x); else printf("in parent(%d), x = %d\n", getpid(), --x); printf("bye from process (%d) with x = %d\n", getpid(), x);

… No boolean type in CNo boolean type in C #define TRUE 1 #define TRUE 1 #define FALSE 0 #define FALSE 0 while(1) { ; /* do nothing */ } Two functions cannot have the same name Variables must be declared at the top of a basic block (for some c systems)Variables must be declared at the top of a basic block (for some c systems) (Cela ne compile pas ) (Cela ne compile pas ) { int a; printf("Hello world\n"); char b; } { int a; printf("Hello world\n"); char b; } Exceptions? Garbage collection in CGarbage collection in C

bad.c char a[10]; char *b = "bbbbb"; b[0]='a'; a[8000]='b'; Bug: –a[8000] is accessing an invalid memory address

GDB Debug Steps $ gcc –o bad bad.c $ gdb./bad (gdb) break main [set break point at main()] (gdb) run [start program] (gdb) step 1 [step one line] Program received signal SIGSEGV, Segmentation fault. main () at bad.c:15 15 a[8000]='b';

Compiler GNU Compiler CollectionGNU Compiler Collection A compiler system produced by the GNU Project supporting various programming languages Compile C code to an executableCompile C code to an executable –gcc -o –gcc -o Reporting all warning messagesReporting all warning messages –gcc -Wall -o –gcc -Wall -o

Compiler An example of compiling two.c files into an executable programAn example of compiling two.c files into an executable program –gcc -Wall -o NomduFichier main.c list.c–gcc -Wall -o NomduFichier main.c list.c An example of compiling two files and then link them separatelyAn example of compiling two files and then link them separately –gcc -Wall -o main.o -c main.c–gcc -Wall -o main.o -c main.c –gcc -Wall -o list.o -c list.c–gcc -Wall -o list.o -c list.c –gcc -Wall -o assign1prob1 main.o list.o–gcc -Wall -o assign1prob1 main.o list.o

Compiler An example of compiling two.c files into an executable programAn example of compiling two.c files into an executable program –gcc -Wall -o NomduFichier main.c list.c–gcc -Wall -o NomduFichier main.c list.c An example of compiling two files and then link them separatelyAn example of compiling two files and then link them separately –gcc -Wall -o main.o -c main.c–gcc -Wall -o main.o -c main.c –gcc -Wall -o list.o -c list.c–gcc -Wall -o list.o -c list.c –gcc -Wall -o assign1prob1 main.o list.o–gcc -Wall -o assign1prob1 main.o list.o

Compiler enter gdbenter gdb –gdb–gdb Quit gdbQuit gdb –quit–quit printing line from a source fileprinting line from a source file –list–list running a programrunning a program –run–run breakpoint at a line numberbreakpoint at a line number –break : –break : –break –break break at a particular functionbreak at a particular function –break : –break : set a breakpoint with conditionset a breakpoint with condition –break if –break if deleting breakpointsdeleting breakpoints –delete–delete proceed onto the next breakpointproceed onto the next breakpoint –continue–continue Single stepSingle step –step–step

Make file Makefile and the make command –We can write many commands in a script file and run it all at once; –For example: all: gcc hello.c –o hello.o mkdir -p folderNameCreer mv hello.o folderNameCreer 1) Write the previous commands in a file called Makefile-1.txt; (in the same directory of hello.c); 2) Type the command: make -f Makefile-1.txt