C软件架构 [英] C software achitecture

查看:63
本文介绍了C软件架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我是C编程的新手(我来自Java世界没人是

完美)我有一个关于C软件可能性的问题

架构。


我看到C函数常常使用返回值来给出

关于函数执行的信息(0 = exec OK,1 = exec

错误)。我想知道如何在缓冲区中使用最干净的代码来输出命令

结果(如lsLinux命令)。下面的代码显示了函数头的可能方式:


//函数执行命令(命令)并将结果放入

buf

int exec(char * command,char ** buf){

...

}


这里,程序员(这个函数的用户)需要提供一个

char **来获取一个char指针。所以,他需要创建一个指针

并将其交给函数(如下):


char * myBufPointer;

exec(" ls -l",& myBufPointer);


exec函数将分配一个内存缓冲区(malloc)和

程序员以后需要释放它...我不知道是不是这是一个很好的主意在一个函数中分配内存并让

程序员以后释放它?


另一个问题来自于程序员为该函数创建

缓冲区的方式。如果他要做一个静态缓冲区(char

myBuf [200]),该函数将不必分配内存......如果200

char不够用于结果(buf)我该怎么办?如果缓冲区是静态的(如果指针是指向内存区域的指针是否为
),并且内存区域足够大,那么我应该检查函数吗? />
??如果它是静态缓冲区,我该如何扩展它?我应该创建一个

另一个吗?


还有另一种方式(最干净的)来创建我的函数exec ??


非常感谢你的帮助,希望能尽快给你看...


Jo?lW

hi all,

I''m a newbie in C programming (i''m coming from Java world "nobody is
perfect") and i have a question about possibilities in C software
architecture.

I saw that C functions where often using the return value to give
informations about execution of the function (0 = exec OK, 1 = exec
mistake). I''m wondering how i can do the cleanest code to put a command
result (like the "ls" Linux command) in a buffer. The code below show a
possible way of the function header:

//function executing a command (command) and putting the result in
buf
int exec(char* command, char** buf){
...
}

Here, the programmer (the user of this function) will need to give a
char** to get a char pointer back. So, he will need to create a pointer
and give it to the function (like this):

char* myBufPointer;
exec("ls -l", &myBufPointer);

The exec function will allocate a memory buffer (malloc) and the
programmer will need to free it afterwards... I don''t know if it''s a
really good idea to allocate memory in a function and to let the
programmer free it later ??

The other problem is coming from the way the programmer is creating the
buffer for the function. If he gonna do a static buffer (char
myBuf[200]) the function will not have to allocate memory... If the 200
char are not enough for the result (buf) how could i do ? Should i
check in the function if the buffer is static (if the pointer is
pointing to a memory area or not) and if the memory area is big enough
?? If it''s a static buffer how can i extend it ? should i create an
other one ?

Is there an other way (a cleanest one) to create my function exec ??

Thanks a lot for your help and hope to read you soon...

Jo?l.W

推荐答案

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

大家好,

我是C编程的新手(我来自Java世界) 没有人是完美的,而且我对C软件架构中的可能性有疑问。

我看到C函数经常使用返回值来给出错误)。我想知道我怎么能用最干净的代码将命令结果(如lsLinux命令)放在缓冲区中。下面的代码显示了函数头的可能方式:

//函数执行命令(命令)并将结果放入/或者执行内容中char * command,char ** buf){
...
}
这里,程序员(这个函数的用户)需要给一个
char **得到一个char指针。因此,他需要创建一个指针
并将其交给函数(如下所示):

char * myBufPointer;
exec(" ls -l",& ; myBufPointer);

exec函数将分配一个内存缓冲区(malloc),然后程序员需要释放它...我不知道它是不是
真的好主意在函数中分配内存并让程序员稍后释放它吗?

另一个问题来自
程序员的方式为函数创建
缓冲区。如果他要做一个静态缓冲区(char
myBuf [200]),该函数将不必分配内存...
如果200
char不足以获得结果(buf)我怎么办?如果缓冲区是静态的(如果指针指向或不指向存储区),并且内存区域足够大,我应该检查函数吗?如果它是静态缓冲区,我该如何扩展它?我应该创建另一个吗?

