zoukankan      html  css  js  c++  java
  • 用 malloc 或 new 申请内存之后,应该立即检查指针值是否为 NULL

    用 malloc 或 new 申请内存之后,应该立即检查指针值是否为 NULL。

    防止使用指针值为 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 ch,string[80],*p;
    10     int n;
    11 
    12     //输入字符串和要查找的字符
    13     cout<<"Test strchr():"<<endl;
    14     cout<<"string:";
    15     cin>>string;
    16     cout<<"ch    :";
    17     cin>>ch;
    18 
    19     //在string中查找ch中的字符并显示
    20     p=strchr(string,ch);
    21     cout<<"p    :"<<p<<endl;
    22 
    23     //输入字符串和要查找的字符串并查找
    24     char substr[80];
    25     cout<<"Test strstr():"<<endl;
    26     cout<<"substr:";
    27     cin>>substr;
    28 
    29     //在string中查找substr中的字符串并显示
    30     p=strstr(string,substr);
    31     cout<<"p    :"<<p<<endl;
    32     return 0;
    33 }
  • 相关阅读:
    PHP ffmpeg详解简单上手 window64 音频amr转mp3
    PHP强制转换类型
    PHP使用array_unique对二维数组去重处理
    发布包
    CSS用户界面样式
    数组
    结构类型(枚举,结构,联合)
    循环
    程序结构
    文件
  • 原文地址:https://www.cnblogs.com/borter/p/9413674.html
Copyright © 2011-2022 走看看