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
PRO
January 11, 2017
Programming
0
2.1k
CSE240 Lecture 11
Introduction to Programming Languages
malloc and free
(202202)
Javier Gonzalez
PRO
January 11, 2017
Tweet
Share
More Decks by Javier Gonzalez
See All by Javier Gonzalez
CSE360 Tutorial 01
javiergs
PRO
0
6
JGS594 Lecture 23
javiergs
PRO
0
400
JGS594 Lecture 22
javiergs
PRO
0
380
JGS594 Lecture 21
javiergs
PRO
0
300
JGS594 Lecture 20
javiergs
PRO
0
180
JGS594 Lecture 19
javiergs
PRO
0
340
JGS594 Lecture 18
javiergs
PRO
0
340
JGS594 Lecture 17
javiergs
PRO
0
320
JGS594 Lecture 16
javiergs
PRO
1
520
Other Decks in Programming
See All in Programming
Hapticをカスタマイズしてみよう / ZOZO Tech Talk #6 Customize Haptic
endoumari
0
360
Why declarative UI frameworks?
tkuenneth
0
200
マイクロサービスプラットフォーム向け負荷試験基盤の初期リリースを終えた話
yuyu_hf
PRO
1
470
Swift Concurrencyによる安全で快適な非同期処理
tattn
2
340
Cloud-Conference-Day-Spring Cloud + Spring Webflux: como desenvolver seu primeiro microsserviço reativo em Java?
kamilahsantos
1
160
Viteはいいぞ/Vite is Good
dojineko
1
110
Reactive Microservices with Spring Boot and JHipster - Spring I/O 2022
mraible
PRO
1
350
未経験QAの私が、よきQA(Question Asker) になっていく物語
atamaplus
0
370
Kotlin KSP - Intro
taehwandev
1
510
コードの解析と言語習得の心得
jinjin33333
0
130
Git Rebase
bkuhlmann
7
1k
GraphQL+KMM開発でわかったこと / What we learned from GraphQL+KMM development
kubode
0
130
Featured
See All Featured
Code Review Best Practice
trishagee
41
6.8k
Robots, Beer and Maslow
schacon
152
7.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
343
17k
Statistics for Hackers
jakevdp
781
210k
Pencils Down: Stop Designing & Start Developing
hursman
112
9.8k
Navigating Team Friction
lara
175
11k
Unsuck your backbone
ammeep
659
55k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
349
27k
KATA
mclloyd
7
8.6k
How to name files
jennybc
39
59k
A Modern Web Designer's Workflow
chriscoyier
689
180k
Rails Girls Zürich Keynote
gr2m
86
12k
Transcript
jgs CSE 240 Introduction to Programming Languages Lecture 11: malloc
and free Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs
Announcement § Homework 02 Programming with C
jgs malloc and free
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 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 | 5 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
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
Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs
Questions
jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D.
javiergs@asu.edu Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.