需要很多参数的函数...... + extern - 怎么样? [英] functions that take many arguments...... + extern - what about it?

查看:64
本文介绍了需要很多参数的函数...... + extern - 怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




问题:

========

我的一些输出功能正在开始采取相当多的

参数......我的意思是......我们正在谈论10-15个论点:-)


所以我想对我自己,也许这已经开始失控如果

我继续在(文件写入)

输出函数中添加额外的功能....


我心想,也许我应该开始将一些

2D指针组装成一些结构,然后将指针传递给

" container"结构,即使我可能不需要结构中的所有

指针。这应该摆脱一些功能

参数...或者说,支持20

参数完全可以接受?举个例子......采取这个函数:


void vtk_output(int nx,int ny,int nz,double * xvalues,

double * yvalues, double * zvalues,

double ** temp_array,double ** porosities,

double ** local_rhocp,double ** p_locations,

int show_p_locations ,int show_rhocp,

unsigned number_of_interior_cells,

int * output_filestep,int output_direction,

int num_integration,int show_boundary)

我正在传递一些值为0或1的整数...也许我会重写程序并使用一些小小的东西 - 使用

逻辑AND / OR用于比较?


以下为0或1:


int show_p_locations,

int show_rhocp,

int num_integration,

int show_boundar


和int output_direction为{0,1,2或3} 。


有没有一种传递说法的好方法....int control_stuff&qu OT;其中


control_stuff(第0位)= show_p_locations;

control_stuff(第1位)= show_rhocp

control_stuff(第2位)= int num_integration,

control_stuff(第3位)= int show_boundar

control_stuff(第4位...... 7)=保留供将来使用......





存在任何上限 - 关于堆栈溢出或

需要考虑的事情,如果有人说100个参数(不是我可以

想到这样做)?


另外,我从不使用任何extern我的头文件中的原型。我没有b $ b得到任何问题所以我不明白为什么extern关键字存在于

的第一位?从我读过的各个地方来看,这个extern关键字

只是说明了这个定义?放在另一个源文件中(AFAIR

- 类似的东西)?


最好的问候

Martin J?rgensen


-

------------------------------ ---------------------------------------------

Martin J?rgensen的主页 - http://www.martinjoergensen.dk

推荐答案

" Martin J?rgensen" <未********* @ spam.jay.net>在消息中写道

news:bp ************ @ news.tdc.dk ...
"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:bp************@news.tdc.dk...

<问题:
========
我的一些输出功能开始引用很多争论......
我的意思是......我们是谈论10-15个论点:-)

所以我想,如果我继续在(文件写入)中继续提供额外的功能,也许这已经开始失控了输出函数....
我想我也许应该开始将一些2D指针组装成一些结构,然后将指针传递给容器结构,即使我可能不需要
结构中的所有指针。这应该摆脱一些功能论点......或者说,与20个参数一起生活是完全可以接受的吗?作为一个例子......采取这个函数:

void vtk_output(int nx,int ny,int nz,double * xvalues,
double * yvalues,double * zvalues,
double ** temp_array,double ** porosities,
double ** local_rhocp,double ** p_locations,
int show_p_locations,int show_rhocp,
unsigned number_of_interior_cells,
int * output_filestep,int output_direction,
int num_integration,int show_boundary)

我正在传递一些值为0或1的整数...也许我应该重写该程序并使用一些有点繁琐的东西 - 使用逻辑
AND / OR进行比较?

以下是0或1:

int show_p_locations,
int show_rhocp,
int num_integration,
int show_boundar

int output_direction是{0,1,2或3}。

是有一种传递说法的好方法....int control_stuff

control_stuff(第0位)= show_p_locations;
control_stuff(第1位)= show_rhocp
control_stuff(第2位)= int num_integration,
control_stuff(第3位) = int show_boundar
control_stuff(bit 4 .... 7)=保留供将来使用...



并且存在任何上限 - 某事关于堆栈溢出或
要考虑的事情,如果一个人通过说100个参数(不是我可以想到这样做)?

此外,我从不使用任何extern ;我的头文件中的原型。我没有得到任何问题,所以我不明白为什么extern关键字存在于
的第一位?从我读过的各个地方,这个extern关键字只是
告诉定义?放在另一个源文件(AFAIR -
之类的东西)?


如果函数是递归调用的,那么堆栈溢出(自动

内存)可能会让人担心。

如果你的函数传递了100个参数,然后你写了一个函数

几乎无法调试。

几乎可以肯定,这是一个糟糕的设计。


