why this code is executing properly

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

why this code is executing properly

跳至解决方案
1,399 次查看
rahulkrishna
Contributor IV

Hi,

i have written this code and expected the code to fail but it does not.

typedef unsigned char UINT8_T;

#define INDEX 100

#define NULL (void *)0

#pragma DATA_SEG __RPAGE_SEG  PAGED_RAM

UINT8_T buffer[INDEX];

#pragma DATA_SEG  DEFAULT

UINT8_T test_data[100];

UINT8_T *ptr_data=NULL;

void test(void)

{

UINT8_T fl_index_u8=0;

ptr_data = buffer;

for(fl_index_u8=0; fl_index_u8 < 100; fl_index_u8++)

{

  buffer[fl_index_u8] = fl_index_u8;

  test_data[fl_index_u8] = *ptr_data;

  ptr_data++;

  }

}

It compiled but with warning : C1860: pointer conversion: possible loss of data

I expected that test_data will be filled with default values of 0, but it is filling with data loaded into buffer variable. I am doing the full chip simulation, the address loaded into ptr_data = 0x00 and it is 2 bytes yet the progam is running successfully?

 

From the map file

ptr_data                                  2108       2       2       1   .bss       

buffer                                  FD1000      64     100       3   PAGED_RAM  

test_data                                 210C      64     100       1   .common    

 

What is the common area?

 

Thanks and regards

标签 (1)
标记 (2)
1 解答
1,244 次查看
kef2
Senior Contributor V

You should edit compiler settings and change C1860 message setting to produce compile error, not warning. Your code works because pointer dereference comes after access to buffer[], which sets up RPAGE register to the right value. Since both, buffer[] and *ptr_data are on the same RPAGE, you see data copied properly. Try defining more data, so that more than one R-page is used, also make ptr_data pointing to data, which is allocated on different R-page than buffer[].

Make sure there's -D__FAR_DATA in compiler command line switch. Project wizard may not include this important setting.

在原帖中查看解决方案

3 回复数
1,245 次查看
kef2
Senior Contributor V

You should edit compiler settings and change C1860 message setting to produce compile error, not warning. Your code works because pointer dereference comes after access to buffer[], which sets up RPAGE register to the right value. Since both, buffer[] and *ptr_data are on the same RPAGE, you see data copied properly. Try defining more data, so that more than one R-page is used, also make ptr_data pointing to data, which is allocated on different R-page than buffer[].

Make sure there's -D__FAR_DATA in compiler command line switch. Project wizard may not include this important setting.

1,244 次查看
rahulkrishna
Contributor IV

Thank you very much the reply. Now i understood. Can you please tell me or atleast point to the document which gives me some information about the switch you are referring to -D __FAR_DATA what does it do?

0 项奖励
回复
1,244 次查看
kef2
Senior Contributor V

__FAR_DATA define is required to make startup file initializing paged data properly. Once you start using paged RAM, you need to have it defined, else startup code will try to init wrong memory areas.