字符串分配空间

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

字符串分配空间

658 Views
13951575469
Contributor I

你好,我正在使用CW11编写MC9S12ZVML31的程序,碰到了一个问题

假如采用这样编程:

方法一:

const char a[]="\nUVWAveInitOver!";
UartSendStr(UART1,a);

void UartSendStr(unsigned char UartNum,const char *Point)
{
   while(*Point!=0)
   {
      UartSend(UartNum,*Point);
      Point++;
   }
}

编译出来的MAP文件

.init           54 R 0xFE8000 0xFE8035 ROM
.startData 27 R 0xFE8036 0xFE8050 ROM
.rodata     144 R 0xFE8054 0xFE80E3 ROM            **********************
.text         16564 R 0xFE80E4 0xFEC197 ROM
.copy        2288 R 0xFEC198 0xFECA87 ROM
.stack       512 R/W 0x1000 0x11FF RAM
ptuTrigE  40 R/W 0x1200 0x1227 RAM
adcLists  128 R/W 0x1228 0x12A7 RAM
.data       2164 R/W 0x12A8 0x1B1B RAM              **********************

方法2:

UartSendStr(UART1,"\nUVWAveInitOver!");

void UartSendStr(unsigned char UartNum,const char *Point)
{
   while(*Point!=0)
   {
      UartSend(UartNum,*Point); 
      Point++;
   }
}

编译出来的MAP文件

.init           54 R 0xFE8000 0xFE8035 ROM
.startData 27 R 0xFE8036 0xFE8050 ROM
.rodata     124 R 0xFE8054 0xFE80CF ROM    **********************
.text         16542 R 0xFE80D0 0xFEC16D ROM
.copy        2308 R 0xFEC16E 0xFECA71 ROM
.stack       512 R/W 0x1000 0x11FF RAM
ptuTrigE    40 R/W 0x1200 0x1227 RAM
adcLists   128 R/W 0x1228 0x12A7 RAM
.data         2184 R/W 0x12A8 0x1B2F RAM    **********************

字符串"\nUVWAveInitOver!"方法一占用.rodata即144-124=20字节  方法二占用.data即 2184-2164=20字节

问题:为什么方法2的方式占用RAM空间,别的编译器对字符串都占用.rodata空间?如何在使用方式二的情况下字符串占用.rodata空间(注:如果我采用方式一,程序的可读性就不好了。)

帮我一下,谢谢

0 Kudos
1 Reply

578 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

HI 瞿 俊杰

I tested your issue with method 2, the string is in .rodata.

Please see attached video.

Have a great day,
Jun Zhang

 

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos