1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

shiracamus様からご指摘があったので、再度私のほうでプログラムを検証し、再度メモリ解放実験を行いました。

//============================================================================
// Name        : At4.cpp
// Author      : 乃木坂好きのITエンジニア
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

# include <iostream>
using namespace std;

//メイン処理
int main(){
  //ポインタ変数を定義する
  int *p1;



  p1 = new int;         // (※) int 型変数1個のメモリ確保

  //p1をxのアドレスで初期化
  *p1 = 10;
  //p1のポインタの値を表示
  cout << "p1がさしている整数型は:" << *p1 << endl;
  //p1のポインタの値を1000にする
  *p1 = 1000;

  cout << "p1 が指している整数型は: " << *p1 << "\n"; //p1ポインタの値を表示

  delete p1;    // (※) メモリの解放

  cout << "メモリ解放後のp1 が指している整数型は: " << *p1 << endl; //メモリ解放後のp1ポインタが指し示す値


  return 0;
}

表示結果は次の通りです。
result.jpg

1
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?