473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Pointers to specific memory addresses

In coding some embedded software, I frequently find that I need to read
from a specific memory location; it's a hard-wired location that always
contains the same information. A register, for example.

To do so, I tend to define a pointer along the lines of

int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data I'm
interested in.

The compiler I compile embedded code with (in the VDSP environment)
spits out a warning that I've created a pointer in this way, but
generates working code anyway.

Why would the compiler be warning me about this? Are there compilers
that would produce errors, rather than warnings? Is there some other way
I should be explicitly defining a location in memory to read?

To reiterate, these memory locations are not system variables with
nicely defined labels that I can fetch; they're raw locations.

It's common to use predefined values supplied with the chip support
software instead of stating the memory location myself, but in rooting
through the support code I find it usually ends up doing much the same
thing anyway.

'Chops
Jan 12 '08 #1
9 6775
On 2008-01-12 14:24:04 -0500, moschops <mo*****@madasafish.comsaid:
>
int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data I'm
interested in.

The compiler I compile embedded code with (in the VDSP environment)
spits out a warning that I've created a pointer in this way, but
generates working code anyway.

Why would the compiler be warning me about this? Are there compilers
that would produce errors, rather than warnings? Is there some other
way I should be explicitly defining a location in memory to read?
Standard C++ does not support hard-coded addresses. Compilers for
embedded systems usually have a facility for this sort of thing.
Alternatively, you can usually just add a cast:

int *p = (int*)0x1000001;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 12 '08 #2
Pete Becker wrote:
On 2008-01-12 14:24:04 -0500, moschops <mo*****@madasafish.comsaid:
>>
int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data
I'm interested in.
-
Standard C++ does not support hard-coded addresses. Compilers for
embedded systems usually have a facility for this sort of thing.
Alternatively, you can usually just add a cast:

int *p = (int*)0x1000001;
Better is to make it a const pointer to a volatile object,
so the compiler won't optimize it (got burned by this with
a TI 34020 compiler).

int volatile *const pSOME_REG =
reinterpret_cast<int volatile *>(0x10000001);

And, if it's a read only register :

volatile int *const pSOME_RO_REG =
reinterpret_cast<int volatile const int*>(0x10000001);

volatile const int* const
Jan 12 '08 #3
On 2008-01-12 16:10:28 -0500, red floyd <no*****@here.dudesaid:
Pete Becker wrote:
>On 2008-01-12 14:24:04 -0500, moschops <mo*****@madasafish.comsaid:
>>>
int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data I'm
interested in.
-
Standard C++ does not support hard-coded addresses. Compilers for
embedded systems usually have a facility for this sort of thing.
Alternatively, you can usually just add a cast:

int *p = (int*)0x1000001;

Better is to make it a const pointer to a volatile object,
so the compiler won't optimize it (got burned by this with
a TI 34020 compiler).

int volatile *const pSOME_REG =
reinterpret_cast<int volatile *>(0x10000001);

And, if it's a read only register :

volatile int *const pSOME_RO_REG =
reinterpret_cast<int volatile const int*>(0x10000001);

volatile const int* const
Maybe, maybe not. But it's a separate question from what was asked.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jan 12 '08 #4
red floyd wrote:
Pete Becker wrote:
>On 2008-01-12 14:24:04 -0500, moschops <mo*****@madasafish.comsaid:
>>>
int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data
I'm interested in.
-
Standard C++ does not support hard-coded addresses. Compilers for
embedded systems usually have a facility for this sort of thing.
Alternatively, you can usually just add a cast:

int *p = (int*)0x1000001;

