• c++ delete 后还有必要set Null 吗?


     最开始我们Declare array as a pointer, allocate with new

    int* a = NULL; // pointer to an int, intiallly to nothing.

      A dynamically allocated array is declared as a pointer, and must not use the fixed array size declaration. The above declaration creates a pointer, but doesn't yet allocate any memory to it.

    Allocate an array with code>new

       When the desired size of an array is known, allocate memory for it with the new operator and save the address of that memory in the pointer. Remember: Pointers may be subscripted just as arrays are. The example below reads in a number and allocates that size array.

    int* a = NULL;   // Pointer to int, initialize to nothing.
    int n;           // Size needed for array
    cin >> n;        // Read in the size
    a = new int[n];  // Allocate n ints and save ptr in a.
    for (int i=0; i<n; i++) {
        a[i] = 0;    // Initialize all elements to zero.
    }
    . . .  // Use a as a normal array
    delete [] a;  // When done, free memory pointed to by a.
    a = NULL;     // Clear a to prevent using invalid memory reference.

    Freeing memory with delete

      When you are finished with dynamically allocated memory, free it with the delete operator. After memory is freed, it can be reused by later new requests. Memory that your program didn't free will be freed when the program terminates. Never free memory that wasn't dynamically allocated - the results are unpredictable.

    delete [] a;  // Free memory allocated for the a array.
    a = NULL;     // Be sure the deallocated memory isn't used.


    Use [] when deleting arrays

    You must specify "[]" when deleting an array, but not for a single value. It isn't possible to delete only part of an array.

     

    Do you have to reset a pointer after delete?

      Following the delete in these examples, I reset the pointer to NULL. This isn't strictly necessary, but it's very good practice so that any use of the pointer will produce an error. Attempts to use memory location 0, which is the normal default value of NULL, will be blocked by the way most operating systems allocate memory.

     Why doesn't delete reset the pointer? It does in some systems, but the language specification does not require it, so not all systems do it.

     a=NULL后,加上这个

    cout<<*a; 运行有错误。因为不指向任何内容,试图访问时,will be blocked by the most os allocate memory.

    cout<<a;输出00000000;

    如果不a=NULL

    cout<<*a;产生一个随机的数字。

    cout<<a;输出指针本身的地址

    能不能delete NULL?

       没必要,但是可以

       C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect. In other words, deleting a null pointer is a safe (yet useless) operation. There is no need to check the pointer for null-ness before passing it to delete:

    if (p) // useless; delete already checks for a null value
    delete(p);

    转载一篇文章;

       原文在此:Can you delete a NULL pointer? 
    没全转过来,只是留个记录。 

    根据目前的C++草案,Working Draft, Standard for Programming Language C++ 

    Working Draft, Standard for Programming Language C++ 写道
    5.3.5 (6): If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2).


      它说如果不是空指针就调用析构函数。换句话说delete NULL;什么也不会发生。 

    不过delete一个指针并不会改变指针的值(不会在delete后将指针设为NULL),所以调用了delete之后最好自己把指针设为空指针,

     

  • 相关阅读:
    7.ps相关选项
    6.ps的大U和小u区别
    5.进程优先级
    4.状态间的六种转换情况
    3.进程的不同状态
    2.进程与程序的关系
    1.进程概念
    不换行
    for引用变量
    脚本进阶
  • 原文地址:https://www.cnblogs.com/youxin/p/2592347.html
Copyright © 2020-2023  润新知