Java&c/c++ array and dynamically allocated memory comparison

In Java, the declared use of arrays and the dynamic allocation of arrays are as follows:
import java.util.Scanner;


public class Fun {

	public static void main(String[] args) {
               int[] a={1,2,3};
               for(int i=0;i<a.length;++i){
    	       System.out.println(a[i]);
             }
	       Scanner scan=new Scanner(System.in);
	       int     size=scan.nextInt();
	       String  strInput=scan.next();
	       scan.close();
	       char[] array=new char[size];//dynamic allocation
	       for(int i=0;i<array.length;i++){
	    	   array[i]=strInput.charAt(i);
	    	   System.out.println(array[i]);
	       }		
	}
}

In contrast, it is slightly different in C/C++, you can pay attention when using it, and give some examples for comparison:

#include<stdio.h>
intmain()
{
	int count;
	scanf("%d", &count);
	int*p = new int[count];
	int*mark = p;
	int i;
	for ( i = 0; i < count; i++)
	{
		p[i] = i;
	}

	for (i = 0; i < count; i++)
	{
		printf("%d\n", *p);
		p++;
	}

	delete[]mark;
	return 0;
}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815173&siteId=291194637