Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
OOP 2012 - Hint: Dynamic allocation in c++
Search
allan914
May 08, 2012
Programming
78
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
OOP 2012 - Hint: Dynamic allocation in c++
allan914
May 08, 2012
More Decks by allan914
See All by allan914
ICS 2012 - Midterm Solution for Problem 1-3
allan914
0
77
Other Decks in Programming
See All in Programming
スマート反転とウェブアクセシビリティ
camiha
0
160
高専キャリア LT 発表内容
crysta1221
6
5.6k
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
180
XHTMLが残したもの
yosuke_furukawa
PRO
2
450
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
120
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
120
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.5k
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
3
510
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
690
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
4
650
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
290
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Bash Introduction
62gerente
615
220k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Google's AI Overviews - The New Search
badams
0
1.6k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
YesSQL, Process and Tooling at Scale
rocio
174
15k
ラッコキーワード サービス紹介資料
rakko
1
4.8M
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Transcript
Dynamic Allocation in C++ allan914.120418 (update: 120420)
Example 1 #include<iostream> using namespace std; int main() { int*
a; int n; cin >> n; a=new int [n]; for(int i=0;i<n;i++) a[i]=i+1; for(int i=0;i<n;i++) cout << a[i] << " "; delete [] a; return 0; }
Example 1: Result Sample Input 10 Sample Output
1 2 3 4 5 6 7 8 9 10
Example 2 #include<iostream> using namespace std; int main() { int**
a; int n,m; cin >> n; cin >> m; a=new int* [n]; for(int i=0;i<n;i++) a[i]=new int [m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=i+j+1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << a[i][j] << " "; cout << endl; } for(int i=0;i<n;i++) delete [] a[i]; delete [] a; return 0; }
Example 2: Result Sample Input 5 4 Sample
Output 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8
Example 3 #include<iostream> using namespace std; int main() { int*
a; int n,m; cin >> n; cin >> m; a=new int [n*m]; int** b; b=new int* [n]; for(int i=0;i<n;i++) b[i]=&(a[n*i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) b[i][j]=i+j+1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << b[i][j] << " "; //cautious with CodeBlocks ! cout << endl; } delete [] b; delete [] a; return 0; }
Example 3: Result Sample Input 5 4 Sample
Output 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8
Example 4 // Headers emitted, cautious with CodeBlocks ! void*
new2d(int n,int m,int size) { void **a = new void* [m*sizeof(void*) + n*m*size]; for(int j=0;j<m;j++) a[j] = ((char *)(a+m)) + j*n*size; return a; } int main() { int** a; int n,m; cin >> n; cin >> m; a = (int**)new2d(m,n,sizeof(int)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=i+j+1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << a[i][j] << " "; cout << endl; } delete [] a; return 0; }
Example 4: Result Sample Input 5 4 Sample
Output 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8
Example 5 #include<iostream> #include<vector> using namespace std; int main() {
int n,m; cin >> n; cin >> m; vector<vector<int> > a(n, vector<int>(m)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=i+j+1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << a[i][j] << " "; cout << endl; } return 0; }
Example 5: Result Sample Input 5 4 Sample
Output 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8