关于隐藏设计规则的信息 [英] About Information Hiding Design Rules

查看:51
本文介绍了关于隐藏设计规则的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello group,


我正在努力熟悉信息隐藏设计规则,而且我很多(b)b有很多(3)所有专家的问题。 AFAIK,一个通用模块有2个

文件:


================

module.h

================

#ifndef __MODULE_HDR_INCLUDED__

#定义__MODULE_HDR_INCLUDED__

typedef struct myadt_t {

void * pvt;

} myadt_t;


/ *定义myadt_t对象的公共接口* /


int myadt_create(myadt_t *);

int myadt_destroy(myadt_t *);

int myadt_get_status(myadt_t *,int *);


#endif


======== ========

module.c

================

#include< stdlib.h>

#include< stdio.h>

#include" module.h"


typedef struct private_myadt_t {

int x;

int y;

} private_myadt_t;


int

myadt_create(myadt_t * ptr)

{

private_myadt_t * pvt;

ptr-> pvt = malloc(sizeo f(private_myadt_t));

if(ptr-> pvt == NULL){

printf(内存无法分配\ nn) ;

返回0;

}

pvt =(private_myadt_t *)ptr-> pvt;

pvt - > x = 10;

pvt-> y = 10;

返回1;

}

int

myadt_destroy(myadt_t * ptr)

{

免费(ptr-> pvt);

返回1;

}


int

myadt_get_status(myadt_t * ptr,int * status)

{

private_myadt_t * pvt;

if(!ptr-> pvt){

printf(" with with未初始化的数据struct \ n");

返回0;

}

pvt =(private_myadt_t *)ptr-> pvt;

* status = pvt-> x + pvt-> y;

返回1;

}


,它可以在主文件中使用如下:


==============

main.c

==============

#inclu de< stdio.h>

#include" module.h"


int

main()

{

myadt_t ADT;

int status;

myadt_create(& ADT);

myadt_get_status(& ADT,& status);

myadt_destroy(& ADT);

printf(" status:%d \ n",status) ;

返回1;

}


我的问题如下:


1)这是一个很好的方法,或者是否有通用的方式

被认为是*在ANSI-C中使用信息隐藏技术的方法?


2)使用上面的代码,我无法阻止使用未初始化的数据

结构。也就是说,如果我将main()更改为


#include< stdio.h>

#include" module.h"

int

main()

{

myadt_t ADT;

int status;

myadt_get_status(& ADT,& status); / *< - 问题在这里! * /

printf(" status:%d \ n",status);

返回1;

}


我希望在屏幕上显示消息使用未初始化的数据struct\ n

,但这不会发生,因为ptr-> pvt是不是

初始化为NULL。那么,我可以在函数中使用哪个测试条件

myadt_get_status()来防止使用未初始化的数据结构?


3)在微控制器编程中,它经常使用碰巧功能

malloc()和free()不可用。有没有办法(也是一个非标准的)

使用信息隐藏设计规则而不使用这些功能?


最好的问候,


matt

Hello group,

I''m trying to become familiar with the information hiding design rules, and I
have a lot (3) of questions for all you experts. AFAIK, a generic module has 2
files:

================
module.h
================
#ifndef __MODULE_HDR_INCLUDED__
#define __MODULE_HDR_INCLUDED__

typedef struct myadt_t {
void * pvt;
} myadt_t;

/* Define the public interface to myadt_t objects */

int myadt_create(myadt_t *);
int myadt_destroy(myadt_t *);
int myadt_get_status(myadt_t *, int *);

#endif

================
module.c
================
#include <stdlib.h>
#include <stdio.h>
#include "module.h"

typedef struct private_myadt_t {
int x;
int y;
} private_myadt_t;

int
myadt_create(myadt_t * ptr)
{
private_myadt_t * pvt;
ptr->pvt = malloc (sizeof(private_myadt_t));
if (ptr->pvt == NULL) {
printf("Memory couldn''t be allocated\n");
return 0;
}
pvt = (private_myadt_t *) ptr->pvt;
pvt->x = 10;
pvt->y = 10;
return 1;
}

