Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CSE240 Lecture 11
Search
Javier Gonzalez-Sanchez
PRO
January 11, 2017
Programming
0
2.3k
CSE240 Lecture 11
Introduction to Programming Languages
malloc and free
(202202)
Javier Gonzalez-Sanchez
PRO
January 11, 2017
Tweet
Share
More Decks by Javier Gonzalez-Sanchez
See All by Javier Gonzalez-Sanchez
CSC305 Summer Lecture 03
javiergs
PRO
0
38
CSC305 Summer Lecture 02
javiergs
PRO
0
34
CSC305 Summer Lecture 01
javiergs
PRO
0
53
CSC307 Lecture 16
javiergs
PRO
0
150
CSC570 Lecture 13
javiergs
PRO
0
41
CSC307 Lecture 17
javiergs
PRO
0
120
UP Lecture 30
javiergs
PRO
0
86
UP Lecture 25
javiergs
PRO
0
78
CSC486 Lecture 14
javiergs
PRO
0
160
Other Decks in Programming
See All in Programming
フロントエンドのパフォーマンスチューニング
koukimiura
6
2.3k
AI Ramen Fight
yusukebe
0
120
Workers を定期実行する方法は一つじゃない
rokuosan
0
130
SwiftでMCPサーバーを作ろう!
giginet
PRO
2
210
オンコール⼊⾨〜ページャーが鳴る前に、あなたが備えられること〜 / Before The Pager Rings
yktakaha4
2
1.2k
それ CLI フレームワークがなくてもできるよ / Building CLI Tools Without Frameworks
orgachem
PRO
13
3.3k
#QiitaBash TDDで(自分の)開発がどう変わったか
ryosukedtomita
1
180
変化を楽しむエンジニアリング ~ いままでとこれから ~
murajun1978
0
590
プロダクトという一杯を作る - プロダクトチームが味の責任を持つまでの煮込み奮闘記
hiliteeternal
0
300
DataformでPythonする / dataform-de-python
snhryt
0
110
CDK引数設計道場100本ノック
badmintoncryer
2
590
構造化・自動化・ガードレール - Vibe Coding実践記 -
tonegawa07
0
160
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
25
1.8k
How to Ace a Technical Interview
jacobian
278
23k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.6k
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
KATA
mclloyd
31
14k
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Docker and Python
trallard
45
3.5k
Transcript
jgs CSE 240 Introduction to Programming Languages Lecture 11: malloc
and free Dr. Javier Gonzalez-Sanchez
[email protected]
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.
[email protected]
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.