糟糕的编程习惯? [英] bad programming practice?

查看:54
本文介绍了糟糕的编程习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作类似的东西是不好的:


//变量声明

//一些代码......


n = inputdata


int array [n];

换句话说,在retreival之后定义一个数组(通过输入数据

o经过一些计算)他的维度?

(为了避免列表)


i认为没有,但我问:)

it''s bad to make a thing like:

//declaration of variables
//some code...

n = inputdata

int array[n];
in other words, to define an array after the retreival (via input data
o after some calculations) of his dimension?
(to avoid make a list)

i think no, but i ask :)

推荐答案



fabio写道:

fabio wrote:
制作一个比如:

//变量声明
//一些代码......

n = inputdata

int array [n换句话说,
换算后(通过输入数据经过一些计算后)来定义一个数组?
(以避免列表)
it''s bad to make a thing like:

//declaration of variables
//some code...

n = inputdata

int array[n];
in other words, to define an array after the retreival (via input data
o after some calculations) of his dimension?
(to avoid make a list)




你不必列出清单。声明指向第一个元素的指针:


int * array;


然后`malloc()`尽可能多。


array = malloc(n * sizeof * array);


然后你可以使用


数组[index]


语法来获取你的元素。检查`malloc()`是否成功。



You don''t have to make a list. Declare a pointer to the first element:

int *array;

Then `malloc()` as much as you need.

array = malloc( n * sizeof *array);

You can then use

array[index]

syntax to get to your elements. Do check `malloc()` succeeded.


fabio< fa ***** @ hotmail.com>写道:
fabio <fa*****@hotmail.com> wrote:
n = inputdata
int array [n];

换句话说,在retreival之后定义一个数组(通过一些计算后的输入数据)他的维度?
n = inputdata

int array[n];

in other words, to define an array after the retreival (via input data
o after some calculations) of his dimension?




根据1999年ISO C标准,这是合法的,但不是在1989年以下/>
标准。你编译一个。可能是C89编译器和b。可能

无论如何都允许这作为扩展。


如果你想避免出于便携性的原因,你可以随时

求助于malloc()。


Richard



That is legal under the 1999 ISO C Standard, but not under the 1989
Standard. You compiler a. probably is a C89 compiler and b. possibly
allows this as an extension anyway.

If you want to avoid it for reasons of portability, you can always
resort to malloc().

Richard


Vladimir S. Oka写道:
Vladimir S. Oka wrote:
fabio写道:
制作类似的东西是不好的:

//变量声明
//一些代码......

n = inputdata
int array [n];

换句话说,在retreival之后定义一个数组(通过一些计算后的输入数据) )他的维度?
(以避免列表)
你不必列出清单。声明指向第一个元素的指针:

int *数组;

然后`malloc()`尽可能多。

array = malloc(n * sizeof * array);
it''s bad to make a thing like:

//declaration of variables
//some code...

n = inputdata

int array[n];
in other words, to define an array after the retreival (via input data
o after some calculations) of his dimension?
(to avoid make a list)
You don''t have to make a list. Declare a pointer to the first element:

int *array;

Then `malloc()` as much as you need.

array = malloc( n * sizeof *array);



不要这样做,这对于malloc()来说是一个非常糟糕的idom

数组对象

如果可能的话使用calloc()即calloc(n,sizeof(array))

自calloc()doe size check for you ,如果不能使用calloc()(哪个
会很奇怪)

手工检查。


事情是你可能会在这里溢出你的类型,

例如

size_t a,b;

char * ptr;

if((ptr = malloc(a,b))== NULL)/ * overflow * /

return(NULL);

溢出是a或b == ULONG_MAX(环绕)

- >只有一个例如,任何能够处理基本算术运算的人都会看到

这一点。


您可能会意外地分配更多资源然后您认为已经拥有

和嗯,

缓冲区溢出?

然后你可以使用

数组[索引]

语法来了解你的元素。检查`malloc()`是否成功。


Don''t do it like this, this is a very very bad idom for malloc()''ing n
objects for an array
if at all possible use calloc() i.e. calloc(n, sizeof(array))
since calloc() doe size checking for you, if can''t use calloc() (wich
would be strange)
check it by hand.

The thing is that you might overflow your type''s here,
e.g.
size_t a, b;
char *ptr;
if (( ptr = malloc(a, b)) == NULL) /* overflow */
return (NULL);
Will overflow is a or b == ULONG_MAX (wrap around)
-> Only "one" example, anyone who can handle basic artithmetic will see
the point.

You might accidently allocated more resources then you think you have
and well,
buffer overflow ?

You can then use

array[index]

syntax to get to your elements. Do check `malloc()` succeeded.






这篇关于糟糕的编程习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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