Better is to make it a const pointer to a volatile object,
so the compiler won't optimize it (got burned by this with
a TI 34020 compiler).
Good thinking, many thanks. I'll probably go easy on the C++ style
reinterpret casting (mainly because the other guy working on this
doesn't speak C++), but it's a good idea.

'Chops
Jan 12 '08 #5
moschops:
In coding some embedded software, I frequently find that I need to
read
from a specific memory location; it's a hard-wired location that
always
contains the same information. A register, for example.

You're using C++ for embedded? Really? Out of curiosity, what
microcontroller are you using that you can get a C++ compiler for? Is
are either of the microcontroller or the compiler expensive?
To do so, I tend to define a pointer along the lines of

int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data
I'm
interested in.

The compiler I compile embedded code with (in the VDSP environment)
spits out a warning that I've created a pointer in this way, but
generates working code anyway.


I don't like when compilers are too liberal. This should be an error.
Why would the compiler be warning me about this?


Be glad that you don't know! If you were to use this compiler for quite
a while, you might some day write code that erroneously uses an integer
for an address (e.g. you write int *p = 9 instead of int *p = 0).

Are there compilers
that would produce errors, rather than warnings?

Yes, all the good ones will give errors for type mismatches.

Is there some other way
I should be explicitly defining a location in memory to read?

As people have said, use a cast:

int *p = (int*)0x6872;
--
Tomás Ó hÉilidhe
Jan 12 '08 #6
Tomás Ó hÉilidhe wrote:
You're using C++ for embedded? Really? Out of curiosity, what
microcontroller are you using that you can get a C++ compiler for? Is
are either of the microcontroller or the compiler expensive?
I'm using this -

http://www.analog.com/processors/vis...testDrive.html

to write code for one of these -

http://www.vmetro.com/category3954.html

I don't like when compilers are too liberal. This should be an error.
Most of the time, I'm in favour of errors over warnings as well. In this
case, though, I'm torn. I create a pointer. The pointer has a value
which is the numerical address of a given unit of the memory. That
numerical address is conceptually an unsigned integer. When I look at
the memory, a pointer holding the address 0x10000001 is identical to an
unsigned integer of the value 0x10000001. The compiler sees a
difference, but the hardware doesn't. I can appreciate that the compiler
should to an extent hold my hand, but on the other hand in embedded work
I need to be close to the hardware.
Be glad that you don't know! If you were to use this compiler for quite
a while, you might some day write code that erroneously uses an integer
for an address (e.g. you write int *p = 9 instead of int *p = 0).
Surely I just _have_ quite deliberately written int* p = 9? Or, in this
case, int* p = 0x10000001 ?

'Chops
Jan 12 '08 #7
On Jan 12, 3:54 pm, moschops <mosc...@madasafish.comwrote:
The pointer has a value
which is the numerical address of a given unit of the memory.
YOU know that, but the compiler doesn't. On some platforms, not all
valid integers are valid pointers. The compiler is enforcing the
standard, which does not make guarantees about hardware.
That
numerical address is conceptually an unsigned integer. When I look at
the memory, a pointer holding the address 0x10000001 is identical to an
unsigned integer of the value 0x10000001.
It might be on YOUR hardware, but it's not on all hardware. The
contents of that memory are a set of bits. The standard can't know
what the implementation does with them.

Imagine for a moment a segmented platform, where pointers are a
segment:offset pair. That 0x10000001 initializer might mean segment
0x1000, offset 0x0001. Or it might mean linear address 0x10000001.
All the compiler is doing is forcing you to declare "I know what I am
doing, and I know this is not portable" by casting.

Imagine a platform that sets the high bit of pointers to indicate that
they are valid.
The compiler sees a
difference, but the hardware doesn't.
What about platforms where unaligned accesses are not allowed?
I can appreciate that the compiler
should to an extent hold my hand, but on the other hand in embedded work
I need to be close to the hardware.
So turn off those warnings or cast them away?
Jan 13 '08 #8
Tomás Ó hÉilidhe wrote:

You're using C++ for embedded? Really? Out of curiosity, what
microcontroller are you using that you can get a C++ compiler for? Is
are either of the microcontroller or the compiler expensive?
Many times a cross-compiler is used. For instance Green Hills for
compiling C++ to run under the VxWorks RTOS. Any number of boards
support that.


Brian
Jan 13 '08 #9
On Jan 13, 12:24 am, moschops <mosc...@madasafish.comwrote:
In coding some embedded software, I frequently find that I need to read
from a specific memory location; it's a hard-wired location that always
contains the same information. A register, for example.

To do so, I tend to define a pointer along the lines of

int* pChipVersionNumber = 0x1000001

for example, where 0x10000001 is the permanent location of the data I'm
interested in.

The compiler I compile embedded code with (in the VDSP environment)
spits out a warning that I've created a pointer in this way, but
generates working code anyway.

Why would the compiler be warning me about this? Are there compilers
that would produce errors, rather than warnings? Is there some other way
I should be explicitly defining a location in memory to read?

To reiterate, these memory locations are not system variables with
nicely defined labels that I can fetch; they're raw locations.

It's common to use predefined values supplied with the chip support
software instead of stating the memory location myself, but in rooting
through the support code I find it usually ends up doing much the same
thing anyway.

'Chops
which cross-compiler are you using?
Jan 13 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example:...
1
by: arvind.nm | last post by:
hi ppl, i am implementing a garbage collector for c++. in order to trace pointers using the root set, i need some way to distinguish pointers from data in memory (heap). i.e by analyzing the...
5
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long...
3
by: Cleo | last post by:
How would I hard code a certain variable to be stored in a specific memory location in a processor. When I declare a variable in my program, the linker automatically determines a location and assigns...
2
by: Virtual_X | last post by:
- as we know that the pointer points to a memory cell - and each memory cell has the minimal size that the computer manage (1 byte) - that code int *p; int x; p= &x;
8
by: Vincent SHAO | last post by:
Well, i have a question here. i want to write 50000 into a specific memory unit (like # 4321). So i wrote these: int number=5000; memcpy((char *)50000,(char *)&number,sizeof(char)); char *...
14
by: Yep | last post by:
Hi Guys, I have some confusion with pointers and implicitly specifying a size of memory to write. I will try to explain what I am trying to do a bit better. char *myaddress=(char *)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.