int
myadt_destroy(myadt_t * ptr)
{
free(ptr->pvt);
return 1;
}

int
myadt_get_status(myadt_t * ptr, int * status)
{
private_myadt_t * pvt;
if (!ptr->pvt) {
printf("Working with uninitialized data struct\n");
return 0;
}
pvt = (private_myadt_t *) ptr->pvt;
*status = pvt->x + pvt->y;
return 1;
}

and it can be used as follows in the main file:

==============
main.c
==============
#include <stdio.h>
#include "module.h"

int
main()
{
myadt_t ADT;
int status;
myadt_create(&ADT);
myadt_get_status(&ADT, &status);
myadt_destroy(&ADT);
printf("status: %d\n", status);
return 1;
}

My question are the following:

1) Is this a good way to proceed or is there way that is universally
recognized as *THE* way to use the information hiding technique in ANSI-C ?

2) With the code above, I can''t prevent the use of uninitialized data
structures. That is, if I changed the main() to

#include <stdio.h>
#include "module.h"
int
main()
{
myadt_t ADT;
int status;
myadt_get_status(&ADT, &status); /* <-- PROBLEM HERE! */
printf("status: %d\n", status);
return 1;
}

I would expect that the message "Working with uninitialized data struct\n"
be displayed on the screen, but this does not occur because ptr->pvt is not
initialized to NULL. So, which test condition could I use in function
myadt_get_status() to prevent the use of uninitialized data struct?

3) In microcontroller programming, it often happens that the functions
malloc() and free() are not available. Is there a way (also a non-standard one)
to use information hiding design rules without using these functions?

Best Regards,

matt

推荐答案

matt写道:
matt wrote:

你好小组,


我正在努力熟悉信息隐藏设计

规则,我有很多(3)个问题给你专家们。 AFAIK,一个

通用模块有2个文件:


================

module.h

================

#ifndef __MODULE_HDR_INCLUDED__

# define __MODULE_HDR_INCLUDED__
Hello group,

I''m trying to become familiar with the information hiding design
rules, and I have a lot (3) of questions for all you experts. AFAIK, a
generic module has 2 files:

================
module.h
================
#ifndef __MODULE_HDR_INCLUDED__
#define __MODULE_HDR_INCLUDED__



宏名称的选择不佳:以两个

下划线(或下划线和大写字母)开头的标识符是

保留用于任何用途,这意味着你不应该使用它们。

Poor choice of macro name: Identifiers that start with two
underscores (or with an underscore and a capital letter) are
"reserved for any use," meaning that you shouldn''t use them.


typedef struct myadt_t {

void * pvt;

} myadt_t;


/ *定义myadt_t对象的公共接口* /


int myadt_create(myadt_t *);

int myadt_destroy(myadt_t *);

int myadt_get_status(myadt_t *,int *);


#endif

[...]
typedef struct myadt_t {
void * pvt;
} myadt_t;

/* Define the public interface to myadt_t objects */

int myadt_create(myadt_t *);
int myadt_destroy(myadt_t *);
int myadt_get_status(myadt_t *, int *);

#endif
[...]


>

我的问题如下:


1)这是一个很好的方法,或者是否有通用的方式

被认为是*在ANSI-C中使用信息隐藏技术的方法?
>
My question are the following:

1) Is this a good way to proceed or is there way that is universally
recognized as *THE* way to use the information hiding technique in ANSI-C ?



有九种和六十种构建部落的方式,

而且每一种方法都是对的!


也就是说,使用包装纸结构是相当不寻常的

并且似乎没有增加太多。更常见的方法是


/ * myadt.h * /

typedef struct myadt_t / * no content * / myadt_t;

myadt_t * myadt_create(void);

int myadt_destroy(myadt_t *);

int myadt_get_status(myadt_t *,int *);


在这个公式中,myadt_t是一个不完整类型:

编译器知道它的名字,并且知道如何操作指针

它,但不知道它有多大,也不知道它在

里面。结构实际包含的内容的细节仍然隐藏在您的实现模块中:


/ * myadt.c * /

#include" myadt.h"

struct myadt_t {

int x;

int y;

} ;


....所以你的实现可以使用它们,但是客户端不能。

