未定义的行为或未定义的行为?就是那个问题 [英] undefined behavior or not undefined behavior? That is the question

查看:73
本文介绍了未定义的行为或未定义的行为?就是那个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


struct foo {int example; struct bar * ptr; };


int main(无效)

{

struct foo baz;

baz。 ptr = NULL; / *未定义的行为? * /


返回0;

}

我的第二个问题与未定义的行为无关。

我按比特读取并且相当于

乘法模数2他们制作了一些符号

,看起来像是两个Z''


有人熟悉吗?


但是:


printf("%d \ nn",10& 42);


printf("%d \\ \\ n,(10 * 42)%2);


肯定不一样。


但他们使用^代表AND并说:


a * b(mod 2)= a ^ b


-

nethlek

解决方案

Mantorok Redgormor写道:

#include< stdio.h>

struct foo {int example; struct bar * ptr; };

int main(void)
结构foo baz;
baz.ptr = NULL; / *未定义的行为? * /

返回0;
}

首先定义struct bar nad #include< stdlib.h>


为什么要指定NULL的指针应该是未定义的

我的第二个问题与未定义的行为无关。
我读了那个按位AND相当于
乘法模数2他们用一些看起来像两个Zs的东西做了一些符号'


我不认为这是对的......我错过了什么吗?

但他们用^表示AND并说:

a * b(mod 2)= a ^ b


^不是AND运算符,它是XOR操作员......

它似乎也无法工作......

-
nethlek




-

#include< stdio.h>

#define p(s)printf(#s" endian" )

int main(void){int v = 1; *(char *)& v?p(Little):p(Big); return 0;}


Giannis Papadopoulos
http://dop.users.uth。 gr /

塞萨利大学

计算机&通信工程部门




2004年2月8日,Sun,Papadopoulos Giannis写道:


Mantorok Redgormor写道:

#include< stdio.h>

struct foo {int example; struct bar * ptr; };

int main(void)
结构foo baz;
baz.ptr = NULL; / *未定义的行为? * /

返回0;
}
首先定义struct bar nad #include< stdlib.h>




分别是不必要和不必要的。为了声明指向所述

结构的指针,不需要定义

''struct bar''。并且NULL在< stdio.h>中定义,在许多其他地方,

AFAIR。

为什么要指定NULL的指针应该是未定义的


谁知道/什么/正在通过Mantorok的头? [给OP
:为什么不/告诉/我们为什么你认为这样的应该是未定义或定义的b $ b而不仅仅是发布随机文本

文件,让我们通过编译器为你运行它?你希望通过这种方式学习怎么办?

我的第二个问题与未定义的行为。
我按位读取AND等同于乘法模数2。他们用一些看起来像两个Zs'的东西做了一些符号



我认为这不对...我错过了什么?




是的,但是我不知道是什么,因为,就像OP一样,你没有给b
你说的任何理由不要认为这是

是对的。"

按位AND与模数为2的整数的

字段中的乘法完全同构比较:


0& 0 = 0 0 * 0 = 0

0& 1 = 0 0 * 1 = 0

1& 0 = 0 1 * 0 = 0

1& 1 = 1 1 * 1 = 1


见?同样的事情。

但他们使用^进行AND




Do你的意思是他们 (也许是一本数学教科书?)使用

逻辑AND运算符,看起来像一个颠倒的宽V,

或指数运算符向上箭头, ;显示在计算机上

屏幕为^?

在C中,逻辑AND拼写为&&,指数拼写为pow

(在#including< math.h>之后),并且运算符拼写为^发音为

" bitwise XOR。"


[sig includes] #include< stdio.h>
#define p(s)printf(#s" endian")
int main(void){int v = 1; *(char * )& v?p(小):p(大);返回0;}




哦是的,那个代码不符合c.l.c标准。 :-) [ISO标准

C并没有真正给出关于字节序的任何有用的定义,

也不保证尝试评估*(char *)& ; $

不会在DeathStation 9000上调用未定义的行为。]


-Arthur


< blockquote> Arthur J. O''Dwyer写道:

On Sun,2004年2月8日,Papadopoulos Giannis写道:

Mantorok Redgormor写道:

#include< stdio.h>

struct foo {int example; struct bar * ptr; };

int main(void)
结构foo baz;
baz.ptr = NULL; / *未定义的行为? * /

返回0;
}
首先定义struct bar nad #include< stdlib.h>



