令人不安的阵营(我认为)问题。 [英] Troubling Alignment (I think) issue.

查看:60
本文介绍了令人不安的阵营(我认为)问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我希望有人在这里可以告诉我一些

对齐问题我有。希望我能通过下面的代码解释这个最好的

。下面的ACSign和MyACSign函数被编译成一个库,其中执行这些

函数的代码被编译成一个单独的库。打电话到MyACSign

工作正常,直接ACSign的电话没有,我相信这是对齐问题,但我不能指出如何。这< i>应该< / ihave

非常基本。任何帮助当然非常感谢,


- Kevin

功能代码:

********* ***************************************** ********* *********

双ACSign(双倍){

if(a 0.0)

return(1.0);

else if(a< 0.0)

return(-1.0);

}


double * MyACSign(double * a){

if(* a 0.0)

* a = 1.0;

else if(* a < 0.0)

* a = -1.0;

返回a;

}


执行代码:

*************************************** *********** ******************

double test = 0;

double test1 = 0;

test = 0.707107;

test1 = test * -1;


printf(" Locations: %u,%u \\\\ n",& test,& test1);


printf(" 0Double:%f,%f",test,test1 );

printf(" 1Double:%f,%f",ACSign(test),ACSign(test1));

printf(" 2Double:%i,%i",(char)ACSign(test),(char)ACSign(test1));


printf(" 3Double:%f ,%f",0.707107,-0.707107);

printf(" 4Double:%f,%f",ACSign(0.707107),ACSign(-0.707107));

printf(" 5Double:%i,%i",(char)ACSign(0.707107),

(char)ACSign(-0.707107));


printf(" 6Double:%f,%f",test,test1);

printf(" 7Double:%f,%f",*(double *)MyACSign(& ;测试),

*(double *)MyACSign(& test1));

printf(" 8Double:%i,%i",(char)* (double *)MyACSign(& test),

(char)*(double *)MyACSign(& test1));

******* ******************************************* ******* ***********

我得到的输出是:

地点:4263558272,4263558264

0Double:0.707107, -0.707107

1Double:0.707107,-0.707107

2Double:-98,-98

3Double:0.707107,-0.707107

4Doub le:0.707107,0.000000

5Double:-98,-98

6Double:0.707107,-0.707107

7Double:1.000000,-1.000000

8Double:1,-1

解决方案

Kevin< ke ******** @ gmail.com写的:


嘿,我希望有人在这里可能能够解决一些

对齐问题我''我有。



我怀疑你的问题与对齐有什么关系。

相反,我怀疑你没有正确宣布

函数在头文件中,例如

double ACSign(double);

double * MyACSign(double *);

原型(括号中的位)是可选的,但是它们可以帮助编译器检查调用。

-

char a [] = " \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6 },* p

= b,i = 24; for(; p + =!* p; * p / = 4)switch(0 [p]& 3)case 0:{return 0 ; for(p - ; i - ; i - )case +

2:{i ++; if(i)break; else default:continue; if(0)case 1:putchar(a [i& 15]); break;}}}


2007年11月7日星期三上午1:57 Kevin< ke ******** @ gmail .comwrote在

文章< 11 ********************** @ i38g2000prf.googlegroups .com>:
< blockq uote class =post_quotes>
嘿,我希望有人在这里可能能够让我知道一些

对齐问题。希望我能通过下面的代码解释这个最好的

。下面的ACSign和MyACSign函数被编译成一个库,其中执行这些

函数的代码被编译成一个单独的库。打电话到MyACSign

工作正常,直接ACSign的电话没有,我相信这是对齐问题,但我不能指出如何。这< i>应该< / ihave

非常基本。任何帮助当然非常感谢,


- Kevin


功能代码:

***** ********************************************* ***** *************

双ACSign(双倍){

如果(a 0.0)

返回(1.0);



括号和小数点都是不必要的。


else if(a< 0.0)

return(-1.0);



同样在这里。


}


double * MyACSign(double * a){

if(* a 0.0)

* a = 1.0;

else if(* a< 0.0 )

* a = -1.0;

返回a;

}


执行代码:

****************************************** ******** ******************

double test = 0;

double test1 = 0;

test = 0.707107;

test1 = test * -1;


printf("地点:%u, %u \\\\ n",& test,& test1);



