SlideShare a Scribd company logo
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 1/21
型の話
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 2/21
型って?
intとか、stringとか
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 3/21
それぞれの型って具体的に何が違うの?
文字なんだから数字とは違うだろって?
機械語に直せば両方とも0と1になるんだから同じじゃないの?
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 4/21
組み込み出身者での視点での私の考え
intだのstringだの、結局は型によって確保するメモリの大きさが違う
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 5/21
Cでは
処理系によるが、以下のようなバイト長(メモリの大きさ)
char:1バイト
int:4バイト
double:8バイト
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 6/21
1バイトって?
1バイト=8ビット
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 7/21
1ビットって?
0と1
二進数(バイナリ)
1ビットだと2つの数字しか表せない(つまり2パターンしか表せない)
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 8/21
じゃあ2ビットだったら何パターンある?
[][]←この箱の中に0と1が入るイメージ
[0][0]
[0][1]
[1][0]
[1][1]
4パターン表せた!
ということは[0][0]を0として、[0][1]を1、[1][0]を2、[1][1]を3としたら、0と1だ
けで数字(10進数)表現できるんじゃね!?
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 9/21
じゃあ1バイト=8ビットだったらどれだけの数字を表せるの?
8ビット=[][][][][][][][]
2ビットで4パターン、ということは4ビットなら16パターン
8ビットなら、16x16パターンつまり2^8(2の8乗)=256パターン
0始まりにすると、0~255までの数字を8つの0と1の組み合わせで表現できる
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 10/21
本題に戻って
型によって確保するメモリが違う
適切なデータの入れ物を用意しないと桁が溢れたり(オーバーフロー)する
ちなみにFFなんかはオーバーフロー関係のバグがあったりする↓
https://wikiwiki.jp/ffdic/裏技・バグ/【オーバーフロー】
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 11/21
結局、型って何?
確保するバイト数(だと私は思っている)
普段意識しないけど、知ってるといつかどこかで得するかも
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 12/21
余談1
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 13/21
Cにおける構造体
typedef struct {
// char int 1 構造体
char foo; // 1
int bar; // 4
} hoge
この構造体、何バイト?
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 14/21
出力してみる(paiza.io)
#include <stdio.h>
typedef struct {
// char int 1 構造体
char foo; // 1
int bar; // 4
} hoge;
int main(void){
hoge Hoge;
int hoge_size = 0;
hoge_size = sizeof(Hoge);
printf("%d", hoge_size); // 8 表示
}
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 15/21
(処理系にもよるが)実は5バイトではない
1バイトと5バイトのメンバで出来ている構造体は5バイトではない
実は8バイト
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 16/21
なんで?
多くのコンパイラは、データを2バイト(=1ワード)単位で揃えようとする
多くのCPUが『奇数番地から始まるデータより偶数番地から始まるデータの方が
効率的に扱える』ようになっているため
charが奇数個あるような場合、1バイトの「何もない領域」を追加して割り当て、
次にメモリ上に確保されるであろうデータが偶数番地から始まるよう調整する
これを「パディング」と呼ぶ
構造体でなくてもchar型変数を1個宣言したような場合や、奇数個の要素を持つ
char型配列を宣言した場合にも、その次に1バイトのパディングが行われたりする
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 17/21
ちなみに
因みにpaizaでは4バイト単位で揃えようとしているため、8バイトになっている
この「○バイト単位で揃える」というのをアラインメントと言ったりする(はず)
ちなみにGo言語でも同様だったりする(https://ota42y.com/blog/2015/05/06/go‑
struct‑offset/)
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 18/21
余談2
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 19/21
volatile修飾子
コンパイラによる最適化を抑制
黙っていると処理系で行われる最適化
volatileの英語の意味は「気まぐれな、移り気な」
つまり「値が勝手に変わってしまう」変数に対してvolatileを宣言
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 20/21
「値が勝手に変わってしまう」って、そんなことあるの?
ある
典型的には組み込みのシステムで使われる、機械の状態を示す変数(レジスタ)
例えば「ボタンを押している/離している」を「押している=1」、「離している
=0」とすると
人間がボタンを操作するごとに、プログラムとは無関係に値が変わる
最適化されていると、0→1の変化をプログラムが検出できない
2019/3/14 Marp Web (under construction)
http://localhost:8080/ 21/21
参考資料
https://world‑trigger.net/2018/03/14/【プログラミング】図解 volatile宣言とは
c言語/
https://qiita.com/YukiMiyatake/items/1191ab03b6c0b5a22876
ここらへんが分かりやすい

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