分别是不必要的和不必要的。为了声明指向所述结构的指针,不需要定义
''struct bar''。 NULL在< stdio.h>中定义,在许多其他地方,
AFAIR。




嗯,每当我使用NULL时,我都用它malloc()调用..所以我认为

他们都在stdlib.h ...

我的第二个问题与未定义的行为无关。
我按位读取AND等于
乘法模数2。他们用一些看起来像两个Zs'的东西做了一些符号



我认为这不对...我错过了什么?



是的,但是我不知道是什么,因为,就像OP一样,你没有说明你的陈述的任何理由,你不会认为这是
是正确的。
按位AND在整数模2的
字段中与乘法完全同构。比较:

0& 0 = 0 0 * 0 = 0
0& 1 = 0 0 * 1 = 0
1& 0 = 0 1 * 0 = 0
1& 1 = 1 1 * 1 = 1

看到了?同样的事情。




也许是1和0(作为整数)......其他地方是不适用的......

[sig includes]

#include< stdio.h>
#define p(s)printf(#s" endian")
int main(void){int v = 1; *(char *)& v?p(小):p(大); return 0;}



哦是的,那段代码不符合c.l.c标准。 :-) [ISO标准
C并没有真正给出关于字节序的任何有用的定义,
也不保证尝试评估*(字符*)& v
赢了在DeathStation 9000上调用未定义的行为。]




我只是想找到机器是大端还是小端...

在谷歌0.29秒后我找到了
http ://www.treedragon.com/ged/fe/no/endianess.htm


为什么*(char *)& v会调用未定义的行为?我只是这样做


union {

int v;

char c [4]; / *假设32位机器* /

};


所以大端的v是0x00 0x00 0x00 0x01而小端是0x01

0x00 0x00 0x00。


使用c [0](或等效*(char *)& v)可以找到endianess ..仍然,

这对8位整数的机器不起作用......

-

#include< stdio.h>

#define p(s)printf(#s" endian")

int main(void){int v = 1; *(char *)& v?p(Little) :p(大);返回0;}


Giannis Papadopoulos
http://dop.users.uth.gr/

塞萨利大学

计算机&通信工程部门


#include <stdio.h>

struct foo { int example; struct bar *ptr; };

int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */

return 0;
}
My second question is not related to undefined behavior.
I read that bitwise AND is equivalent to
"Multiplication modulus two" They made some notation
with something that looked like two Zs''

Anyone familiar with that?

However:

printf("%d\n", 10 & 42);

printf("%d\n", (10 * 42) % 2);

most certainly differ.

but they used ^ for AND and said:

a * b (mod 2) = a ^ b

--
nethlek

解决方案

Mantorok Redgormor wrote:

#include <stdio.h>

struct foo { int example; struct bar *ptr; };

int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */

return 0;
}

first of all define struct bar nad #include <stdlib.h>

why should assignment of a pointer to NULL should be undefined
My second question is not related to undefined behavior.
I read that bitwise AND is equivalent to
"Multiplication modulus two" They made some notation
with something that looked like two Zs''

I don''t think this is right... Am I missing something?
but they used ^ for AND and said:

a * b (mod 2) = a ^ b
the ^ is not AND operator, it is the XOR operator...
and it doesn''t seem to work either...
--
nethlek



--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.



On Sun, 8 Feb 2004, Papadopoulos Giannis wrote:


Mantorok Redgormor wrote:

#include <stdio.h>

struct foo { int example; struct bar *ptr; };

int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */

return 0;
}

first of all define struct bar nad #include <stdlib.h>



