/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

Similar documents
I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu

I117 II I117 PROGRAMMING PRACTICE II SOFTWARE DEVELOPMENT ENV. 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

I117 II I117 PROGRAMMING PRACTICE II DEBUG Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

I117 II I117 PROGRAMMING PRACTICE II HASH Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

r07.dvi

ohp07.dvi

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental data structures /06/14(Tue) / Memory Management

I117 プログラミング演習II

ohp03.dvi

r03.dvi

O(N) ( ) log 2 N

untitled

r08.dvi

ohp08.dvi

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

joho07-1.ppt

I117 II I117 PROGRAMMING PRACTICE II SCRIPT LANGUAGE 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

Microsoft PowerPoint - H22プログラミング第一(E)#12

ex14.dvi

untitled

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

Microsoft Word - Cプログラミング演習(12)

program.dvi

XMPによる並列化実装2

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

新・明解C言語 ポインタ完全攻略

C B

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

C言語によるアルゴリズムとデータ構造

tuat1.dvi

I J

ex12.dvi

第5回お試しアカウント付き並列プログラミング講習会

slide5.pptx

untitled

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

lexex.dvi

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

ex01.dvi

6-1

memo

& & a a * * ptr p int a ; int *a ; int a ; int a int *a

超初心者用

(search: ) [1] ( ) 2 (linear search) (sequential search) 1

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

BW BW

PowerPoint プレゼンテーション

Introduction Purpose This training course describes the configuration and session features of the High-performance Embedded Workshop (HEW), a key tool

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

,,,,., C Java,,.,,.,., ,,.,, i

PowerPoint プレゼンテーション

yacc.dvi

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

ex01.dvi

programmingII2019-v01

WinHPC ppt

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

バイオプログラミング第 1 榊原康文 佐藤健吾 慶應義塾大学理工学部生命情報学科

pptx

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

Taro-リストⅢ(公開版).jtd


I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

XcalableMP入門

Page 1 of 6 B (The World of Mathematics) November 20, 2006 Final Exam 2006 Division: ID#: Name: 1. p, q, r (Let p, q, r are propositions. ) (10pts) (a

Microsoft PowerPoint - kougi9.ppt

平成 31 年度 筑波大学大学院博士課程 システム情報工学研究科 コンピュータサイエンス専攻 博士前期課程 ( 一般入学試験 8 月期 ) 試験問題基礎科目 ( 数学, 情報基礎 ) Mathematics/Fundamentals of Computer Science [ 注意事項 ][Inst

解きながら学ぶC言語

comment.dvi

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

新版明解C言語 実践編

1.ppt

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology


Minimum C Minimum C Minimum C BNF T okenseq W hite Any D


cpp1.dvi

エラー処理・分割コンパイル・コマンドライン引数

Microsoft Word - j201drills27.doc

LIFO(last in first out, ) 1 FIFO(first in first out, ) 2 2 PUSH POP : 1

ohp11.dvi

r11.dvi

1) OOP 2) ( ) 3.2) printf Number3-2.cpp #include <stdio.h> class Number Number(); // ~Number(); // void setnumber(float n); float getnumber();

News_Letter_No35(Ver.2).p65

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

Sport and the Media: The Close Relationship between Sport and Broadcasting SUDO, Haruo1) Abstract This report tries to demonstrate the relationship be

AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

Microsoft Word - j201drills27.doc

第1回 プログラミング演習3 センサーアプリケーション

Java (5) 1 Lesson 3: x 2 +4x +5 f(x) =x 2 +4x +5 x f(10) x Java , 3.0,..., 10.0, 1.0, 2.0,... flow rate (m**3/s) "flow

02: 変数と標準入出力

Transcription:

I117 II I117 PROGRAMMING PRACTICE II 2 MEMORY MANAGEMENT 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu@jaist.ac.jp

/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination

/ FINAL REPORT free topic : decide topic by 7/5(Tue) report us of the topic by e-mail: i117report@jaist.ac.jp / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program choose good theme TAask TA and me The final report must have deep consideration

/ TODAY'S INDEX / memory management malloc () / fundamental structure / array and linked list / tree, stack, queue

MAIN() Exercise: PRINTS MAIN() ARGUMENTS / prints main() arguments.! #include <stdio.h>! int main(int argc, char* argv[])! {! int i;! for(i = 0; i < argc; i++)! {! }! }! printf("argv[%d]: %s\n", i, argv[i]);!

