質問するログイン新規登録

回答編集履歴

3

operator

2017/07/23 06:54

投稿

yumetodo
yumetodo

スコア5852

answer CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  じゃないと何を指し示すのか(point)わからないじゃないですか。
18
18
 
19
- ちなみにどこを指しているかわからない or NULL pointerはdereferenceしてはいけません。
19
+ ちなみにどこを指しているかわからない or NULL pointerはdereferenceしてはいけません。dereferenceとは具体的に言うと、`operator *`(単項演算子), `operator []`の呼び出しです。
20
20
 
21
21
  ```c
22
22
  int x;

2

UB

2017/07/23 06:54

投稿

yumetodo
yumetodo

スコア5852

answer CHANGED
@@ -25,8 +25,12 @@
25
25
  int* p3 = NULL;
26
26
  int* p4 = malloc(sizof(int));
27
27
  *p1;//OK
28
- //*p2;//NG
28
+ //*p2;//NG: Undefined Behavior: C11, 6.5.3.2p4
29
- //*p3;//NG
29
+ //*p3;//NG: Undefined Behavior: C11, 6.5.3.2p4
30
30
  *p4;//OK
31
31
  free(p4);
32
- ```
32
+ ```
33
+
34
+ ref:
35
+
36
+ - [c++ - Is dereferencing a pointer that's equal to nullptr undefined behavior by the standard? - Stack Overflow](https://stackoverflow.com/questions/28573215/is-dereferencing-a-pointer-thats-equal-to-nullptr-undefined-behavior-by-the-sta)

1

追記

2017/07/23 05:37

投稿

yumetodo
yumetodo

スコア5852

answer CHANGED
@@ -2,10 +2,31 @@
2
2
 
3
3
  正回答では、格納する場所を作り、それを指すポインタを`scanf`に渡しました。
4
4
 
5
- ```cpp
5
+ ```c
6
6
  int x;//格納する場所
7
7
  int* x_p = &x;//格納する場所を指すポインタ
8
8
  scanf("%d", x_p);
9
9
  ```
10
10
 
11
- ここで上例で`x_p`のみの状態が誤回答の状態です。どこを指しているか不明なポインタを渡しています。
11
+ ここで上例で`x_p`のみの状態が誤回答の状態です。どこを指しているか不明なポインタを渡しています。
12
+
13
+ # 追記
14
+
15
+ > ポインタはあらかじめ格納する場所を用意しておく必要があるということですか?
16
+
17
+ じゃないと何を指し示すのか(point)わからないじゃないですか。
18
+
19
+ ちなみにどこを指しているかわからない or NULL pointerはdereferenceしてはいけません。
20
+
21
+ ```c
22
+ int x;
23
+ int* p1 = &x;
24
+ int* p2;
25
+ int* p3 = NULL;
26
+ int* p4 = malloc(sizof(int));
27
+ *p1;//OK
28
+ //*p2;//NG
29
+ //*p3;//NG
30
+ *p4;//OK
31
+ free(p4);
32
+ ```