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
OOP 2012 - Hint: Dynamic allocation in c++
Search
allan914
May 08, 2012
Programming
77
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
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.4k
Oxcを導入して開発体験が向上した話
yug1224
4
350
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
140
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
320
Inside Stream API
skrb
1
810
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.1k
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
620
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
220
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
170
act1-costs.pdf
sumedhbala
0
130
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
770
Featured
See All Featured
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
630
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
740
Building AI with AI
inesmontani
PRO
1
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Building the Perfect Custom Keyboard
takai
2
800
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
290
Building Applications with DynamoDB
mza
96
7.1k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
180
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.6k
Building Adaptive Systems
keathley
44
3.1k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
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