Informatyka Wy-03 Dynamiczna alokacja pamięci, wyjątki mgr inż. Krzysztof Kołodziejczyk krzysztof.m.kolodziejczyk@pwr.edu.pl 13.03.2019
Strona kursu http://w12.pwr.wroc.pl/inf/
Konsultacje Piątek 13:00 C2/111a
Konspekt 1 W poprzednim odcinku... 2 man 3 Dynamiczna alokacja pamięci
Konspekt 1 W poprzednim odcinku... Stałe i zmienne Wskaźniki Tablice Arytmetyka wskaźników 2 man
Stałe 42 42.0 4 "42"
Stałe 42 42.0 4 "42" 42L 0xBUL 0.0F 0.0L 0777 \n \0x20
Zmienne char int, short int, long int float, double, long double signed / unsigned array *
Zasięg zmiennej i n t plus2 ( i n t a ){ i n t x=2; a += x ; return a ; } void main ( void ){ i n t a=5; i n t b=plus2 ( a ) ; p r i n t f ( "%d %d\n", a, b ) ; // 5 7 p r i n t f ( "%d\n", x ) ; // e r r o r : x undeclared }
Zasięg zmiennej void main ( void ){ i n t a=5; i n t b=6; { i n t a=7; b=8; } p r i n t f ( "%d %d\n", a, b ) ; // 5 8 p r i n t f ( "%d\n", x ) ; // e r r o r : x undeclared }
Zasięg zmiennej void main ( void ){ i n t a ; f o r ( i n t i =0; i <10; i ++) { a = i ; } audytoriumf ( "%d\n", a ) ; audytoriumf ( "%d\n", i ) ; }
Zasięg zmiennej void main ( void ){ i n t a ; i n t i ; f o r ( i =0; i <10; i ++) { a = i ; } z a p l u s a f ( "%d %d\n", a, i ) ; }
Zasięg zmiennej void main ( void ){ f o r ( i n t i =0; i <2; i ++) { f o r ( i n t i =0; i <3; i ++) { p r i n t f ( "%d ", i ) ; } } }
Zasięg zmiennej void main ( void ){ i n t i ; i n t i ; // e r r o r : r e d e c l a r a t i o n o f i }
Wskaźniki f0c1be80 [80]00 00 00[80]00 00 00 f0c1be88 00 00 00 00 00 00 00 00 f0c1be90 00 00 00 00 00 00 00 00 f0c1be98 [84]be c1 f 0 00 00 00 00 i n t a=192; i n t b=204; i n t p_a ; p_a=&a ; //p_a = f0c1be80 p_a = 128; p_a=&b ; //p_a = f0c1be84 p_a = 128;
Tablice jednowymiarowe f0c1be80 02 00 00 00 08 00 00 00 f0c1be88 20 00 00 00 80 00 00 00 f0c1be90 00 02 00 00 00 08 00 00 f0c1be98 [80 be c1 f0]00 00 00 00 i n t a [ ] = { 2, 8, 3 2, 1 2 8, 5 1 2, 2 0 4 8 } ; i n t p_a = a ; &a [ 0 ] == a == p_a a [ 0 ] == p_a
Tablice dwuwymiarowe f0c1be80 01 02 03 04 07 06 08 09 f0c1be88 04 08 01 05 00 00 00 00 f0c1be90 [80 be c1 f0 84 be c1 f0] f0c1be98 [88 be c1 f0]00 00 00 00 char b [ 3 ] [ 4 ] = { { 1, 2, 3, 4 }, { 7, 6, 8, 9 }, {4,8,1,5} } ;
Tablice dwuwymiarowe f0c1be80 01 02 03 04 07 06 08 09 f0c1be88 04 08 01 05 00 00 00 00 f0c1be90 [80 be c1 f0 84 be c1 f0] f0c1be98 [88 be c1 f0]00 00 00 00 char b [ 3 ] [ 4 ] = { { 1, 2, 3, 4 }, { 7, 6, 8, 9 }, {4,8,1,5} } ; b == b [ 0 ] [ 0 ] == ( b [ 0 ] )
Arytmetyka wskaźników f0c1be80 02 00 00 00 08 00 00 00 f0c1be88 20 00 00 00 80 00 00 00 f0c1be90 00 02 00 00 00 08 00 00 f0c1be98 00 00 00 00 00 00 00 00 i n t a [ ] = { 2, 8, 3 2, 1 2 8, 5 1 2, 2 0 4 8 } ; a [ 2 ] == ( a+2) == 32 &a [ 2 ] == a + 2 s i z e o f ( i n t ) == f0c1be88
Arytmetyka wskaźników f0c1be80 01 02 03 04 07 06 08 09 f0c1be88 04 08 01 05 00 00 00 00 f0c1be90 [80 be c1 f0 84 be c1 f0] f0c1be98 [88 be c1 f0]00 00 00 00 char b [ 3 ] [ 4 ] = { { 1, 2, 3, 4 }, { 7, 6, 8, 9 }, {4,8,1,5} } ; b [ 2 ] == f0c1be88 ( ( b+2) + 1) == b [ 2 ] [ 1 ] == 8
Arytmetyka wskaźników i n t a = 258; char p_a = ( char )&a ; p_a [ 1 ] ; //1 p_a [ 0 ] ; //2
Konspekt 1 W poprzednim odcinku... 2 man
man man nazwa_strony
man man nazwa_strony man numer_sekcji nazwa_strony
man man nazwa_strony man numer_sekcji nazwa_strony man printf #printf(1) Ta strona podręcznika man opisuje polecenie printf w wersji GNU.
man man nazwa_strony man numer_sekcji nazwa_strony man printf #printf(1) Ta strona podręcznika man opisuje polecenie printf w wersji GNU. man 3 printf #printf(3) Linux Programmer s Manual
man pages 1 komendy ogólne 2 wywołania systemowe 3 funkcje biblioteki C 4 pliki specjalne 5 formaty plików 6 gry komputerowe i wygaszacze ekranu 7 różne 8 administracja systemem i daemony
Why does man print gimme gimme gimme at 00:30? We ve noticed that some of our automatic tests fail when they run at 00:30 but work fine the rest of the day. They fail with the message gimme gimme gimme in stderr, which wasn t expected. Why are we getting this output? https://unix.stackexchange.com/questions/405783
Konspekt 3 Dynamiczna alokacja pamięci malloc/calloc free realloc memcpy
malloc i n t a = 5 ; i n t p_a = &a ;
malloc #i n c l u d e <s t d l i b. h> (... ) i n t p_a = malloc ( s i z e o f ( i n t ) ) ; p_a = 5 ;
malloc i n t p_a = malloc ( s i z e o f ( i n t ) ) ; p_a = 5 ;
malloc i n t p_a = malloc ( s i z e o f ( i n t ) ) ; p_a = 5 ;
calloc i n t a [ 1 0 ] ; i n t p_a = a ; p_a = a ;
calloc i n t p_a = malloc (10 s i z e o f ( i n t ) ) ;
calloc i n t p_a = c a l l o c (10, s i z e o f ( i n t ) ) ;
calloc i n t a [ 1 0 ] ; i n t p_a = malloc (10 s i z e o f ( i n t ) ) ; i n t p_a = c a l l o c (10, s i z e o f ( i n t ) ) ;
free void memory( void ) { char napis ; napis = malloc ( 1 0 2 4 ) ; }
i n t main ( void ){ char p l i k [ 1 6 ] ; char buf [ 6 4 ] ; FILE wsk_pliku ; while ( 1 ) { p r i n t f ( "Nazwa p l i k u : " ) ; g e t s ( p l i k ) ; i f ( ( wsk_pliku=fopen ( p l i k, " r " ) )!= NULL) { while ( f g e t s ( buf, 6 4, wsk_pliku ) ) { p r i n t f ( "%s ", buf ) ; } } } free #i n c l u d e <s t d i o. h> #i n c l u d e <s t d l i b. h>
free 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t d l i b. h> 3 #i n c l u d e <s t r i n g. h> 4 5 void main ( void ){ 6 char buf=null; char p l i k [ 1 6 ] ; 7 long l e n =0; FILE wsk_pliku=null; 8 while ( 1 ) { 9 p r i n t f ( "Nazwa p l i k u : " ) ; f g e t s ( p l i k, 1 6, s t d i n ) ; 10 p l i k [ s t r l e n ( p l i k ) 1]=0; 11 i f ( ( wsk_pliku=fopen ( p l i k, " r " ) )!= NULL) { 12 while ( g e t l i n e (&buf,& len, wsk_pliku )!= 1) { 13 p r i n t f ( "%s ", buf ) ; 14 } 15 f c l o s e ( wsk_pliku ) ; 16 } 17 }
realloc void *realloc(void *ptr, size_t size); ptr==null > malloc size==0 > free move
memcpy char a [ ] = " H e l l o world! " ; a = "Goodbye world! " ; // e r r o r : assignment to e x p r e s s i o n with array type
memcpy char a = malloc ( 3 2 ) ; p r i n t f ( "%p\n", a ) ; a = " Test1 " ; p r i n t f ( "%p\n", a ) ; a = " Test2 " ; p r i n t f ( "%p\n", a ) ; a = " Test1 " ; p r i n t f ( "%p\n", a ) ; / 0 x557ce5ab6260 0 x557ce540a808 0 x557ce540a80e 0 x557ce540a808 /
memcpy 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t d l i b. h> 3 #i n c l u d e <s t r i n g. h> 4 i n t main ( void ){ 5 char a = " Hello world! " ; 6 p r i n t f ( "%p\n", a ) ; 7 strncpy ( a, "Goodbye! ", 1 3 ) ; 8 p r i n t f ( "%p %s \n", a, a ) ; 9 return 0 ; 10 } 11 / 0 x55cc60220814 12 segmentation f a u l t ( core dumped) /
memcpy 1 #i n c l u d e <s t d i o. h> 2 #i n c l u d e <s t d l i b. h> 3 #i n c l u d e <s t r i n g. h> 4 i n t main ( void ){ 5 char a = malloc ( 1 3 ) ; 6 strncpy ( a, " H e l l o world! ", 1 3 ) ; 7 p r i n t f ( "%p %s \n", a, a ) ; 8 strncpy ( a, "Goodbye! ", 1 3 ) ; 9 p r i n t f ( "%p %s \n", a, a ) ; 10 return 0 ; 11 } 12 / 13 0 x55d22b4b8260 Hello world! 14 0 x55d22b4b8260 Goodbye! 15 /
memcpy 1 #i n c l u d e <s t d i o. h> 2 3 char t e s t ( void ) { 4 char a [ ] = " t e s t 1 " ; 5 return a ; 6 } 7 void main ( void ) { 8 char a = t e s t ( ) ; 9 p r i n t f ( "%s \n", a ) ; 10 11 } 12 13 warning : f u n c t i o n r e t u r n s address o f l o c a l v a r i a b l e 14 segmentation f a u l t ( core dumped)
Funkcje zakazane gets() strcpy() strcat() *sprintf("%s")
Bibliografia http://lmgtfy.com/?q=man+fgets https://linux.die.net/man/3/realloc http://robert.wojcik.staff.iiar.pwr.wroc.pl/dydaktyka/wyklad_c/inz/ https://pl.wikibooks.org/wiki/c/błędy#obsługa_błędów_i_wyjątków
Koniec Dziękuję za uwagę!