16位系统上的16位指针类型转换 [英] 16 bit pointer typecast on 16 bit system
问题描述
ANSI C对于将16位指针转换为16位指针的说法是什么,
当字节指针指向奇怪的地址?我在Samsung CalmShine 16编译器中检测到了
问题。这个编译器我用的是/ b
三星16位智能卡芯片,我想知道它是否符合ANSI标准,或者是否违反它。
看看这个超级简单的例子,其中b的值是
不正确:
#include< stdio .h>
//在EEPROM存储器中定义一个地址为0x081000的数组
char data [8] = {0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07} _at_
0x81000,eeprom;
int main(无效)
{
char a;
短b;
长c;
char * bpData;
//设置指针指向奇数地址
bpData =& data [1];
//读取一个字节来自偶数地址
a = *(bpData + 1);
//从奇数地址读取一个字。这会导致问题。而不是
//获得0x0102而不是0x001。 ANSI C对此有何看法?
b = *(短*)(bpData);
//只是为了检查指针的值,如果它的价值是将
存储在一个变量中
c =(long)bpData;
返回0x0;
}
是的,你应该得到0x102(如果它是Big-endian)或0x201(如果它是
Little-endian)但绝对不是0x1。
我已经用gcc中的以下测试程序确认了这一点:
#include< stdio.h>
char a [] = {0,1,2,3,4};
short b ;
int main()
{
b = *(短*)(a + 1);
printf(" b = 0x%x \ n",b);
返回0;
}
它打印结果b = 0x201 (在x86上,这是小端)。
我会说你的编译器坏了...
MQ。
mi ***** *********@gmail.com 写道:
是的,你应该得到0x102(如果它是Big-endian)或0x201(如果它是
Little-endian)但绝对不是0x1。
我已经用gcc中的以下测试程序确认了这一点:
#include< stdio.h>
char a [] = {0,1,2,3,4};
短b;
int main()
{
b = *(短*)(a + 1);
printf(" b = 0x%x \ n",b);
返回0;
}
打印结果b = 0x201 (在x86上,这是小端)。
我会说你的编译器坏了...
I ''你说你的期望被打破了。
你拿的是一个值,并且从未指向过短的指针,强迫
转换它指向短指针,并取消引用它 - 听起来像我这样会产生不确定的行为。
-
Chris" seeker" Dollin
我还在这里,我正在拿着答案 - Karnataka,/ Love and Affection /
文章< 11 ****************** ***@a14g2000cwb.googlegroups。 comChristian Wittrock < cu*@xponcard.dkwrites:
....
char * bpData;
....
bpData =& data [1];
....
//从奇数地址读取一个字。这会导致问题。而不是
//获得0x0102而不是0x001。 ANSI C对此有何评价?
b = *(短*)(bpData);
未定义的行为。
-
dik t。冬天,cwi,kruislaan 413,1098 sj amsterdam,nederland,+ 31205924131
home:bovenover 215,1025 jn amsterdam,nederland; http://www.cwi.nl/~dik/
Hi,
What does ANSI C say about casting an 8 bit pointer to a 16 bit one,
when the byte pointer is pointing to an odd address? I have detected a
problem in the Samsung CalmShine 16 compiler. This compiler I use for
the Samsung 16 bit Smartcard chips and I want to know if it is
compliant with the ANSI standard or if it violates it.
Have a look at this super simple example, where the value of b is
incorrect:
#include <stdio.h>
// Define an array in the EEPROM memory at address 0x081000
char data[8]={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07} _at_
0x81000, eeprom;
int main ( void )
{
char a;
short b;
long c;
char* bpData;
// Set up the pointer to point to an odd address
bpData = &data[1];
// read a byte from an even address
a= *(bpData + 1);
// Read a word from an odd address. This causes problems. Instead of
// getting 0x0102 I get 0x001. What does ANSI C say about this?
b= *(short*)(bpData) ;
// just to check the value of the pointer, if the value of it is to
be stored in a variable
c= (long)bpData;
return 0x0 ;
}
yes, you should get 0x102 (if it is Big-endian) or 0x201 (if it is
Little-endian) but definitely not 0x1.
I''ve confirned this with the following test program in gcc:
#include <stdio.h>
char a[] = {0,1,2,3,4};
short b;
int main()
{
b = *(short *)(a+1);
printf("b = 0x%x\n",b);
return 0;
}
It prints the result "b = 0x201" (on an x86, which is little-endian).
I''d say that your compiler is broken...
MQ.
mi**************@gmail.com wrote:
yes, you should get 0x102 (if it is Big-endian) or 0x201 (if it is
Little-endian) but definitely not 0x1.
I''ve confirned this with the following test program in gcc:
#include <stdio.h>
char a[] = {0,1,2,3,4};
short b;
int main()
{
b = *(short *)(a+1);
printf("b = 0x%x\n",b);
return 0;
}
It prints the result "b = 0x201" (on an x86, which is little-endian).
I''d say that your compiler is broken...I''d say your expectations are broken.
You take a value that isn''t and never has been pointer-to-short, forcibly
convert it to pointer-to-short, and dereference it -- that sounds to me
like it would generate undefined behaviour.
--
Chris "seeker" Dollin
"I''m still here and I''m holding the answers" - Karnataka, /Love and Affection/
In article <11*********************@a14g2000cwb.googlegroups. com"Christian Wittrock" <cu*@xponcard.dkwrites:
....char* bpData;....
bpData = &data[1];....
// Read a word from an odd address. This causes problems. Instead of
// getting 0x0102 I get 0x001. What does ANSI C say about this?
b= *(short*)(bpData) ;Undefined behaviour.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
这篇关于16位系统上的16位指针类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!