delete 数组后 指针是否为null

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hanfeiyan/article/details/84917804
/** \brief
    测试 delete 数组指针后,数组指针是否为 nullptr
 *
    测试结果: delete 数组指针后, 数组指针不为 nullptr
 *
 * \param
 * \param
 * \return
 *
 */
#include <iostream>



using std::cout ;

int main(){

    int * ary = new int [100];

    if(ary == nullptr) cout << "ary == nullptr \n";
    else {

        cout << "ary pointer is not nullptr.\n";
    }

    ary[1] = 1l;

    ary[2] = 12;

    delete []  ary;

    ary = nullptr;

    if(ary==nullptr) cout << "ary == nullptr \n";

    else {

        cout << "ary pointer is not nullptr.\n";
    }

    return 0;
}

 

 

ary pointer is not nullptr.
ary == nullptr 

Process returned 0 (0x0)   execution time : 0.004 s
Press ENTER to continue.

 

展开阅读全文

Java中数组为null

02-26

[size=14px]我的代码中想使用string数组去储存从文件中读取的字符串,我使用全局变量dictionary[]这个字符串数组在public String[] readFileByLines(String fileName, int firstWordLength)这个方法中复制tempString中的值,测试之后发现dictionary[]中可以得到所有的值。但是当我在 public boolean deCipher(String cipherTextInput, int[] keyInput,int firstWordLength,String[] dictionary)这个方法中再次测试dictionary[]中的值的时候,发现全部是空。我知道这可能存在只想地址,让后tempString被销毁,所以dictionary指向空的可能。但是我不明白怎样才能避免这种情况。 急求大家帮忙。代码贴在下面,谢谢了!大家主要帮忙看一下readFileByLines和deCipher这两个方法就好了。[/size]rnrnrnrn[code=java]public class VigAlgoCracker rn char[] inputTextChar;rn char[] keyChar;rn String[] dictionary;rn final int wordIndex = 167964;rn int realSize = 0;rn //IdentityHashMap map=new IdentityHashMap();rnrn public void setTextChar(String cipherTextInput) rn String temp = cipherTextInput.replaceAll(" ", "");rn this.inputTextChar = temp.toCharArray();rn rn if (this.inputTextChar.length == 0) rn System.out.println("The input is empty!");rn return;rn rnrn rnrn public boolean deCipher(String cipherTextInput, int[] keyInput,int firstWordLength) rn setTextChar(cipherTextInput);rn int length1 = this.inputTextChar.length;rn rn int[] cipherDigt = getDigit(length1, this.inputTextChar);rnrn int[] plainDigt = getResultDigt(cipherDigt, keyInput);rn for(int i=0;i 64) rn digit[i] = (int) textChar[i] - 65;rn else if ((int) textChar[i] > 96 && (int) textChar[i] < 123) rn digit[i] = (int) textChar[i] - 97;rn else rn System.out.println("Your input is invalid,please enter letter only!");rn return null;rn rn rnrn return digit;rn rnrn public int[] getResultDigt(int[] charDigt, int[] keyDigt) rn int length1 = charDigt.length;rn int length2 = keyDigt.length;rn int[] resultDigt = new int[charDigt.length];rn int j = 0;rn for (int i = 0; i < length1; i++) rn if (j < length2) rn resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26;rn if (resultDigt[i] < 0) rn resultDigt[i] = resultDigt[i] + 26;rn rn else rn j = 0;rn resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26;rn if (resultDigt[i] < 0) rn resultDigt[i] = resultDigt[i] + 26;rn rn rn j++;rn rnrn return resultDigt;rn rnrnrn public void getKeyWord(String cipherText, int keyLength,int firstWordLength,String[] dictionary) rn rn int[] key = new int[keyLength];rn for (int i = 0; i < keyLength; i++) rn key[i] = 65;rn rn rn int arrIndex = 0;rn int alphLength = 0;rn boolean test=false;rnrn while (checkKey(key, keyLength)) rn while (alphLength < 26) rn key[0] = 65 + alphLength;rn if(deCipher(cipherText, key,firstWordLength)==true)rn return;rn rn rn System.out.println();rn alphLength++;rn rn while (arrIndex < keyLength && key[arrIndex] == 90 && checkKey(key, keyLength)) rn key[arrIndex] = 65;rn alphLength = 0;rn arrIndex++;rn rn if (arrIndex < keyLength) rn if (key[arrIndex] < 90) rn key[arrIndex]++;rn arrIndex = 0;rn rn rn rn rn rnrnrn public boolean checkKey(int[] key, int keyLength) rn for (int i = 0; i < keyLength; i++) rn if (key[i] != 90) rn return true;rn rn rn return false;rn rnrn public void readFileByLines(String fileName, int firstWordLength) rn File file = new File(fileName);rn BufferedReader reader = null;rn dictionary = new String[wordIndex];rnrnrn try rn System.out.println("The files content show by lines:");rn reader = new BufferedReader(new FileReader(file));rn String tempString = null;rn int index = 0;rn while ((tempString = reader.readLine()) != null) rn if (tempString.length() == firstWordLength) rn rn this.dictionary[index] = new String(tempString);rn System.out.println(this.dictionary[index]);rn index++;rn rnrn rn rn reader.close();rn catch (IOException e) rn e.printStackTrace();rn finally rn if (reader != null) rn try rn reader.close();rn catch (IOException e1) rn rn rn rnrn rn rn rnrnrn System.out.println();rnrnrnrnrnrnrn rnrn return dictionary;rnrn rnrnrn public static void main(String[] args) rnrn rnrn System.out.println("Please enter the file name or directory:");rn Scanner input=new Scanner(System.in);rn String fileName=input.nextLine();rn //String fileName="dict.txt";rn System.out.println("Please enter the ciphertext:");rn String ciphertext= input.nextLine();rn System.out.println("Please enter the first word length:");rn int firstWordLength=input.nextInt();rn System.out.println("Please enter the key length:");rn int keyLength=input.nextInt();rn VigAlgoCracker cracker=new VigAlgoCracker();rn cracker.readFileByLines(fileName,firstWordLength);rn cracker.getKeyWord(ciphertext,keyLength,firstWordLength);rn rn[/code] 论坛

没有更多推荐了,返回首页