型の話

  • 1. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 1/21 型の話
  • 2. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 2/21 型って? intとか、stringとか
  • 3. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 3/21 それぞれの型って具体的に何が違うの? 文字なんだから数字とは違うだろって? 機械語に直せば両方とも0と1になるんだから同じじゃないの?
  • 4. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 4/21 組み込み出身者での視点での私の考え intだのstringだの、結局は型によって確保するメモリの大きさが違う
  • 5. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 5/21 Cでは 処理系によるが、以下のようなバイト長(メモリの大きさ) char:1バイト int:4バイト double:8バイト
  • 6. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 6/21 1バイトって? 1バイト=8ビット
  • 7. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 7/21 1ビットって? 0と1 二進数(バイナリ) 1ビットだと2つの数字しか表せない(つまり2パターンしか表せない)
  • 8. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 8/21 じゃあ2ビットだったら何パターンある? [][]←この箱の中に0と1が入るイメージ [0][0] [0][1] [1][0] [1][1] 4パターン表せた! ということは[0][0]を0として、[0][1]を1、[1][0]を2、[1][1]を3としたら、0と1だ けで数字(10進数)表現できるんじゃね!?
  • 9. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 9/21 じゃあ1バイト=8ビットだったらどれだけの数字を表せるの? 8ビット=[][][][][][][][] 2ビットで4パターン、ということは4ビットなら16パターン 8ビットなら、16x16パターンつまり2^8(2の8乗)=256パターン 0始まりにすると、0~255までの数字を8つの0と1の組み合わせで表現できる
  • 10. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 10/21 本題に戻って 型によって確保するメモリが違う 適切なデータの入れ物を用意しないと桁が溢れたり(オーバーフロー)する ちなみにFFなんかはオーバーフロー関係のバグがあったりする↓ https://wikiwiki.jp/ffdic/裏技・バグ/【オーバーフロー】
  • 11. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 11/21 結局、型って何? 確保するバイト数(だと私は思っている) 普段意識しないけど、知ってるといつかどこかで得するかも
  • 12. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 12/21 余談1
  • 13. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 13/21 Cにおける構造体 typedef struct { // char int 1 構造体 char foo; // 1 int bar; // 4 } hoge この構造体、何バイト?
  • 14. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 14/21 出力してみる(paiza.io) #include <stdio.h> typedef struct { // char int 1 構造体 char foo; // 1 int bar; // 4 } hoge; int main(void){ hoge Hoge; int hoge_size = 0; hoge_size = sizeof(Hoge); printf("%d", hoge_size); // 8 表示 }
  • 15. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 15/21 (処理系にもよるが)実は5バイトではない 1バイトと5バイトのメンバで出来ている構造体は5バイトではない 実は8バイト
  • 16. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 16/21 なんで? 多くのコンパイラは、データを2バイト(=1ワード)単位で揃えようとする 多くのCPUが『奇数番地から始まるデータより偶数番地から始まるデータの方が 効率的に扱える』ようになっているため charが奇数個あるような場合、1バイトの「何もない領域」を追加して割り当て、 次にメモリ上に確保されるであろうデータが偶数番地から始まるよう調整する これを「パディング」と呼ぶ 構造体でなくてもchar型変数を1個宣言したような場合や、奇数個の要素を持つ char型配列を宣言した場合にも、その次に1バイトのパディングが行われたりする
  • 17. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 17/21 ちなみに 因みにpaizaでは4バイト単位で揃えようとしているため、8バイトになっている この「○バイト単位で揃える」というのをアラインメントと言ったりする(はず) ちなみにGo言語でも同様だったりする(https://ota42y.com/blog/2015/05/06/go‑ struct‑offset/)
  • 18. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 18/21 余談2
  • 19. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 19/21 volatile修飾子 コンパイラによる最適化を抑制 黙っていると処理系で行われる最適化 volatileの英語の意味は「気まぐれな、移り気な」 つまり「値が勝手に変わってしまう」変数に対してvolatileを宣言
  • 20. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 20/21 「値が勝手に変わってしまう」って、そんなことあるの? ある 典型的には組み込みのシステムで使われる、機械の状態を示す変数(レジスタ) 例えば「ボタンを押している/離している」を「押している=1」、「離している =0」とすると 人間がボタンを操作するごとに、プログラムとは無関係に値が変わる 最適化されていると、0→1の変化をプログラムが検出できない
  • 21. 2019/3/14 Marp Web (under construction) http://localhost:8080/ 21/21 参考資料 https://world‑trigger.net/2018/03/14/【プログラミング】図解 volatile宣言とは c言語/ https://qiita.com/YukiMiyatake/items/1191ab03b6c0b5a22876 ここらへんが分かりやすい