Unnecessary and unnecessary, respectively. A definition for
''struct bar'' is not needed in order to declare a pointer to said
struct. And NULL is defined in <stdio.h>, among many other places,
AFAIR.
why should assignment of a pointer to NULL should be undefined
Who knows /what/ was running through Mantorok''s head? [To
the OP: Why not /tell/ us why you think such-and-such should be
undefined, or defined, rather than just posting a random text
file and letting us run it through a compiler for you? How do
you expect to learn anything this way?]

My second question is not related to undefined behavior.
I read that bitwise AND is equivalent to
"Multiplication modulus two" They made some notation
with something that looked like two Zs''



I don''t think this is right... Am I missing something?



Yes, but I have no idea what, because, like the OP, you didn''t
give any reasons for your statement that you "don''t think this
is right."
Bitwise AND is exactly isomorphic to multiplication in the
field of the integers modulo 2. Compare:

0 & 0 = 0 0 * 0 = 0
0 & 1 = 0 0 * 1 = 0
1 & 0 = 0 1 * 0 = 0
1 & 1 = 1 1 * 1 = 1

See? Same thing.

but they used ^ for AND



Do you mean "they" (perhaps a math textbook?) used the
logical AND operator, looking something like an upside-down wide V,
or the exponentiation operator "up-arrow," displayed on a computer
screen as "^"?
In C, logical AND is spelled &&, exponentiation is spelled "pow"
(after #including <math.h>), and the operator spelled ^ is pronounced
"bitwise XOR."

[sig includes] #include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little) : p(Big); return 0;}



Oh yeah, and that code isn''t c.l.c-compliant. :-) [ISO standard
C doesn''t really give any useful definitions regarding endianness,
nor does it guarantee that the attempted evaluation of *(char*)&v
won''t invoke undefined behavior on the DeathStation 9000.]

-Arthur


Arthur J. O''Dwyer wrote:

On Sun, 8 Feb 2004, Papadopoulos Giannis wrote:

Mantorok Redgormor wrote:

#include <stdio.h>

struct foo { int example; struct bar *ptr; };

int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */

return 0;
}

first of all define struct bar nad #include <stdlib.h>


Unnecessary and unnecessary, respectively. A definition for
''struct bar'' is not needed in order to declare a pointer to said
struct. And NULL is defined in <stdio.h>, among many other places,
AFAIR.



Hmm, whenever I used NULL, I used it with malloc() calls.. So I thought
they were both in stdlib.h...

My second question is not related to undefined behavior.
I read that bitwise AND is equivalent to
"Multiplication modulus two" They made some notation
with something that looked like two Zs''



I don''t think this is right... Am I missing something?


Yes, but I have no idea what, because, like the OP, you didn''t
give any reasons for your statement that you "don''t think this
is right."
Bitwise AND is exactly isomorphic to multiplication in the
field of the integers modulo 2. Compare:

0 & 0 = 0 0 * 0 = 0
0 & 1 = 0 0 * 1 = 0
1 & 0 = 0 1 * 0 = 0
1 & 1 = 1 1 * 1 = 1

See? Same thing.



Maybe for 1 and 0 (as integers)... elsewhere is unapplicable...
[sig includes]

#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little) : p(Big); return 0;}


Oh yeah, and that code isn''t c.l.c-compliant. :-) [ISO standard
C doesn''t really give any useful definitions regarding endianness,
nor does it guarantee that the attempted evaluation of *(char*)&v
won''t invoke undefined behavior on the DeathStation 9000.]



I am only trying to find if the machine is big endian or little endian..
After 0.29 s in google I found
http://www.treedragon.com/ged/fe/no/endianess.htm

And why should *(char*)&v invoke undefined behaviour? I am only doing this

union {
int v;
char c[4]; /* assuming 32-bit machine */
};

So v in big endian is 0x00 0x00 0x00 0x01 and in little endian is 0x01
0x00 0x00 0x00.

Using c[0] (or equivalently *(char*)&v) one can find endianess.. Still,
this doesn''t work on machines with 8-bit integers...
--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.


这篇关于未定义的行为或未定义的行为?就是那个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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