Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > access memory location

Reply
Thread Tools

access memory location

 
 
Ivan
Guest
Posts: n/a
 
      11-16-2005
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


 
Reply With Quote
 
 
 
 
Daniel Fischer
Guest
Posts: n/a
 
      11-16-2005
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
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      11-16-2005
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

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-16-2005
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.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      11-16-2005
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
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-16-2005
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

 
Reply With Quote
 
Ivan
Guest
Posts: n/a
 
      11-16-2005

"red floyd" <> wrote in message
news:13Lef.16952$q%. 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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-16-2005
Ivan wrote:
> "red floyd" <> wrote in message
> news:13Lef.16952$q%. 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).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid access to memory location. (Exception from HRESULT: 0x8007 =?Utf-8?B?SUthdHphdg==?= ASP .Net 0 06-11-2007 08:21 AM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
How to trick a page into running at a location different from it'sphysical location? Luke Dalessandro ASP .Net 0 01-15-2006 05:59 AM
access memory location Ivan C Programming 7 11-16-2005 10:27 PM
Programms memory adress location access? TIM C++ 9 04-12-2004 05:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57