Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
CSE240 Lecture 11
Javier Gonzalez
January 11, 2017
Programming
0
1.8k
CSE240 Lecture 11
Introduction to Programming Languages
malloc and free
(201701)
Javier Gonzalez
January 11, 2017
Tweet
Share
More Decks by Javier Gonzalez
See All by Javier Gonzalez
javiergs
0
520
javiergs
0
360
javiergs
0
330
javiergs
0
340
javiergs
0
260
javiergs
0
210
javiergs
0
130
javiergs
0
190
javiergs
0
480
Other Decks in Programming
See All in Programming
yasaichi
31
7.7k
vixentael
0
320
makomakok
0
150
grapecity_dev
0
170
grapecity_dev
0
170
minamijoyo
3
470
heistak
2
130
meemeelab
0
280
afilina
PRO
0
100
grapecity_dev
0
170
lilobase
PRO
1
710
bells17
0
360
Featured
See All Featured
iamctodd
19
2k
stephaniewalter
260
11k
malarkey
119
16k
philhawksworth
190
17k
sferik
610
54k
jonyablonski
19
1.2k
caitiem20
308
17k
holman
448
130k
addyosmani
311
21k
yeseniaperezcruz
302
31k
orderedlist
PRO
328
36k
rasmusluckow
318
18k
Transcript
jgs CSE 240 Introduction to Programming Languages Lecture 11: malloc
and free Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs
malloc #include <stdio.h> #include <stdlib.h> int main(){ int x = 5; int *y = (int*) malloc (sizeof(int)); *y = 7; printf("value of y: %d \n", *y); printf("address of y: %p \n", y); printf("address of y: %p \n", &y); printf("value of x: %d \n", x); printf("address of x: %p \n", &x); free(y); return 0; } 7 0x1c 0x1c y 0x78 5 x 0x74
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs
malloc and arrays #include <stdio.h> #include <stdlib.h> int main(){ int x = 5; int *y = (int*) malloc (sizeof(int) * 3); *(y+0) = 1; y[1] = 5; *(y+2) = 7; printf("value of y[0]: %d \n", y[0]); printf("value of y[1]: %d \n", y[1]); printf("value of y[2]: %d \n", y[2]); free(y); return 0; } 1 0x1c 0x1c y 0x78 5 7
jgs What about 2D arrays?
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs
2D Array
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs
malloc and 2D arrays #include <stdio.h> #include <stdlib.h> int main(){ int **array = (int**) malloc (sizeof(int*) * 3); *(array+0) = (int*) malloc (sizeof(int ) * 3); array[1] = (int*) malloc (sizeof(int ) * 3); *(array+2) = (int*) malloc (sizeof(int ) * 3); array[0][0] = 1; array[1][1] = 1; array[2][2] = 1; *(*(array +1 ) + 2) = 3; // this is array[1][2] printf ("array [1][2] is %d \n", array[1][2]); printf ("array [2][2] is %d \n", *(*(array+2)+2) ); // free (s) go here return 0; }
jgs Test Yourselves
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 jgs
Dynamic 2D Array
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs
Dynamic 2D Array
jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu
Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.