Java的优点是不可能破坏计算机的内存,也不需要单独存储数组大小。 C的的优点是数组访问编译以直接读取/写入内存,而则更快。 Hi all,I need to come up with some differences between arrays in Java and C, I havesearched Google and so far all I have found is the following:Arrays in Java are reference types with automatic allocation of memory. InC, arrays are groups of variables of the same type in adjacent memory.Allocation for dynamic arrays is handled by the programmer.This is an 8 mark question in an old exam paper, so I am assuming there aremore differences, but where can I find them?!Thank you for your help.--Paul Morrison 解决方案I would try this search target on google:<c arrays shortcomings>A Java array is basically thisstruct jdouble1d{double *mem;int size;};Evey time you read and write, the java compiler checks against the sizemember and throws an exception for out of bounds. Internally it callsmalloc() to allocate the memory, and by some magic the garbage collectorknows when the array goes out of scope and calls free().A Java 2d array is thisstruct jdouble2d{struct jdouble1d *mem;int size;};You will see that this has the quirk that not all the sub-arrays need be ofthe same length.What is a C array - just a chunk of memory that the program interprets asbeing doubles, or chars, or whatever.To allocate on the stack you use the notationdouble buff[123];To allocate on the heap you call malloc()double *buff = malloc(123 * sizeof(double));If you call malloc() you must call free() manually, the compiler doesn''thave a magic garbage collector that does it for you.Finally a C 2d arrays are a bit tricky. If we declaredouble box[8][10];we get a chunk of memory containing 800 doubles.box[3][4] = 1;anddouble box2[800];box[3 * 10 + 4] = 1;are essentially equivalent.However you can also construct a 2d array by calling lots of mallocs().double **box = malloc(8 * sizeof(double *));for(i=0;i<8;i++)box[i] = malloc(10 * sizeof(double));and you use the same notationbox[3][4] = 1;as for the 2d array, despite the fact that the underlying representation isquite different.The advantage of Java is that it is impossible to corrupt the computer''smemory, and you d not need to store the array size separately. The advantgeof C is that array accesses compile to direct memory read/writes, and so arefaster.size member and throws an exception for out of bounds. Internally it calls malloc() to allocate the memory, and by some magic the garbagecollector knows when the array goes out of scope and calls free(). A Java 2d array is this struct jdouble2d { struct jdouble1d *mem; int size; }; You will see that this has the quirk that not all the sub-arrays needbe of the same length. What is a C array - just a chunk of memory that the programinterprets as being doubles, or chars, or whatever. To allocate on the stack you use the notation double buff[123]; To allocate on the heap you call malloc() double *buff = malloc(123 * sizeof(double)); If you call malloc() you must call free() manually, the compilerdoesn''t have a magic garbage collector that does it for you. Finally a C 2d arrays are a bit tricky. If we declare double box[8][10]; we get a chunk of memory containing 800 doubles. box[3][4] = 1; and double box2[800]; box[3 * 10 + 4] = 1; are essentially equivalent. However you can also construct a 2d array by calling lots ofmallocs().You can also construct a 2D array by just calling malloc twice.See C FAQ item 6.16, and look at the second example code. http://www.eskimo.com/~scs/C-faq/q6.16.htmlAlso take a look at the following links: http://code.axter.com/allocate*2darray.h http://code.axter.com/allocate*2darray.c double **box = malloc(8 * sizeof(double *)); for(i=0;i<8;i++) box[i] = malloc(10 * sizeof(double)); and you use the same notation box[3][4] = 1; as for the 2d array, despite the fact that the underlyingrepresentation is quite different. The advantage of Java is that it is impossible to corrupt thecomputer''s memory, and you d not need to store the array size separately. Theadvantge of C is that array accesses compile to direct memory read/writes, andso are faster. 这篇关于Java和C中一维数组之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 19:43