编号要打印指针,请使用''%p''说明符并将相应的

参数转换为void *。此外,''\ r''不仅没有必要,而且可能会导致问题。 C标准库将''\ n''转换为系统用作行尾的



printf (0Double:%f,%f,test,test1);

printf(" 1Double:%f,%f",ACSign(test),ACSign(test1));

printf(" 2Double:%i,%i",(char)ACSign(test),(char)ACSign(test1));



将一个double值转换为char然后在告诉它看起来后将它们正确传递给printf的重点是什么?对于一个int?使用

''%c''格式打印字符。


printf(" 3Double:%f,%f" ,0.707107,-0.707107);

printf(" 4Double:%f,%f",ACSign(0.707107),ACSign(-0.707107));

printf( 5Double:%i,%i,(char)ACSign(0.707107),



再次出现相同的问题。除了你有望看到有损后的什么

cast?


(char)ACSign(-0.707107));


printf(" ; 6Double:%f,%f",test,test1);

printf(" 7Double:%f,%f",*(double *)MyACSign(& test),



编号使用*(MyACSign(& test))。


*(double *)MyACSign( &安培; TEST1));



同样。


printf(" 8Double:%i,%i",(char) *(double *)MyACSign(& test),

(char)*(double *)MyACSign(& test1));



再次将这个神秘的强制转换为char值。


********* ***************************************** ********* *********

我得到的输出是:

地点:4263558272,4263558264

0Double:0.707107,-0.707107

1Double:0.707107,-0.707107

2Double:-98,-98

3Double:0.707107,-0.707107

4Double:0.707107,0.000000

5Double:-98,-98

6Double:0.707107,-0.707107

7Double:1.000000,-1.000000

8Double:1,-1



由于您在多个地方调用了未定义的行为,您可以获得

any一种输出。我建议你修复明显的错误并重新编译

并重新运行。


Bingo,我偶然发现同样的答案时间 - 谢谢!我在编译代码中错过了隐含的声明警告,我的

合作伙伴错过了标题中的这一项功能。


谢谢再次!


- Kevin

11月6日下午3:37,Ben Pfaff< b ... @ cs.stanford.eduwrote:


Kevin< kevinwo ... @ gmail.comwrites:


嘿,我希望有人在这里或许可以让我知道我正在讨论的一些问题。



我怀疑你的问题与对齐有什么关系。

相反,我怀疑你没有正确宣布

函数在头文件中,例如

double ACSign(double);

double * MyACSign(double *);

原型(括号中的位)是可选的,但是它们可以帮助编译器检查调用。

-

char a [] = " \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6 } *,* p

= b,i = 24; for(; p + =!* p; * p / = 4)switch(0 [p]& 3)case 0:{return 0; for(p - ; i - ; i - )case +

2:{i ++; if(i)break; else default:continue; if(0)case 1:putchar( a [i& 15]); break;}}}



Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I''m having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can''t pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,

- Kevin
Function Code:
************************************************** ******************
double ACSign ( double a) {
if (a 0.0)
return(1.0);
else if (a < 0.0)
return(-1.0);
}

double * MyACSign (double * a) {
if (*a 0.0)
*a = 1.0;
else if (*a < 0.0)
*a = -1.0;
return a;
}

Execution Code:
************************************************** ******************
double test = 0 ;
double test1 = 0;
test = 0.707107;
test1 = test * -1;

printf("Locations: %u, %u\r\n", &test, &test1);

printf("0Double: %f, %f", test, test1);
printf("1Double: %f, %f", ACSign(test), ACSign(test1));
printf("2Double: %i, %i", (char)ACSign(test), (char)ACSign(test1));

printf("3Double: %f, %f", 0.707107, -0.707107);
printf("4Double: %f, %f", ACSign(0.707107), ACSign(-0.707107));
printf("5Double: %i, %i", (char)ACSign(0.707107),
(char)ACSign(-0.707107));

printf("6Double: %f, %f", test, test1);
printf("7Double: %f, %f", *(double*)MyACSign(&test),
*(double*)MyACSign(&test1));
printf("8Double: %i, %i", (char)*(double*)MyACSign(&test),
(char)*(double*)MyACSign(&test1));
************************************************** ******************
The output I get is:
Locations: 4263558272, 4263558264
0Double: 0.707107, -0.707107
1Double: 0.707107, -0.707107
2Double: -98, -98
3Double: 0.707107, -0.707107
4Double: 0.707107, 0.000000
5Double: -98, -98
6Double: 0.707107, -0.707107
7Double: 1.000000, -1.000000
8Double: 1, -1