/ MEMORY MANAGEMENT

/ ALLOCATE NEW MEMORY #include <stdlib.h> void * malloc (size_t size); void free (void *ptr); malloc: Memory ALLOCate size / allocate "size"- bytes memory and return the pointer. free: malloc () / frees the malloc'ed memory. *) malloc () free / you should free all the unnecessary malloc'ed memory. E.x.) struct listnode *target = (struct listnode *) malloc (sizeof (struct listnode)); free (target);

EXERCISE: DYNAMIC ALLOCATION OF 2-D ARRAY malloc() / use malloc() twice.! char array[m][n + 1] / take 2 arguments and create char array[m][n + 1].! array[0] "ABC..." / Fill "ABC..." in array [0].! array[1] "BCD..." / Fill with shifted string like "BCD..." in array[1] and after.! array[x]nul/ Terminate with NUL string in each array[x].! print / print the two arguments.! array[x] print / print each array[x] as one line.!

/ ANSWER 1. #include <stdio.h>! 2. #include <stdlib.h>! 3. void! 4. main(int argc, char **argv)! 5. {! 6. int m, n;! 7. int i, j;! 8. char **array;! 9. if (argc < 3)! 10. {! 11. printf ("%s <m> <n>\n", argv[0]);! 12. exit (1);! 13. }! 14. m = atoi (argv[1]);! 15. n = atoi (argv[2]);! 1. printf ("m = %d, n = %d\n", m, n);! 2. array = (char **) malloc (m * sizeof (char *));! 3. for (i = 0; i < m; i++)! 4. array[i] = (char *) malloc (n + 1);! 5. for (i = 0; i < m; i++)! 6. {! 7. for (j = 0; j < n; j++)! 8. array[i][j] = 'A' + i + j;! 9. array[i][n] = '\0';! 10. }! 11. for (i = 0; i < m; i++)! 12. printf ("%02d: %s\n", i, array[i]);! 13. }!

/ MEMORY MANAGEMENT / Dynamic memory allocation / Free-list, Memory-pool / for example, manage constant-size memory chunks in a linked list / reference count (Buddy Memory Allocation) http://en.wikipedia.org/wiki/buddy_memory_allocation / change size of array doubly each time / garbage collection

/ ARRAY AND LINKED LIST

(ARRAY) N P r o g r a m \0 / two dimension array M

(ARRAY) / PROS AND CONS : / the size of the array is static. insert / i 1 2 3 4 5 6 7 8 9 delete /

(LINKED-LIST)

(LINK-LIST) pros : / a list has basically unlimited size cons / need bookkeeping meta- memory

( ) or

DOUBLY LINKED LIST unlimited sized, fast insertion and deletion linked with pointers (SingleDoubly) Linked List NULL= =NULL

LIST LIMITATION unlimited size NULL= =NULL new =NULL add

LIST INSERTION NULL= =NULL new

LIST INSERTION NULL= =NULL new

LIST INSERTION NULL= =NULL new

LIST INSERTION done by manipulation of 4 pointers no shifting of slide is deletion

LIST DELETION NULL= =NULL unnecessary

LIST DELETION NULL= =NULL unnecessary

LIST DELETION NULL= =NULL unnecessary

LISTNODE LISTNODE STRUCTURE void * "void *" serves as abstract struct listnode { }; struct listnode *; struct listnode *; void *;

INSIDE LIST left-> left-> struct listnode *left; right-> right-> struct listnode *right; to be changed struct listnode *target;

() LIST ADDITION EXAMPLE /* left right target left target right */ left-> = target; target-> = left; target-> = right; right-> = target;

() LIST DELETION EXAMPLE /* left target right target left right */ left-> = right; right-> = left; /* left right target */ target->-> = target->; target->-> = target->;

LIST LIST STRUCTURE struct list { struct listnode *head; /* */ struct listnode *tail; /* */ int count; /* */ };

POINTER MANIPULATION

EXERCISE: ARRAY MODIFICATION PROGRAM / Has one array inside! / the array is variable length! (fgets) / input one line from stdin! "add " + <string> + '\n'! "remove " + <string> + '\n'! "add " + <num> + <string> + '\n'! ignore rest.! / After each inputs, print the length of array and its contents.!