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
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
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
490
源内ハンズオン概要編
hideg
0
230
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
1
240
Cloudflare is Agents
chimame
0
190
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
130
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
110
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
190
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
580
My Marp Sample
sinoue0108
0
130
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
330
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
7
2.3k
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
150
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
260
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
The browser strikes back
jonoalderson
0
1.6k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
630
Large-scale JavaScript Application Architecture
addyosmani
515
110k
4 Signs Your Business is Dying
shpigford
187
23k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Unsuck your backbone
ammeep
672
58k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Navigating Team Friction
lara
192
16k
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