"There are nine and sixty ways of constructing tribal lays,
And every single one of them is right!"

That said, the use of the "wrapper" struct is fairly unusual
and doesn''t seem to add much. An approach more commonly seen is

/* myadt.h */
typedef struct myadt_t /* no content */ myadt_t;
myadt_t * myadt_create(void);
int myadt_destroy(myadt_t *);
int myadt_get_status(myadt_t *, int *);

In this formulation, myadt_t is an "incomplete type:" the
compiler knows its name and knows how to manipulate pointers to
it, but doesn''t know how big it is nor what it looks like on the
inside. The details of what the struct actually contains remain
hidden in your implementation module:

/* myadt.c */
#include "myadt.h"
struct myadt_t {
int x;
int y;
};

.... so your implementation can use them, but the clients can''t.


2)使用上面的代码,我无法阻止使用未初始化的

数据结构。也就是说,如果我将main()更改为


#include< stdio.h>

#include" module.h"

int

main()

{

myadt_t ADT;

int status;

myadt_get_status(& ADT,& status); / *< - 问题在这里! * /

printf(" status:%d \ n",status);

返回1;

}


我希望消息使用未初始化的数据

struct\\\
显示在屏幕上,但这不会发生,因为

ptr-> pvt未初始化为NULL。那么,我可以在函数myadt_get_status()中使用哪个测试条件来阻止使用未初始化的

数据结构?
2) With the code above, I can''t prevent the use of uninitialized
data structures. That is, if I changed the main() to

#include <stdio.h>
#include "module.h"
int
main()
{
myadt_t ADT;
int status;
myadt_get_status(&ADT, &status); /* <-- PROBLEM HERE! */
printf("status: %d\n", status);
return 1;
}

I would expect that the message "Working with uninitialized data
struct\n" be displayed on the screen, but this does not occur because
ptr->pvt is not initialized to NULL. So, which test condition could I
use in function myadt_get_status() to prevent the use of uninitialized
data struct?



没有办法检查一个对象并决定它是否已经或者没有被初始化。如果没有给出一个值,那么从b中获取值的尝试会产生未定义的

行为。即使你躲避U.B.通过检查对象的'/ b $ b个别字节,垃圾和在对象中可能类似于在正确初始化的对象中看到的




但是,如果你采用更多的话,这个问题大多会消失br />
通常的安排。由于myadt_create()是所有

myadt_t实例的来源,因此确保myadt_create()初始化

新实例正确解决了问题。你的客户有

无法自己创建一个myadt_t实例,所以你的客户

没有机会忘记初始化一个。

There is no way to examine an object and decide whether it
has or hasn''t been initialized. If it hasn''t been given a value,
then the attempt to pluck a value from it produces undefined
behavior. Even if you dodge the U.B. by examining the object''s
individual bytes, the "garbage" in the object might resemble what
you''d see in a properly initialized object.

However, this problem mostly goes away if you adopt the more
usual arrangement. Since myadt_create() is the source of all
myadt_t instances, making sure that myadt_create() initializes
the new instances properly solves the problem. Your client has
no way to create a myadt_t instance on its own, so your client
has no opportunity to forget to initialize one.


3)在微控制器编程中,经常发生

函数malloc()和free()不可用。有没有办法(也是一个

非标准的)使用信息隐藏设计规则而不使用

这些功能?
3) In microcontroller programming, it often happens that the
functions malloc() and free() are not available. Is there a way (also a
non-standard one) to use information hiding design rules without using
these functions?



你可以做几件事。在一个极端,你可以编写自己的malloc()等,也许是由一个静态分配的大数据库的内存支持。在另一方面,你可以

有一个静态分配的myadt_t实例数组,并且可以跟踪当前正在使用哪些以及哪些是可用的。


-
Er ********* @ sun .com


2008年7月1日下午07:16,Eric Sosman写道:
On 07/01/2008 07:16 PM, Eric Sosman wrote:

matt写道:
matt wrote:

> 2)使用上面的代码,我无法阻止使用未初始化的数据结构。也就是说,如果我将main()改为

