By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

access memory location

P: n/a
Hi

how can I access the contents of a memoy location say at address 0x3FF in C?

I tried this but didn't work

unsigned int *wVal = 0x3FF;
x = *wVal;
Thanks
ivan
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Wed, 16 Nov 2005 18:04:26 +0000, Ivan wrote:
how can I access the contents of a memoy location say at address 0x3FF
in C?
Not at all, but
unsigned int *wVal = 0x3FF;
x = *wVal;


might work on some platforms. It's not valid C, of course.

The address being odd might be a problem on some machines. Try accessing
it through an unsigned char * instead of int *, which needs to be aligned
on many platforms.
Daniel
Nov 16 '05 #2

P: n/a
Ivan wrote:
Hi

how can I access the contents of a memoy location say at address 0x3FF in C?

I tried this but didn't work

unsigned int *wVal = 0x3FF;
x = *wVal;


That's undefined behavior in standard C++, but it may work on your
platform. You have to cast it explicitly because 0x3FF is an int and
ints cannot be implicitly converted to pointers.

unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);
Jonathan

Nov 16 '05 #3

P: n/a
Jonathan Mcdougall wrote:
Ivan wrote:
Hi

how can I access the contents of a memoy location say at address 0x3FF in C?

I tried this but didn't work

unsigned int *wVal = 0x3FF;
x = *wVal;

That's undefined behavior in standard C++, but it may work on your
platform. You have to cast it explicitly because 0x3FF is an int and
ints cannot be implicitly converted to pointers.

unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);


What, exactly, is the OP trying to accomplish? Is he trying to tweak an
interrupt vector or memory mapped I/O?

Unless you're on a system where there's no memory management, there's no
guarantee that 0x3ff will be a valid memory address in your address
space. If you're attempting to access a memory-mapped I/O port, or some
system structure, you're toast, under a virtual memory system.

If you're writing an OS kernel or run-time executive, or you're in an
environment where there's no MMU, then that's an entirely different
kettle of fish.

Nov 16 '05 #4

P: n/a
Ivan a écrit :
how can I access the contents of a memoy location say at address 0x3FF in C?


Memory location is a vague concept. If you meant physical address, you
can try

unsigned int *wVal = (unsigned int *)0x3FF;

but the result depends on the implementation. Even if this is
compilable, the value can be a physical address or not. If your system
has a MMU (Memory Management Unit), the physical addresses can only be
accessed under special conditions (Kernel mode, ring0 etc.), like the
ones in a driver.

--
A+

Emmanuel Delahaye
Nov 16 '05 #5

P: n/a
Ivan wrote:
Hi

how can I access the contents of a memoy location say at address 0x3FF in C?

I tried this but didn't work

unsigned int *wVal = 0x3FF;
x = *wVal;


Consult your compiler's documentation to see if it supports declaring
variables at specific memory addresses. Metrowerks Codewarrior for
example has a specific syntax to do so. And I would imagine that almost
any compiler that supports embedded systems would have a similar
capability.

Greg

Nov 16 '05 #6

P: n/a

"red floyd" <no*****@here.dude> wrote in message
news:13*****************@newssvr12.news.prodigy.co m...
Jonathan Mcdougall wrote:
Ivan wrote:
Hi

how can I access the contents of a memoy location say at address 0x3FF in C?
I tried this but didn't work

unsigned int *wVal = 0x3FF;
x = *wVal;

That's undefined behavior in standard C++, but it may work on your
platform. You have to cast it explicitly because 0x3FF is an int and
ints cannot be implicitly converted to pointers.

unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);


What, exactly, is the OP trying to accomplish? Is he trying to tweak an
interrupt vector or memory mapped I/O?

Unless you're on a system where there's no memory management, there's no
guarantee that 0x3ff will be a valid memory address in your address
space. If you're attempting to access a memory-mapped I/O port, or some
system structure, you're toast, under a virtual memory system.

If you're writing an OS kernel or run-time executive, or you're in an
environment where there's no MMU, then that's an entirely different
kettle of fish.

it is an embedded system, I thought there was a standard way of doing it,
well I suppose I'll have to contact them to know how to do it.

thanks you all
Nov 16 '05 #7

P: n/a
Ivan wrote:
"red floyd" <no*****@here.dude> wrote in message
news:13*****************@newssvr12.news.prodigy.co m...

it is an embedded system, I thought there was a standard way of doing it,
well I suppose I'll have to contact them to know how to do it.

thanks you all


Then Johnathan is correct. The main issue I see is the potential
misalignment.

Assuming you do in fact have direct hardware access, and that you really
want an unsigned at that alignment, you should use one of the two
constructs:

// C style (you did crosspost to c.l.c)
unsigned int *const wPtr = ((unsigned int *)0x3ff);

or

// C++ style (you did crosspost to c.l.c++)
unsigned int *const wPtr = reinterpret_cast<unsigned int *>(0x3ff);

if 0x3ff is a read-only memory address, make it

unsigned int const *const wPtr = ...;

If 0x3ff is a memory mapped register, which can change under you, make
it volatile as well (volatile goes before the * in the declaration, both
sides of the assignment).
Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.