带malloc的动态二维数组? [英] dynamic 2 D array with malloc??

查看:84
本文介绍了带malloc的动态二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


i试图创建一个大小为N x 3的动态2D数组(N将作为参数输入
)以下代码:


int ** xyz;

int i,N;


N = 30000;

xyz = malloc(3 * sizeof(int *));

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

xyz [i] = malloc(sizeof(int)* N);

程序运行得太慢,最后出现错误,当我增加

到6288:

" proj1.exe中的未处理异常:0xC0000005:访问冲突。


似乎是耗尽内存错误 。我的电脑有600Mb内存和大约

1Gb免费硬盘。


我想知道我的阵列是否太大以至于我的电脑的RAM可以''处理?

太棒了。


plz帮助??

解决方案

Vi*******@gmail.com 写道:
< blockquote class =post_quotes>
i我正在尝试使用以下代码创建一个大小为N x 3的动态2D数组(N将作为参数输入
):


int ** xyz;

int i,N;


N = 30000;

xyz = malloc(3 * sizeof(int *));

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



嗯。难道N不是3吗?


xyz [i] = malloc(sizeof(int)* N);



是的,它应该。今天BOOM。


PS关于首选商场风格的常用评论。


-

克里斯幸运人" Dollin

任何事情都可能在接下来的半小时内发生。 / Stingray /


Vi ****** *@gmail.com 写道:


i我试图创建一个大小为N x 3的动态二维数组(N将是
$ b使用以下代码将$ b作为参数输入:


int ** xyz;

int i,N;



按惯例大写是为宏保留的。


N = 30000;

xyz = malloc(3 * sizeof(int *));



因为3你修好了你可以做到


int * xyz [3];


如果你使用malloc()然后测试返回值


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

xyz [i] = malloc(sizeof(int)* N);



um。你分配了3个int * s然后你尝试索引这个内存块中的30,000个项目。


你的意思是:


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

{

xyz [i] = malloc(sizeof(int)* N);

if(xyz [0] == NULL)

handle_error();

}


请注意如何明智地使用空格可以提高可读性。


程序运行太慢,最后出现错误,当我增加

to 6288:

" proj1.exe中的未处理异常:0xC0000005:访问冲突。


似乎是用尽内存错误 。我的电脑有600Mb内存和大约

1Gb免费硬盘。


我想知道我的阵列是否太大以至于我的电脑的RAM可以''处理?

太棒了。



只是因为你的电脑有600M并不意味着操作系统会让你拥有它

all。


p = malloc(3 * N * sizeof(int *));


if(p = = NULL)

printf(不能malloc那!! !! \ n);

-

Nick Keighley

- 是的,它在实践中有效 - 但它在理论上是否有效?




" Nick基斯利" < ni ****************** @ hotmail.comwrote in message

Vi ******* @ gmail.com 写道:


>我正在尝试使用以下代码创建一个大小为N x 3的动态2D数组(N将作为参数输入):

int ** xyz;
int i,N ;



按惯例大写是为宏保留的。



N是一个例外。使用N来计算一个强烈的惯例。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


hi all,

i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

N=30000;
xyz=malloc(3*sizeof(int*));
for (i=0; i<N;i++)
xyz[i]=malloc(sizeof(int)*N);
the program ran too slow and finally error appeared when i increased
to 6288:
"Unhandled exception in proj1.exe :0xC0000005:Access violation.

it seems a "run out off memory error" . my pc has 600Mb RAM and around
1Gb free in harddisk.

Iam wondering if my array is too large that my pc''s RAM can''t handle?
it is amazing.

plz help??

解决方案

Vi*******@gmail.com wrote:

i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

N=30000;
xyz=malloc(3*sizeof(int*));
for (i=0; i<N;i++)

Um. Shouldn''t that N be 3?

xyz[i]=malloc(sizeof(int)*N);

Yes, it should. BOOM today.

PS usual remarks about preferred style of mallocation.

--
Chris "lucky man" Dollin
"Anything can happen in the next half-hour." /Stingray/


Vi*******@gmail.com wrote:

i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;

by convention uppercase is reserved for macros.

N=30000;
xyz=malloc(3*sizeof(int*));

since 3 is fixed you could do

int *xyz[3];

if you use malloc() then test the return value

for (i=0; i<N;i++)
xyz[i]=malloc(sizeof(int)*N);

um. You allocate 3 int*s then you try to index 30,000 items in
this block of memory.

You meant:

for (i = 0; i < 3; i++)
{
xyz[i] = malloc(sizeof (int) * N);
if (xyz[0] == NULL)
handle_error();
}

note how judicious use of whitespace imprves readability.

the program ran too slow and finally error appeared when i increased
to 6288:
"Unhandled exception in proj1.exe :0xC0000005:Access violation.

it seems a "run out off memory error" . my pc has 600Mb RAM and around
1Gb free in harddisk.

Iam wondering if my array is too large that my pc''s RAM can''t handle?
it is amazing.

just because your PC has 600M doesn''t mean the OS will let you have it
all.

try this

p = malloc(3 * N * sizeof(int*));

if (p == NULL)
printf ("can''t malloc that!!\n);
--
Nick Keighley
- Yes it works in practice - but does it work in theory?



"Nick Keighley" <ni******************@hotmail.comwrote in message

Vi*******@gmail.com wrote:

>i am trying to create a dynamic 2D array with size N x 3 (N will be
put in as a parameter) using the following code:

int **xyz;
int i,N;


by convention uppercase is reserved for macros.

N is an exception. There is a strong convention for using N to hold a count.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于带malloc的动态二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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