解决方案

Kevin <ke********@gmail.comwrites:

Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I''m having.

I doubt your problems have anything to do with alignment.
Rather, I suspect that you are not properly declaring the
functions in a header file, e.g.
double ACSign(double);
double *MyACSign(double *);
The prototypes (the bits in parentheses) are optional but they
help the compiler to check the calls.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}


On Wednesday 07 Nov 2007 1:57 am Kevin <ke********@gmail.comwrote in
article <11**********************@i38g2000prf.googlegroups .com>:

Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I''m having. Hopefully I can explain this best
through code below. ACSign and MyACSign functions below get compiled
(gcc) into one library, where as the code that executes these
functions gets compiled into a seperate library. Calls to MyACSign
work fine, calls to the straight ACSign do not and I believe it is due
to alignment issues, but I can''t pinpoint how. This <i>should</ihave
been very elementary. Any help is of course greatly appreciated,

- Kevin
Function Code:
************************************************** ******************
double ACSign ( double a) {
if (a 0.0)
return(1.0);

Both the parenthesis and the decimal point are unnecessary.

else if (a < 0.0)
return(-1.0);

Similarly here.

}

double * MyACSign (double * a) {
if (*a 0.0)
*a = 1.0;
else if (*a < 0.0)
*a = -1.0;
return a;
}

Execution Code:
************************************************** ******************
double test = 0 ;
double test1 = 0;
test = 0.707107;
test1 = test * -1;

printf("Locations: %u, %u\r\n", &test, &test1);

No. To print pointers use the ''%p'' specifier and cast the corresponding
argument to void *. Also the ''\r'' is not only not necessary but may
cause problems. The C Standard library translates a ''\n'' to whatever
the system uses as end-of-line.

printf("0Double: %f, %f", test, test1);
printf("1Double: %f, %f", ACSign(test), ACSign(test1));
printf("2Double: %i, %i", (char)ACSign(test), (char)ACSign(test1));

What''s the point in casting a double value to a char and then passing
them inccorrectly to printf after telling it to look for an int? Use
the ''%c'' format to print a char.

printf("3Double: %f, %f", 0.707107, -0.707107);
printf("4Double: %f, %f", ACSign(0.707107), ACSign(-0.707107));
printf("5Double: %i, %i", (char)ACSign(0.707107),

Same problems again. Besides what are you expecting to see after a lossy
cast?

(char)ACSign(-0.707107));

printf("6Double: %f, %f", test, test1);
printf("7Double: %f, %f", *(double*)MyACSign(&test),

No. Use *(MyACSign(&test)).

*(double*)MyACSign(&test1));

Similarly.

printf("8Double: %i, %i", (char)*(double*)MyACSign(&test),
(char)*(double*)MyACSign(&test1));

Again this mysterious cast to a char value.

************************************************** ******************
The output I get is:
Locations: 4263558272, 4263558264
0Double: 0.707107, -0.707107
1Double: 0.707107, -0.707107
2Double: -98, -98
3Double: 0.707107, -0.707107
4Double: 0.707107, 0.000000
5Double: -98, -98
6Double: 0.707107, -0.707107
7Double: 1.000000, -1.000000
8Double: 1, -1

As you have invoked undefined behaviour in several places you could get
any kind of output. I suggest you fix the obvious errors and recompile
and rerun.


Bingo, I stumbled upon the same answer at the same time - thanks!. I
missed the implied declaration warning in the compiling code and my
partners missed this one function in the header.

Thanks again!

- Kevin
On Nov 6, 3:37 pm, Ben Pfaff <b...@cs.stanford.eduwrote:

Kevin <kevinwo...@gmail.comwrites:

Hey, I was hoping someone out here might be able to clue me in on some
alignment issues I''m having.


I doubt your problems have anything to do with alignment.
Rather, I suspect that you are not properly declaring the
functions in a header file, e.g.
double ACSign(double);
double *MyACSign(double *);
The prototypes (the bits in parentheses) are optional but they
help the compiler to check the calls.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6}*,*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}



这篇关于令人不安的阵营(我认为)问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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