为整数数组分配空间 [英] allocating space for an array of integers

查看:57
本文介绍了为整数数组分配空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个整数矩阵[i] [j],动态分配内存

(malloc或calloc),因为i和j是在

期间定义的程序的执行。


我没有问题以静态方式执行此操作。例如,如果我想要声明一个矩阵20x10的整数然后访问

矩阵[15,8]元素:

int矩阵[20] [10];

int a;


a =矩阵[15] [8];


但是我不知道在i = 20和j = 10的情况下动态地做它。

int i,j,a;

i = 20 ; j = 10;


感谢您的帮助,

Gattaca

解决方案

Gattaca写道:

我想通过动态分配内存(malloc或calloc)来创建一个整数矩阵[i] [j]因为i和j是在执行程序期间定义。

以静态方式执行此操作我没有问题。例如,如果我想要声明一个20x10的整数矩阵然后访问
矩阵[15,8]元素:
int matrix [20] [10];
int a;

a = matrix [15] [8];

但是我不知道在i = 20和j =的情况下动态地做它10.
int i,j,a;

i = 20; j = 10;




那将是:


int **矩阵;


矩阵=(int **)calloc(i * j,sizeof int);


a =矩阵[15] [8];


C-FAQ,K& R和许多其他来源提供更多详细信息

关于这个以及与多个
维度阵列相关的其他事情的其他方法。


案例


2004年5月24日星期一12:41:57 +0200,Case写道:

那将是:


不在我的书中。

int **矩阵;

矩阵=(int **)calloc(i * j,sizeof int);


除了你不应该在这里投,并且sizeof int是一个

语法错误(你必须指的是sizeof(int)),这会分配一个连续的内存块i * j * sizeof(int)long,所有指向指向int的指针。

a = matrix [15] [8];


矩阵[15]可能存在,但它是空的。


#include< stdlib.h>

#include< stdio.h>


int main(无效)

{

int i = 20 ,j = 10;

int count;

int **矩阵;

矩阵= malloc(sizeof *矩阵* i);

if(matrix == NULL)

返回EXIT_FAILURE;

for(count = 0; count< i; count ++){

矩阵[count] = malloc(sizeof * matrix [count] * j);

if(matrix [count] == NULL)

返回EXIT_FAILURE;

}

矩阵[15] [8] = 10;

printf("%d \ nn",matrix [15] [8]);


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

free(matrix [count]);

免费(矩阵);

返回0;

}

C-FAQ,K& R和许多其他来源给出更多细节
关于这个以及与多维数组相关的其他方法的其他方法。




的确如此。


-