有没有其他方式(最干净的)来创建我的函数exec ??

hi all,

I''m a newbie in C programming (i''m coming from Java world "nobody is
perfect") and i have a question about possibilities in C software
architecture.

I saw that C functions where often using the return value to give
informations about execution of the function (0 = exec OK, 1 = exec
mistake). I''m wondering how i can do the cleanest code to put a command
result (like the "ls" Linux command) in a buffer. The code below show a
possible way of the function header:

//function executing a command (command) and putting the result in
buf
int exec(char* command, char** buf){
...
}

Here, the programmer (the user of this function) will need to give a
char** to get a char pointer back. So, he will need to create a pointer
and give it to the function (like this):

char* myBufPointer;
exec("ls -l", &myBufPointer);

The exec function will allocate a memory buffer (malloc) and the
programmer will need to free it afterwards... I don''t know if it''s a
really good idea to allocate memory in a function and to let the
programmer free it later ??

The other problem is coming from the way the
programmer is creating the
buffer for the function. If he gonna do a static buffer (char
myBuf[200]) the function will not have to allocate memory...
If the 200
char are not enough for the result (buf) how could i do ? Should i
check in the function if the buffer is static (if the pointer is
pointing to a memory area or not) and if the memory area is big enough
?? If it''s a static buffer how can i extend it ? should i create an
other one ?

Is there an other way (a cleanest one) to create my function exec ??




int exec(char * command,char * buf);


将buf设为char *而不是char **,

并让调用数组的函数句柄分配,

以及可能需要完成的任何释放。

这样,buf指向的内存,

可以是自动的,静态的或分配的。


char myBufPointer [200];

exec(" ls -l",myBufPointer);





char * myBufPointer = malloc(200);

if(myBufPointer!= NULL){

exec(" ls -l",myBufPointer);

}

免费(myBufPointer);


-

pete



int exec(char* command, char* buf);

Make buf a char* instead of a char**,
and let the calling function handle allocation for the array,
as well as any freeing that may need to be done.
That way, the memory that buf points to,
could be either automatic, static or allocated.

char myBufPointer[200];
exec("ls -l", myBufPointer);

or

char *myBufPointer = malloc(200);
if (myBufPointer != NULL) {
exec("ls -l", myBufPointer);
}
free(myBufPointer);

--
pete


>我看到C函数经常使用返回值来给出
>I saw that C functions where often using the return value to give
有关函数执行的信息(0 =执行正常,1 =执行错误)。我想知道我怎么能用最干净的代码将命令结果(如lsLinux命令)放在缓冲区中。下面的代码显示了函数头的可能方式:

//函数执行命令(命令)并将结果放入/或者执行内容中char * command,char ** buf){
...
}
这里,程序员(这个函数的用户)需要给一个
char **得到一个char指针。因此,他需要创建一个指针
并将其交给函数(如下所示):

char * myBufPointer;
exec(" ls -l",& ; myBufPointer);

exec函数将分配一个内存缓冲区(malloc),然后程序员需要释放它...我不知道它是不是
真的好主意在函数中分配内存并让程序员以后释放它吗?


有些人不喜欢它,但这是很常见的事情。有时

你有一对函数:一个分配一些东西,一个

解除分配它。在C ++中,这被称为构造函数。通常函数(例如fopen()和fclose())更多地了解

如何分配比调用者多的内存和/或析构函数。分配的内容可能不是
是一个简单的内存块:它可能是一个完整的链表,或者是一个结构

,带有指向某些缓冲区的指针,或者其他什么。构造函数和

析构函数知道细节,调用者不应该。

另一个问题来自于程序员创建
缓冲区的方式功能。


如果你传入一个char **所以函数可以将一个指针

返回到分配的缓冲区,它取决于函数分配它。