#include< stdio.h>
#include" module.h"
int
main( )
{
myadt_t ADT;
int status;
myadt_get_status(& ADT,& status); / *< - 问题在这里! * /
printf(状态:%d \ n,状态);
返回1;
}
我希望消息使用未初始化的数据
struct\\\
显示在屏幕上,但这不会发生,因为
ptr-> pvt未初始化为NULL。那么,我可以在函数myadt_get_status()中使用哪个测试条件来防止使用未初始化的数据结构?
> 2) With the code above, I can''t prevent the use of uninitialized
data structures. That is, if I changed the main() to

#include <stdio.h>
#include "module.h"
int
main()
{
myadt_t ADT;
int status;
myadt_get_status(&ADT, &status); /* <-- PROBLEM HERE! */
printf("status: %d\n", status);
return 1;
}

I would expect that the message "Working with uninitialized data
struct\n" be displayed on the screen, but this does not occur because
ptr->pvt is not initialized to NULL. So, which test condition could I
use in function myadt_get_status() to prevent the use of uninitialized
data struct?



没有办法检查一个对象并决定它是否已经或者没有被初始化。


There is no way to examine an object and decide whether it
has or hasn''t been initialized.



但是像valgrind这样的程序可以识别你是否使用未初始化的内存来计算
。事实上,如果我用valgrind运行示例程序我将获得


matt @ isaac:〜/ work / c / information-hiding

However there are programs like valgrind that can recognize whether you are
using uninitialised memory. Infact, if I run the example program with valgrind I
obtain

matt@isaac:~/work/c/information-hiding


valgrind ./exe


[...]


== 4581 ==

== 4581 =使用大小为4的未初始化值

== 4581 ==在0x804849E:myadt_get_status(在

/ home / matt / work / c /信息隐藏/ exe)

== 4581 == by 0x80484DE:main(在/ home / matt / work / c / information-hiding / exe)

== 4581 ==

== 4581 =使用大小为4的未初始化值

== 4581 ==在0x80484A3:myadt_get_status(在

中) / home / matt / work / c / information-hiding / exe)

== 4581 == by 0x80484DE:main(在/ home / matt / work / c / information-hiding / exe)

状态:953015467

[...]
valgrind ./exe

[...]

==4581==
==4581== Use of uninitialised value of size 4
==4581== at 0x804849E: myadt_get_status (in
/home/matt/work/c/information-hiding/exe)
==4581== by 0x80484DE: main (in /home/matt/work/c/information-hiding/exe)
==4581==
==4581== Use of uninitialised value of size 4
==4581== at 0x80484A3: myadt_get_status (in
/home/matt/work/c/information-hiding/exe)
==4581== by 0x80484DE: main (in /home/matt/work/c/information-hiding/exe)
status: 953015467

[...]

但是,这个问题大多会消失如果你采用更多

通常的安排。由于myadt_create()是所有

myadt_t实例的来源,因此确保myadt_create()初始化

新实例正确解决了问题。你的客户有

无法自己创建一个myadt_t实例,所以你的客户

没有机会忘记初始化一个。
However, this problem mostly goes away if you adopt the more
usual arrangement. Since myadt_create() is the source of all
myadt_t instances, making sure that myadt_create() initializes
the new instances properly solves the problem. Your client has
no way to create a myadt_t instance on its own, so your client
has no opportunity to forget to initialize one.



你对正确的意思是什么?采用这个解决方案应该足够了


myadt_t *

myadt_create(无效)

{

myadt_t * ptr;

ptr = malloc(sizeof(myadt_t));

if(ptr == NULL){

printf(" Memory couldn '不得分配\ nn;);

返回0;

}

ptr-> x = 10;

ptr-> y = 10;

返回ptr;

}


.... isn 是吗?


感谢您的时间。


matt

What do you mean with "properly"? Adopting this solution should suffice

myadt_t *
myadt_create(void)
{
myadt_t * ptr;
ptr = malloc (sizeof(myadt_t));
if (ptr == NULL) {
printf("Memory couldn''t be allocated\n");
return 0;
}
ptr->x = 10;
ptr->y = 10;
return ptr;
}

.... isn''t it?

Thanks for your time.

matt


这篇关于关于隐藏设计规则的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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