这是可接受的(即合规的)代码吗? [英] Is this acceptable (i.e., compliant) code?

查看:65
本文介绍了这是可接受的(即合规的)代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否在以下程序中调用任何未定义的行为?


/ *打印出存储在变量中的位模式的函数,无论

类型* /


#include< limits.h> / * CHAR_BIT * /

#include< stdio.h> / * putc()* /


void printbits(unsigned char * p,int bytes);


int main(void)

{

double x;

int k;


k = 64;

printbits((unsigned char *)& k,sizeof(int));

putc(''\ n'',stdout);


k = -1;

printbits((unsigned char *)& k,sizeof(int));

putc(''\ n'',stdout );


x = 3.1415;

printbits((unsigned char *)& x,sizeof(double));

putc(''\ n'',stdout);


x = -1e300;

printbits((unsigned char *)& x,sizeof( double));

putc(''\ n'',stdout);


x = 1e300;

printbits( (unsigned char *)& x,sizeof(double));

putc(''\ n'',stdout);


return( 0);

}


void printbits(unsigned char * p,int bytes)

{

int byte;

unsig ned char mask;


for(byte = 0; byte< bytes; putc('''',stdout),byte ++)

for(mask = 1<<(CHAR_BIT-1); mask; mask>> = 1)

putc((mask& p [byte])?''''':''0'',stdout);


返回;

}

Am I invoking any undefined behavior in the following program?

/* Function to print out the bit pattern stored in a variable regardless of
type */

#include <limits.h> /* CHAR_BIT */
#include <stdio.h> /* putc() */

void printbits(unsigned char *p, int bytes);

int main(void)
{
double x;
int k;

k = 64;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

k = -1;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

x = 3.1415;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = -1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = 1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

return(0);
}

void printbits(unsigned char *p, int bytes)
{
int byte;
unsigned char mask;

for(byte = 0; byte < bytes; putc('' '', stdout), byte++)
for(mask = 1 << (CHAR_BIT-1); mask; mask >>= 1)
putc((mask & p[byte])? ''1'': ''0'', stdout);

return;
}

推荐答案



" William L. Bahn" <无线***** @ toomuchspam.net>写了

"William L. Bahn" <wi*****@toomuchspam.net> wrote

我是否在以下程序中调用任何未定义的行为?


看不出有什么问题。你避免使用的主要问题是

普通字符可以有陷阱表示,所以你需要unsigned char来表示

任意位。

请注意,程序的输出将根据平台而有所不同。 ints

可能并不总是具有相同的大小和字节序。理论上可以双倍的b
也有不同的表示形式,但几乎所有人都已经确定了

的IEEE标准。


公约不是使用圆括号作为返回表达式BTW,但是这个

是唯一的狡辩。
/ *打印出存储在变量中的位模式的函数,无论
类型是什么* /

#include< limits.h> / * CHAR_BIT * /
#include< stdio.h> / * putc()* /

void printbits(unsigned char * p,int bytes);

int main(void)
{
double x;
int k;

k = 64;
printbits((unsigned char *)& k,sizeof(int));
putc('' \ n'',stdout);

k = -1;
printbits((unsigned char *)& k,sizeof(int));
putc(' '\ n'',stdout);

x = 3.1415;
printbits((unsigned char *)& x,sizeof(double));
putc(' '\ n'',stdout);

x = -1e300;
printbits((unsigned char *)& x,sizeof(double));
putc( ''\ n'',stdout);

x = 1e300;
printbits((unsigned char *)& x,sizeof(double));
putc( ''\ n'',stdout);

返回(0);


void printbits(unsigned char * p,int bytes)
{
int byte;
unsigned char mask;

for(byte = 0; byte< byte s; putc(''',stdout),byte ++)
for(mask = 1<< (CHAR_BIT-1);面具; mask>> = 1)
putc((mask& p [byte])?''1'':''0'',stdout);

返回;
}

Am I invoking any undefined behavior in the following program?

Can''t see anything wrong. The main problem, which you have avoided, is that
plain chars can have trap representations, so you need unsigned char for
arbitrary bits.
Note that the output of the program will vary acording to the platform. ints
may not always have the same size and endianness. Theoretically double can
also have different representations, but pretty much everyone has settled on
the IEEE standard.

Convention is not to use parentheses for a return expression, BTW, but this
is the only quibble.
/* Function to print out the bit pattern stored in a variable regardless of type */

#include <limits.h> /* CHAR_BIT */
#include <stdio.h> /* putc() */

void printbits(unsigned char *p, int bytes);

int main(void)
{
double x;
int k;

k = 64;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

k = -1;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

x = 3.1415;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = -1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = 1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

return(0);
}

void printbits(unsigned char *p, int bytes)
{
int byte;
unsigned char mask;

for(byte = 0; byte < bytes; putc('' '', stdout), byte++)
for(mask = 1 << (CHAR_BIT-1); mask; mask >>= 1)
putc((mask & p[byte])? ''1'': ''0'', stdout);

return;
}



William L. Bahn写道:

< snip>


虽然代码看起来不错,但我认为风格可以刷一点。


建议:


printbits可能是静态的。将其向上移动到文件中,以便

定义作为原型。


2.不要将代码堵塞到循环结构中。


3.使用void指针参数而不是将每个参数转换为

printbits为unsigned char。


4.使用size_t表示对象的大小。 size_t是sizeof运算符的结果类型

。 size_t在stddef.h中定义。


5.将sizeof应用于变量本身,而不是应用于

变量的类型。


6.''return(0);''很难看,请使用''return 0;''代替。

Mark F. Haigh



#include< stdio.h>

#include< stddef.h>

#include< limits.h>


static void printbits(void * mem,size_t size)

{

size_t i;

unsigned char mask;

char * p = mem;


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

for(mask = 1<<<(CHAR_BIT - 1); mask; mask>> = 1)

putc((mask& p [i])?'' 1'':''0'',stdout);


putc('''',stdout);

}

putc(''\ n'',stdout);

}


int main(无效)

{

double x;

int k;


k = 64;

printbits(& k ,sizeof k );


k = -1;

printbits(& k,sizeof k);


x = 3.1415 ;

printbits(& x,sizeof x);


x = -1e300;

printbits(& k,sizeof x);


x = 1e300;

printbits(& k,sizeof x);


返回0 ;

}
William L. Bahn wrote:
<snip>

Although the code looks ok, I think the style can be brushed up a bit.

Suggestions:

1. printbits may be able to be static. Move it up in the file so the
definition serves as the prototype.

2. Don''t unnecessarily jam code into loop constructs.

3. Use a void pointer argument instead of casting every argument to
printbits to unsigned char.

4. Use a size_t to represent sizes of objects. size_t is the type of
the result of the sizeof operator. size_t is defined in stddef.h.

5. Apply sizeof to the variable itself, rather than to the type of the
variable.

6. ''return(0);'' is ugly, use ''return 0;'' instead.
Mark F. Haigh


#include <stdio.h>
#include <stddef.h>
#include <limits.h>

static void printbits(void *mem, size_t size)
{
size_t i;
unsigned char mask;
char *p = mem;

for(i = 0; i < size; i++) {
for(mask = 1 << (CHAR_BIT - 1); mask; mask >>= 1)
putc((mask & p[i]) ? ''1'' : ''0'', stdout);

putc('' '', stdout);
}
putc(''\n'', stdout);
}

int main(void)
{
double x;
int k;

k = 64;
printbits(&k, sizeof k);

k = -1;
printbits(&k, sizeof k);

x = 3.1415;
printbits(&x, sizeof x);

x = -1e300;
printbits(&k, sizeof x);

x = 1e300;
printbits(&k, sizeof x);

return 0;
}


2004年8月7日星期六,William L. Bahn写道:
On Sat, 7 Aug 2004, William L. Bahn wrote:
我是否在以下程序中调用任何未定义的行为?

/ *打印出存储在变量中的位模式的函数,无论
类型是什么* /

#包括< limits.h> / * CHAR_BIT * /
#include< stdio.h> / * putc()* /

void printbits(unsigned char * p,int bytes);


size_t bytes?

int main(无效)
{
双x;
int k;

k = 64;
printbits((unsigned char *)& k,sizeof(int));
putc(''\ n'',stdout);

k = -1;
printbits((unsigned char *)& k,sizeof(int));
putc(''\ n'',stdout);

x = 3.1415;
printbits((unsigned char *)& x,sizeof(double));
putc(''\ n'',stdout);

x = -1e300;
printbits((unsigned char *)& x,sizeof(double));
putc(''\ n'',stdout);

x = 1e300;
printbits((unsigned char *)& x,sizeof(double));
putc(''\ n'',stdout);

返回(0);
}
void printbits(unsigned char * p,int bytes)


void printbits (unsigned char * p,size_t bytes)?

{
int byte;


size_t byte?

unsigned char mask;

for(byte = 0; byte< bytes; putc('' '',stdout),byte ++)
for(mask = 1<<(CHAR_BIT-1); mask; mask>> = 1)


( unsigned char)1<< CHAR_BIT - 1?

putc((mask& p [byte])?''1'':''0'',stdout);

返回;
}
Am I invoking any undefined behavior in the following program?

/* Function to print out the bit pattern stored in a variable regardless of
type */

#include <limits.h> /* CHAR_BIT */
#include <stdio.h> /* putc() */

void printbits(unsigned char *p, int bytes);
size_t bytes?
int main(void)
{
double x;
int k;

k = 64;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

k = -1;
printbits((unsigned char *) &k, sizeof(int));
putc(''\n'', stdout);

x = 3.1415;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = -1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

x = 1e300;
printbits((unsigned char *) &x, sizeof(double));
putc(''\n'', stdout);

return(0);
}

void printbits(unsigned char *p, int bytes)
void printbits(unsigned char *p, size_t bytes)?
{
int byte;
size_t byte?
unsigned char mask;

for(byte = 0; byte < bytes; putc('' '', stdout), byte++)
for(mask = 1 << (CHAR_BIT-1); mask; mask >>= 1)
(unsigned char) 1 << CHAR_BIT - 1?
putc((mask & p[byte])? ''1'': ''0'', stdout);

return;
}




德成


PS小学生可能会争辩说,对于托管的

实施,演员阵容是不必要的......



Tak-Shing

P.S. Pedants might argue that the cast is unnecessary for hosted
implementations...


这篇关于这是可接受的(即合规的)代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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