如果他要做一个静态缓冲区(char
myBuf [200]),该函数将不必分配内存......如果200
char不是足够的结果(buf)我该怎么办?


如果调用者分配缓冲区,调用者也需要传入缓冲区的

长度(并且正确)。被调用的函数

不能检查这个。

如果缓冲区是静态的(如果指针是


您无法检查指针是否指向静态缓冲区。

甚至不要考虑尝试。

指向内存区域与否如果内存区域足够大
??
如果它是静态缓冲区我怎么能扩展它?


你不要,所以不要考虑尝试。

我应该创建另一个吗?


我的建议是让被调用的exec()函数执行分配,

,因为它知道它从运行的命令中获取了多少数据。分配

合理猜测。如果你需要更多realloc()缓冲区更大。

必要时重复。

还有另一种方法(最干净的)来创建我的函数exec ??
informations about execution of the function (0 = exec OK, 1 = exec
mistake). I''m wondering how i can do the cleanest code to put a command
result (like the "ls" Linux command) in a buffer. The code below show a
possible way of the function header:

//function executing a command (command) and putting the result in
buf
int exec(char* command, char** buf){
...
}

Here, the programmer (the user of this function) will need to give a
char** to get a char pointer back. So, he will need to create a pointer
and give it to the function (like this):

char* myBufPointer;
exec("ls -l", &myBufPointer);

The exec function will allocate a memory buffer (malloc) and the
programmer will need to free it afterwards... I don''t know if it''s a
really good idea to allocate memory in a function and to let the
programmer free it later ??
Some people don''t like it, but it''s a very common thing. Sometimes
you have a pair of functions: one allocates something, and one
deallocates it. In C++ this is called "constructors" and "destructors".
Often the functions (e.g. fopen() and fclose()) know more about how
much memory to allocate than the caller. What is allocated might not
be a simple memory block: it might be a whole linked list, or a structure
with pointers to some buffers, or whatever. The constructors and
destructors know that detail, the caller shouldn''t.
The other problem is coming from the way the programmer is creating the
buffer for the function.
If you are passing in a char** so the function can return a pointer
to the allocated buffer, it''s up to the function to allocate it.
If he gonna do a static buffer (char
myBuf[200]) the function will not have to allocate memory... If the 200
char are not enough for the result (buf) how could i do ?
If the caller allocates the buffer, the caller needs to pass in the
length of the buffer also (and CORRECTLY). The called function
CANNOT check this.
Should i
check in the function if the buffer is static (if the pointer is
You cannot check if a pointer points at a static buffer.
Don''t even think about trying.
pointing to a memory area or not) and if the memory area is big enough
??
If it''s a static buffer how can i extend it ?
You DON''T, so don''t even think about trying.
should i create an
other one ?
My suggestion is to let the called exec() function do the allocating,
as it knows how much data it got back from the command it ran. Allocate
a reasonable guess. If you need more realloc() the buffer bigger.
Repeat if necessary.
Is there an other way (a cleanest one) to create my function exec ??




注意exec()是一个常见的系统函数,而你的exec()

函数很可能调用函数(例如popen())那称之为

共同的系统功能。这不应该是重要的,但是如果

库被破坏它可能会。


Gordon L. Burditt



Beware that exec() is a common system function, and your exec()
function is likely to call functions (e.g. popen()) that call that
common system function. This isn''t supposed to matter, but if the
library is broken it might.

Gordon L. Burditt


感谢您的建议,


但是您建议的方式出现的问题来自于尺寸

命令执行结果。如果结果大于

缓冲区,我将丢失信息......正如Gordon Burditt所说,我应该给
给函数缓冲区大小..?!如果缓冲区大小不够大,我应该返回错误

值(返回1;)吗?

Thanks for your suggestion,

But the problem with the way you suggest to do it, comes from the size
of the command execution result. If the result is bigger than the
buffer, i will lost informations... As Gordon Burditt said, i sould
give the buffer size to the function ..?! And should i return an error
value (return 1;) if the buffer size isn''t big enough ??


这篇关于C软件架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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