int main(void){int putchar(int),i = 0; unsigned long t = 500555079,n [] = { t

,159418370,88921539,286883974,80500161,0};而(n [i])putchar(*(!(t& 1

)+!(t !|| ++ⅰ)+ QUOT;#\\\
")),(T&安培;&安培; 1+(T>> =异〜-i))||(T = N [1] ^ N [ i-1]);返回0;}


Pieter Droogendijk< gi*@binky.homeunix.org>写道:
于星期一,2004年12点41分57秒+0200 5月24日,凯斯写道:
这将是:
不在我的书中。




也不是我的,但不完全是因为你提到的原因。

int **矩阵;

矩阵=(int **)calloc(i * j,sizeof int);



除了事实你不应该在这里投,并且sizeof int是一个
语法错误(你必须指的是sizeof(int)),这会分配一个连续的内存块i * j * sizeof(int) long,所有指向int的指针。




你的块大小正确,但是你在哪里获得

一个刚刚分配的内存块有一种类型为所有指向int指针的指针的印象?这是字节,这些字节是如何解释的取决于你用来访问它的指针。

在这种情况下,矩阵是一个int **。这意味着*矩阵被视为

int *,而不是int **。您可以很容易地将calloc()调用的

结果分配给unsigned char *,并将所有字节

视为unsigned chars。
<块引用类= post_quotes> A =矩阵[15] [8];

矩阵[15]可能存在,但它的NULL 。




否 - 这一切都是零,这不一定是一回事。这个

是calloc()没有malloc()那么有用的一个原因 - 你不能将
可靠地用于除整数类型之外的任何东西。对于指针和

浮点类型,所有位零都不一定是有趣的

值。

#include< stdlib.h>
#include< stdio.h>

int main(void)
{i / 20} i = 20,j = 10;
int count;
int **矩阵;
矩阵= malloc(sizeof *矩阵* i);




我认为在那里使用空格是相当混淆的。它会更清楚


矩阵= malloc(sizeof *矩阵* i);


甚至更好


矩阵= malloc(i * sizeof *矩阵);


没有更多关于你的代码的评论,除了在非玩具程序中,你

需要更加小心malloc()错误发生的事情。


Richard


I would like to create a matrix[i][j] of integers by allocating memory
dynamically (malloc or calloc) because i and j are defined during
execution of the program.

I have got not problem to do this in the static way. For instance, if I
want to declare a matrix 20x10 of integers and then to access
matrix[15,8] element:
int matrix[20][10];
int a;

a=matrix[15][8];

But I havn''t the clue to do it dynamically in case of i=20 and j=10.
int i,j,a;
i=20;j=10;

Thanks for your help,

Gattaca

解决方案

Gattaca wrote:

I would like to create a matrix[i][j] of integers by allocating memory
dynamically (malloc or calloc) because i and j are defined during
execution of the program.

I have got not problem to do this in the static way. For instance, if I
want to declare a matrix 20x10 of integers and then to access
matrix[15,8] element:
int matrix[20][10];
int a;

a=matrix[15][8];

But I havn''t the clue to do it dynamically in case of i=20 and j=10.
int i,j,a;
i=20;j=10;



That would be:

int **matrix;

matrix = (int **)calloc(i * j, sizeof int);

a = matrix[15][8];

The C-FAQ, K&R and many other sources give more details
about other ways to this and other things related to multi-
dimentional arrays.

Case


On Mon, 24 May 2004 12:41:57 +0200, Case wrote:

That would be:
Not in my book.
int **matrix;

matrix = (int **)calloc(i * j, sizeof int);
Besides the fact that you shouldn''t cast here, and that sizeof int is a
syntax error (you must mean sizeof (int)), this allocates a continuous
block of memory i*j*sizeof(int) long, all pointers to pointers to int.
a = matrix[15][8];
matrix[15] may exist, but it''s NULL.

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
int i=20, j=10;
int count;
int **matrix;
matrix = malloc (sizeof*matrix*i);
if (matrix == NULL)
return EXIT_FAILURE;
for (count=0; count < i; count++) {
matrix[count] = malloc (sizeof*matrix[count]*j);
if (matrix[count] == NULL)
return EXIT_FAILURE;
}
matrix[15][8] = 10;
printf ("%d\n", matrix[15][8]);

for (count=0; count < i; count++)
free (matrix[count]);
free (matrix);
return 0;
}
The C-FAQ, K&R and many other sources give more details
about other ways to this and other things related to multi-
dimentional arrays.



Indeed.

--
int main(void){int putchar(int),i=0;unsigned long t=500555079,n[]={t
,159418370,88921539,286883974,80500161,0};while(n[i])putchar(*(!(t&1
)+!(t||!++i)+"# \n")),(t&&1+(t>>=i-~-i))||(t=n[i]^n[i-1]);return 0;}


Pieter Droogendijk <gi*@binky.homeunix.org> wrote:

On Mon, 24 May 2004 12:41:57 +0200, Case wrote:

That would be:
Not in my book.



Nor in mine, but not entirely for the reasons you mention.

int **matrix;

matrix = (int **)calloc(i * j, sizeof int);



Besides the fact that you shouldn''t cast here, and that sizeof int is a
syntax error (you must mean sizeof (int)), this allocates a continuous
block of memory i*j*sizeof(int) long, all pointers to pointers to int.



You were right up to the size of the block, but where do you get the
impression that a just allocated block of memory has a type of "all
pointers to pointers to int"? It''s bytes, and how those bytes are
interpreted depends on the pointer you use to access it.
In this case, matrix is an int **. That means that *matrix is seen as an
int *, not as an int **. You could just as easily have assigned the
result of the calloc() call to an unsigned char *, and treated all bytes
as unsigned chars.

a = matrix[15][8];



matrix[15] may exist, but it''s NULL.



No - it''s all bits zero, which is not necessarily the same thing. This
is one reason why calloc() is less useful than malloc() - you can''t
reliably use it for anything but integer types. For pointers and
floating point types, all bits zero is not necessarily an interesting
value.
#include <stdlib.h>
#include <stdio.h>

int main (void)
{
int i=20, j=10;
int count;
int **matrix;
matrix = malloc (sizeof*matrix*i);



Rather obfuscatory use of whitespace there, I think. It''d be clearer as

matrix=malloc(sizeof *matrix * i);

or even better

matrix=malloc(i * sizeof *matrix);

No more comments on your code, except that in a non-toy program, you
need to be more careful about what happens on malloc() error.

Richard


这篇关于为整数数组分配空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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