声明静态指针。 [英] Declaring static pointers.

查看:104
本文介绍了声明静态指针。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。

我的微控制器程序使用字符串数组。我想将

字符串及其指针存储在ROM中。我能够初始化数组所以

将字符串放入ROM中,但编译器将指针放在

RAM中。我很感激如何初始化它的建议,以便

指针和字符串都在ROM中。


这是我使用的声明。


const char * myvariables [] = {" var1"," var2"," vr3"," variable4"," v5"};


这里我发现var1,var2等在ROM中但是& var1,& var2等在RAM中。


编译器是MSP430-GCC。


建议赞赏。

解决方案

6月1日下午2:49,edson< e ... @ eircom .netwrote:


问候。

我的微控制器程序使用字符串数组。我想将

字符串及其指针存储在ROM中。我能够初始化数组所以

将字符串放入ROM中,但编译器将指针放在

RAM中。我很感激如何初始化它的建议,以便

指针和字符串都在ROM中。


这是我使用的声明。


const char * myvariables [] = {" var1"," var2"," vr3"," variable4"," v5"};


这里我发现var1,var2等在ROM中但是& var1,& var2等在RAM中。


编译器是MSP430-GCC。


建议表示赞赏。



肯定是链接器/硬件依赖?


这些指针基本上是美化的数字,恰好指向

存储数据的ROM中的正确位置。

然而,c编译器不知道这些存储的位置,因为

即接头选择。因此简单地说,c编译器不能将指针放在ROM中。


将指针放在ROM中的唯一方法是

配置链接器始终将变量放在ROM中已知地址的正确

位置。这可以通过

链接器脚本完成(大多数事情都可以)。然后简单地在代码中初始化指针

(例如:const char * var1 =(char *)0xaddress)。


但是,为什么需要在ROM中有指针吗?

即使在你的微控制器设置中,你也不能使用相对的

地址吗?

另外,看看在重定位表上链接器

产生,你可能会在那里找到大多数东西...


希望这有帮助,

Franchie


Franchie写道:


>



.... snip ...


>

这些指针基本上都是美化的数字,这些数字恰好是b $ b指向存储数据的ROM中的正确位置。



你需要摆脱这个想法。它可能适用于某些地方,但不是所有的b $ b。例如,指向我家的指针可能是:


沿着这条街走两个街区,左转,走一个街区,

右转它是左边的第3个房子。


你可能会注意到它有一个数字,实际上是几个数字,

指示等等。这不是一个数字。


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost .txt>

< http://www.securityfocus.com/columnists/423>

< http://www.aaxnet.com/editor/edit043 .html>

< http://kadaitcha.cx/vista/dogsbreakfast/index.html>

cbfalconer at maineline dot net


-

通过 http:/的免费Usenet帐户发布/www.teranews.com


6月1日下午5:55,CBFalconer< cbfalco ... @ yahoo.comwrote:


你需要摆脱那个id EA。它可能适用于某些地方,但不是所有的b $ b。例如,指向我家的指针可能是:

沿着这条街走两个街区,左转,走一个街区,

右转,它是'左边的第3个房子。

你可能注意到有一个数字,实际上有几个数字,

指示等等。这不是一个数字。



当然,到你家的方向是无关紧要的? :D


但是,在当前的上下文中,我们引用内存中的地址,

通常是距离给定参考点的* *字节* 。

从这个意义上说,说出一个数字有什么问题?


也许你是对的,而且这看起来计算得太多了

从硬件的角度来看,这对于高级别的b
编程来说并不理想。

在辩护中,OP问的是微观的控制器,这是非常接近硬件的
;-)


你需要摆脱这个想法。它可能适用于某些地方,但不是全部。



只是感兴趣...''指针=记忆地址''想法不在哪里?
工作?这不是指针的定义吗?


谢谢,

Franchie。


Greetings.
My microcontroller program uses arrays of strings. I want to store the
strings AND their pointers in ROM. I am able to initialize the arrays so
that the strings are put in ROM, but the compiler puts the pointers in
RAM. I would appreciate advice on how to initialize it so that both
pointers and strings are in ROM.

Here is the declaratin I use.

const char *myvariables[] = {"var1", "var2", "vr3", "variable4", "v5"};

Here I find that var1, var2 etc are in ROM but &var1, &var2 etc are in RAM.

Compiler is MSP430-GCC.

Advice appreciated.

解决方案

On Jun 1, 2:49 pm, edson <e...@eircom.netwrote:

Greetings.
My microcontroller program uses arrays of strings. I want to store the
strings AND their pointers in ROM. I am able to initialize the arrays so
that the strings are put in ROM, but the compiler puts the pointers in
RAM. I would appreciate advice on how to initialize it so that both
pointers and strings are in ROM.

Here is the declaratin I use.

const char *myvariables[] = {"var1", "var2", "vr3", "variable4", "v5"};

Here I find that var1, var2 etc are in ROM but &var1, &var2 etc are in RAM.

Compiler is MSP430-GCC.

Advice appreciated.

Surely thats linker/hardware dependant?

The pointers are basically glorified numbers which happen to point to
the correct location in ROM where the data is stored.
The c compiler, however, does not know where these will be stored, as
that is the linkers choice. Thus simply put, the c compiler cannot
place the pointers in ROM.

The only way to get the pointers to be placed in ROM would be to
configure the linker to always place the variables in the correct
place in ROM, at a known address. This can probably be done with a
linker script (most things can). Then simply initialise the pointers
in your code (eg: const char *var1 = (char*)0xaddress ).

However, why do you need to have the pointers in ROM?
Even in your microcontroller setting, couldn''t you use relative
addresses?
Alternatively, have a look at the relocation table the linker
produces, you will probably find most things in there...

Hope this helps,
Franchie


Franchie wrote:

>

.... snip ...

>
The pointers are basically glorified numbers which happen to
point to the correct location in ROM where the data is stored.

You need to get rid of that idea. It may work some places, but not
all. For example, a pointer to my house might be:

"Go down this street two blocks, turn left, go one block,
turn right and it''s the 3rd house on the left."

which you may note has a number, in fact several numbers,
directions, etc. It is not a number.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com


On Jun 1, 5:55 pm, CBFalconer <cbfalco...@yahoo.comwrote:

You need to get rid of that idea. It may work some places, but not
all. For example, a pointer to my house might be:
"Go down this street two blocks, turn left, go one block,
turn right and it''s the 3rd house on the left."
which you may note has a number, in fact several numbers,
directions, etc. It is not a number.

Surely the direction to your house is irrelevant at this point? :D

However, in the current context, we refer to an address in memory,
typically a *number* of bytes away from a given reference point.
In that sense, what is wrong with saying its a number??

Maybe you are right, though, and this is looking at computing too much
from a hardware standpoint, which is not ideal for high-level
programming.
In defence, the OP was asking about a micro-controller, which is
notoriously close to hardware ;-)

You need to get rid of that idea. It may work some places, but not all.

Just interested... where does the ''pointer = memory address'' idea not
work? Isn''t that the very definition of ''pointer''?

Thanks,
Franchie.


这篇关于声明静态指针。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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