如果你传递20个参数,那么你有一个令人难以置信的函数很难调试。

由于80%的成本软件是维护,我认为这是什么

应该真正减少我们传递给我们的函数的东西。通过使函数尽可能小而简单,我们使它们更容易调试并因此维护。


有时候问题的复杂性要求我们的函数有很多

参数。在这种情况下,我们将不得不依赖

概率测试+代码覆盖+端点(fencepost)样式测试。


所以我猜我说的是我们应该限制(尽可能多)我们传递给函数的

参数。但原因不是由于堆栈

(我们将其称为自动内存以避免出现问题)而是因为

难以确保正确性巨大的投入。


IMO-YMMV。

最好的问候
Martin J?rgensen

-
----------------------------------------------- ----------------------------
Martin J?rgensen的家 - http://www.martinjoergensen.dk



在星期一, 2006年6月12日22:52:26 +0200

Martin J?rgensen< un ********* @ spam.jay.net>写道:
On Mon, 12 Jun 2006 22:52:26 +0200
Martin J?rgensen <un*********@spam.jay.net> wrote:
我想我也许应该开始将一些2D指针组装成一些结构,然后将指针传递给
"容器"结构,即使我可能不需要结构中的所有指针。那应该摆脱一些功能性的论点......或者说,参与20个论证是完全可以接受的吗?举个例子......采取这个函数:


void vtk_output(int nx,int ny,int nz,double * xvalues,
double * yvalues, double * zvalues,
double ** temp_array,double ** porosities,
double ** local_rhocp,double ** p_locations,
int show_p_locations,int show_rhocp,
unsigned number_of_interior_cells,
int * output_filestep,int output_direction,
int num_integration,int show_boundary)

我认为你可能在一个函数中完成了太多工作,你可能需要

想要有多个输出功能。也许每种类型的

数据。这可能会使事情变得更整洁。

并且存在任何上限 - 关于堆栈溢出或者需要考虑的事情,如果一个人通过说100个参数(不是我可以想到这样做)?
I thought to myself that perhaps I should begin to assemble some of
the 2D pointers into some structures and then pass the pointer to the
"container" structure, even though I probably wont be needing all
pointers in the structure. That should get rid of a a few of function
arguments... Or would it be completely acceptable to live with say 20
arguments? As an example... Take this function:



void vtk_output(int nx, int ny, int nz, double *xvalues,
double *yvalues, double *zvalues,
double **temp_array, double **porosities,
double **local_rhocp, double **p_locations,
int show_p_locations, int show_rhocp,
unsigned number_of_interior_cells,
int *output_filestep, int output_direction,
int num_integration, int show_boundary)
I think you might have too much being done in one function, you might
want to have more than one output function. Maybe one for each kind of
data. That will probably make thing neater.
And does any upper limit exists - something about stack overflow or
something to consider, if one passes say 100 arguments (not that I
could think of doing it)?



我认为C规范规定了参数数量的最小值但

不是最大值,尽管大多数实现可能有限制。

你应该查看你正在使用的编译器的文档。

当然,根据作为

参数传递的结构的大小,你可能有内存限制问题。但是你真的不想
不应该考虑将那么多参数传递给函数。


I think C specification specifies a minimum for number of arguments but
not a maximum, although most implementations probably have a limit.
You should check the documentation for the compiler you''re using. Of
course, depending on the size of the structures being passed as
parameters you could have memory limitation issues. But you really
shouldn''t think about passing that many parameters to a function.


Martin J?rgensen写道:
Martin J?rgensen wrote:


问题:
========
我的一些输出函数开始采用相当多的
参数。我的意思是......我们正在谈论10-15个论点:-)


你提出的唯一C语言问题是最小的翻译

限制......


* 31个参数在一个函数定义中

* 31个参数在一个函数调用中


[在C99中它有127个参数和参数。]

所以我想,如果我继续投入使用,也许这已经开始失控了(文件写入)
输出功能的额外功能....

我心里想,也许我应该开始将一些2D指针组装成一些结构,然后将指针传递给
&q UOT;容器"结构,


或者执行大多数低级文件I / O的操作,使用指针参数

blocks。

尽管我可能不需要结构中的所有指针。那应该摆脱一些功能性的论点......或者说,参与20个论证是完全可以接受的吗?


这取决于。你更有可能点击实现/平台和

一般编程

问题比实际语言问题。

作为一个例子......采取这个功能:

void vtk_output(int nx,int ny,int nz,double * xvalues,
double * yvalues,double * zvalues,
double ** temp_array,double ** porosities,
double ** local_rhocp,double ** p_locations,
int show_p_locations,int show_rhocp,
unsigned number_of_interior_cells,
int * output_filestep,int output_direction,
int num_integration,int show_boundary)

我正在传递一些值为0或1的整数......也许我应该重写程序并使用一些有点蠢货的东西 - 使用
逻辑AND / OR进行比较?

以下是0或1:

int show_p_locations,
int show_rhocp,
int num_integration,
int show_boundar

和int output_direction i s {0,1,2或3}。

是否有一种传递说法的好方法....int control_stuff

control_stuff(第0位)= show_p_locations;
control_stuff(第1位)= show_rhocp
control_stuff(第2位)= int num_integration,
control_stuff(第3位) = int show_boundar
control_stuff(bit 4 .... 7)=保留供将来使用......




考虑一下在POSIX中打开的方式,但它不是很好的IMNSHO。

并且存在任何上限 - 关于堆栈溢出或
要考虑的事情,如果一个人通过说100个论点(不是我可以想到这样做)?


见上文。

此外,我从不使用任何extern我的头文件中的原型。


缺乏extern是偶然的,缺少这种

函数的原型是

罪犯。 ;-)

我还没有遇到任何问题所以我不明白为什么extern关键字存在首先?从我读过的各个地方,这个extern关键字
只是告诉我的定义?放在另一个源文件中(AFAIR
- 类似的东西)?
Hi,

Problem:
========
Some of my output functions are beginning to take pretty many
arguments... I mean.... We''re talking about 10-15 arguments :-)
The only C language issue you''ve raised is the minimum translation
limit...

* 31 parameters in one function definition
* 31 arguments in one function call

[In C99 it''s 127 parameters and arguments.]
So I thought to myself, perhaps this is beginning to get out of hands if
I continue to put in extra functionality in the (file-writing)
output-functions....

I thought to myself that perhaps I should begin to assemble some of the
2D pointers into some structures and then pass the pointer to the
"container" structure,
Or do what most low level file I/O does, use pointers to parameter
blocks.
even though I probably wont be needing all
pointers in the structure. That should get rid of a a few of function
arguments... Or would it be completely acceptable to live with say 20
arguments?
It depends. You''re more likely to hit implementation/platform and
general programming
issues than actual language issues.
As an example... Take this function:

void vtk_output(int nx, int ny, int nz, double *xvalues,
double *yvalues, double *zvalues,
double **temp_array, double **porosities,
double **local_rhocp, double **p_locations,
int show_p_locations, int show_rhocp,
unsigned number_of_interior_cells,
int *output_filestep, int output_direction,
int num_integration, int show_boundary)
I''m passing some integers that either has the value 0 or 1... Perhaps I
should rewrite the program and use some bit-fiddling stuff - using
logical AND / OR for comparing?

The following holds either 0 or 1:

int show_p_locations,
int show_rhocp,
int num_integration,
int show_boundar

And int output_direction is {0, 1, 2 or 3}.

Is there a good way of passing say.... "int control_stuff" where

control_stuff (bit 0) = show_p_locations;
control_stuff (bit 1) = show_rhocp
control_stuff (bit 2) = int num_integration,
control_stuff (bit 3) = int show_boundar
control_stuff (bit 4....7) = reserved for future use...

?
Consider the way that open works in POSIX, but it ain''t pretty IMNSHO.
And does any upper limit exists - something about stack overflow or
something to consider, if one passes say 100 arguments (not that I could
think of doing it)?
See above.
Also, I never use any "extern" prototypes in my header files.
The lack of extern is incidental, the lack of prototype for such
functions is
criminal. ;-)
I didn''t get any problems yet so I don''t see why the "extern" keyword exists
in the first place? From what I''ve read various places, this extern keyword
just tells that the definition? is placed in another source file (AFAIR
- something like that)?




它表示被delcared的标识符有外部链接。它b / b
不需要另外一个源文件中的
。 [有了标题,它并不总是很清楚

是什么

''另一个源文件''意味着因为标题本身通常是另一个

来源

档!]


它基本上是多余的,但不像''auto'那么冗余。


-

彼得



It says that the identifier being delcared has external linkage. It
needn''t
be in another source file per se. [With headers, it''s not always clear
what
''another source file'' means since the header itself is usually another
source
file!]

It''s basically redundant, but not quite as redundant as ''auto''.

--
Peter


这篇关于需要很多参数的函数...... + extern - 怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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