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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.6k
JavaDoc 再入門
nagise
1
430
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
220
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
170
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
420
The NotImplementedError Problem in Ruby
koic
1
990
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
120
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.5k
はてなアカウント基盤 State of the Union
cockscomb
1
1.1k
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
440
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
590
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
7.1k
Featured
See All Featured
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
340
Building the Perfect Custom Keyboard
takai
2
800
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
160
Speed Design
sergeychernyshev
33
1.9k
Test your architecture with Archunit
thirion
1
2.3k
Making Projects Easy
brettharned
120
6.7k
GraphQLとの向き合い方2022年版
quramy
50
15k
What's in a price? How to price your products and services
michaelherold
247
13k
Agile that works and the tools we love
rasmusluckow
331
22k
Ethics towards AI in product and experience design
skipperchong
2
320
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.6k
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