access memory location

I

Ivan

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
 
D

Daniel Fischer

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
 
J

Jonathan Mcdougall

Ivan said:
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
 
R

red floyd

Jonathan said:
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.
 
E

Emmanuel Delahaye

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.
 
G

Greg

Ivan said:
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
 
I

Ivan

red floyd said:
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
 
R

red floyd

Ivan said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top