Java和C中一维数组之间的差异 [英] Differences between one-dimensional arrays in Java and C

查看:48
本文介绍了Java和C中一维数组之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我需要想出Java和C中的数组之间的一些差异,我有搜索Google的b $ b $到目前为止所有我已发现如下:


Java中的数组是具有自动内存分配的引用类型。在
C中,数组是相邻内存中相同类型的变量组。

动态数组的分配由程序员处理。


这是旧试卷中的8个标记问题,所以我假设有更多的差异,但我在哪里可以找到它们?!


感谢您的帮助。


-

Paul Morrison

解决方案

" Paul Morrison"写道:

我需要在Java和C中找出一些数组之间的差异,我已经搜索了谷歌,到目前为止我发现的所有内容如下:

Java中的数组是具有自动内存分配的引用类型。在C语言中,数组是相邻内存中相同类型的变量组。
动态数组的分配由程序员处理。

这是一个8标记的问题。老试卷,所以我假设有更多的差异,但我在哪里可以找到它们?!




我会在谷歌上尝试这个搜索目标:

< c数组缺点>




" Paul Morrison" < PA ** @ morrison1985.freeserve.co.uk>写了


我需要在Java和C中找出一些数组之间的差异,我已经搜索了Google,到目前为止,我发现以下内容:

Java中的数组是具有自动内存分配的引用类型。在C语言中,数组是相邻内存中相同类型的变量组。
动态数组的分配由程序员处理。

这是一个8标记的问题。老试卷,所以我假设有更多的差异,但我在哪里可以找到它们?!



Java阵列基本上就是这个

struct jdouble1d

{

double * mem;

int size;

}; < e />
Evey读写时间,java编译器检查大小

成员并抛出异常超出界限。在内部它调用

malloc()来分配内存,并且通过一些魔法垃圾收集器

知道阵列何时超出范围并调用free()。 />

这是一个Java 2d数组


struct jdouble2d

{

struct jdouble1d * mem;

int size;

};


你会看到这有怪癖而不是所有的子阵列需要

相同的长度。


什么是C数组 - 只是一块内存,程序解释为

是双打,或者是chars,或者其他什么。

要在堆栈上分配你使用的符号

double buff [123];


要在堆上分配你调用malloc()


double * buff = malloc(123 * sizeof(double));


如果你调用malloc()你必须手动调用free(),编译器没有
有一个神奇的垃圾收集器为你做这个。


最后一个C 2d arra是有点棘手的。如果我们宣布


双盒[8] [10];


我们得到一块包含800双打的内存。


box [3] [4] = 1;




double box2 [800];

box [3 * 10 + 4] = 1;


基本上是等价的。

但是你也可以通过调用很多来构造一个二维数组mallocs()。


double ** box = malloc(8 * sizeof(double *));

for(i = 0; i< 8; i ++)

box [i] = malloc(10 * sizeof(double));


你使用相同的符号

box [3] [4] = 1;


和2d数组一样,尽管底层表示是完全不同的。
。 />

Java的优势在于它不可能破坏计算机的内存,并且你不需要单独存储数组大小。 C的优点是b $ b是数组访问编译以直接读取/写入内存,因此更快地获得


< blockquote>

Malcolm写道:

" Paul Morrison" < PA ** @ morrison1985.freeserve.co.uk>写了


我需要想出Java中的数组和
C之间的一些差异,我搜索了Google,到目前为止我发现的所有内容如下:

Java中的数组是自动分配
内存的引用类型。在C中,数组是相邻
内存中相同类型的变量组。动态数组的分配由程序员处理。

这是旧考试试卷中的8个标记问题,所以我假设
存在更多差异,但我在哪里可以找到它们? !
一个Java数组基本上就是这个结构jdouble1d
{
双* mem;
int size;
};
Evey时间你读写,java编译器检查



size成员并抛出异常超出范围。在内部它调用
malloc()来分配内存,并且通过一些魔术,垃圾
收集器知道数组何时超出范围并调用free()。​​

Java 2d数组是这个结构jdouble2d
结构jdouble1d * mem;
int size;
};

你会发现这有一个怪癖,并不是所有的子阵列都需要
的长度相同。

什么是C数组 - 只需要一块内存就可以了
解释为双打,字符或其他。
要在堆栈上分配你使用的符号
双buff [123];

要在堆上分配你打电话malloc()

double * buff = malloc(123 * sizeof(double));

如果你调用malloc()你必须手动调用free(),编译器
没有一个神奇的垃圾收集器为你做这件事。

最后一个C 2d阵列有点棘手。如果我们宣布

双盒[8] [10];

我们得到一块包含800个双打的内存。

盒子[3] [4] = 1;


双盒子[800];
盒子[3 * 10 + 4] = 1;

本质上是等价。
但是你也可以通过调用大量的
mallocs()构建一个二维数组。


你也可以通过调用malloc两次来构造一个二维数组。

参见C FAQ 6.16,看看第二个示例代码。
http://www.eskimo.com/~scs/C-faq/q6.16.html


另请查看以下链接:
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));

并使用相同的表示法
方框[3] [4] = 1;

至于2d数组,尽管底层的
表示形式完全不同。
Java的优点是不可能破坏
计算机的内存,也不需要单独存储数组大小。 C的
的优点是数组访问编译以直接读取/写入内存,而
则更快。




Hi all,

I need to come up with some differences between arrays in Java and C, I have
searched Google and so far all I have found is the following:

Arrays in Java are reference types with automatic allocation of memory. In
C, 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 are
more differences, but where can I find them?!

Thank you for your help.

--
Paul Morrison

解决方案

"Paul Morrison" writes:

I need to come up with some differences between arrays in Java and C, I
have searched Google and so far all I have found is the following:

Arrays in Java are reference types with automatic allocation of memory. In
C, 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
are more differences, but where can I find them?!



I would try this search target on google:
<c arrays shortcomings>



"Paul Morrison" <pa**@morrison1985.freeserve.co.uk> wrote


I need to come up with some differences between arrays in Java and C, I
have searched Google and so far all I have found is the following:

Arrays in Java are reference types with automatic allocation of memory. In
C, 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
are more differences, but where can I find them?!


A Java array is basically this

struct jdouble1d
{
double *mem;
int size;
};

Evey time you read and write, the java compiler checks against the size
member and throws an exception for out of bounds. Internally it calls
malloc() to allocate the memory, and by some magic the garbage collector
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 need be of
the same length.

What is a C array - just a chunk of memory that the program interprets 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 compiler doesn''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 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 notation
box[3][4] = 1;

as for the 2d array, despite the fact that the underlying representation is
quite different.

The advantage of Java is that it is impossible to corrupt the computer''s
memory, and you d not need to store the array size separately. The advantge
of C is that array accesses compile to direct memory read/writes, and so are
faster.



Malcolm wrote:

"Paul Morrison" <pa**@morrison1985.freeserve.co.uk> wrote


I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following:

Arrays in Java are reference types with automatic allocation of memory. In C, 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 are more differences, but where can I find them?!
A Java array is basically this

struct jdouble1d
{
double *mem;
int size;
};

Evey time you read and write, the java compiler checks against the


size member and throws an exception for out of bounds. Internally it calls malloc() to allocate the memory, and by some magic the garbage collector 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 need be of the same length.

What is a C array - just a chunk of memory that the program interprets 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 compiler doesn''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 of mallocs().

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.html

Also 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 underlying representation is quite different.

The advantage of Java is that it is impossible to corrupt the computer''s memory, and you d not need to store the array size separately. The advantge of C is that array accesses compile to direct memory read/writes, and so are faster.




这篇关于Java和C中一维数组之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