zoukankan      html  css  js  c++  java
  • 释放了内存却继续使用它

    释放了内存却继续使用它。 有三种情况:

    (1)程序中的对象调用关系过于复杂,实在难以搞清楚某个对象究竟是否已经释放了内 存,此时应该重新设计数据结构,从根本上解决对象管理的混乱局面。

    (2)函数的 return 语句写错了,注意不要返回指向“栈内存”的“指针”或者“引用”

    , 因为该内存在函数体结束时被自动销毁。

    (3)使用 free 或 delete 释放了内存后,没有将指针设置为 NULL。导致产生“野指针”。

     1 #include <iostream>
     2 #include <string.h>
     3 
     4 //main()函数
     5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     6 using namespace std;
     7 int main(int argc, char** argv) {
     8         //声明字符数组
     9     char string[]="This is a test.";
    10     int n;
    11 
    12     //获得字符串的长度
    13     cout<<"string:"<<string<<endl;
    14     n=strlen(string);
    15     cout<<"The length of "<<"""<<string<<"": "<<n<<endl;
    16 
    17     //输入字符并计算其长度
    18     cout<<"string:";
    19     cin>>string;
    20     n=strlen(string);
    21     cout<<"The length of "<<"""<<string<<"": "<<n<<endl;
    22     return 0;
    23 }
  • 相关阅读:
    UVa 725 Division --- 简单枚举
    最短路之Floyd算法
    最短路之Bellman-Ford算法
    快速排序算法
    Dijkstra算法 --- 单源最短路
    HDU 2553 N皇后问题 --- 经典回溯
    HDU 2072 单词数
    HDU 1241 Oil Deposits --- 入门DFS
    jq val() 和 html() 用法注意
    jq 登录正则验证
  • 原文地址:https://www.cnblogs.com/borter/p/9413669.html
Copyright © 2011-2022 走看看