EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

How to read/write an 'address' from a memory location......

Started by Unknown October 18, 2007
I am trying to read an address from a memory location and then write
into that memory location.
i.e.
<i>
unsigned long * mem_pointer = 0x93700020; //Valid memory location on
my hardware
unsigned long mem_value = *mem_pointer; //Retrieve the value (another
memory location) from
                                                               //the
pointer
</i>

The question is - How do i use the value at <i> mem_value </i> as an
address? Basically, I want to do something like.
<i>
*(unsigned char *)mem_value = 10.
</i>

Thanks and warm regards,
Rintu

On Oct 18, 10:16 am, aeroboro...@rediffmail.com wrote:
> I am trying to read an address from a memory location and then write > into that memory location. > i.e. > <i> > unsigned long * mem_pointer = 0x93700020; //Valid memory location on > my hardware > unsigned long mem_value = *mem_pointer; //Retrieve the value (another > memory location) from > //the > pointer > </i> > > The question is - How do i use the value at <i> mem_value </i> as an > address? Basically, I want to do something like. > <i> > *(unsigned char *)mem_value = 10. > </i> > > Thanks and warm regards, > Rintu
Your question / posting is unclear. Karthik Balaguru
aeroboro221@rediffmail.com wrote:
> > I am trying to read an address from a memory location and then > write into that memory location. i.e. > <i> > unsigned long * mem_pointer = 0x93700020; //Valid memory location > on my hardware > unsigned long mem_value = *mem_pointer; //Retrieve the value > (another memory location) from > //the > pointer > </i> > > The question is - How do i use the value at <i> mem_value </i> > as an address? Basically, I want to do something like. > <i> > *(unsigned char *)mem_value = 10. > </i>
Please keep your source lines under 72 chars (67 is better) to avoid those silly line wraps. Also avoid the // comment style, which doesn't survive line wraps, and is even illegal in C90 withut extensions. You have: unsigned long *mem_pointer = <something> where <something> is a suitable description of an address in your machine. To access the data at that address just use a *, i.e.: *mem_pointer = 10; or if (10 == *mem_pointer) /* do something */; or something_else = *mem_pointer. Note that a cast is not a legal lvalue. I.e: (unsigned char *)<VALUE> = anything; The type pointed to is built into the value of mem_pointer (unless that type is 'void'). -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> -- Posted via a free Usenet account from http://www.teranews.com
On 2007-10-18, aeroboro221@rediffmail.com <aeroboro221@rediffmail.com> wrote:
> I am trying to read an address from a memory location and then write > into that memory location. > i.e. ><i> > unsigned long * mem_pointer = 0x93700020; //Valid memory location on > my hardware > unsigned long mem_value = *mem_pointer; //Retrieve the value (another > memory location) from > //the > pointer ></i> > > The question is - How do i use the value at <i> mem_value </i> as an > address? Basically, I want to do something like. ><i> > *(unsigned char *)mem_value = 10. ></i>
Please don't wrap code examples when you post them. unsigned long **p1, *p2, v; p1 = (unsigned long **)0x93700020; p2 = *p1; v = *p2; or if you prefer a one-liner: v = **((unsigned long **)0x93700020); -- Grant Edwards grante Yow! Hello, GORRY-O!! at I'm a GENIUS from HARVARD!! visi.com
In article <1192684583.602035.191600@z24g2000prh.googlegroups.com>,
 <aeroboro221@rediffmail.com> wrote:
>I am trying to read an address from a memory location and then write >into that memory location.
[....]
>The question is - How do i use the value at <i> mem_value </i> as an >address? Basically, I want to do something like. > *(unsigned char *)mem_value = 10.
That should work, assuming that "unsigned long" is the same size as a pointer on your system. But as Grant Edwards points out, it's easier to use the compiler's type system directly, and just cast the address to be a pointer to a pointer, rather than a pointer to an unsigned long (which is another pointer in disguise): unsigned char **mem_pointer = 0x93700020; foo = **mem_pointer; or, if you prefer: unsigned char **mem_pointer = 0x93700020; unsigned char *mem_value = *mem_pointer; foo = *mem_value; -- Wim Lewis <wiml@hhhh.org>, Seattle, WA, USA. PGP keyID 27F772C1
<aeroboro221@rediffmail.com> wrote in message
news:1192684583.602035.191600@z24g2000prh.googlegroups.com...
> I am trying to read an address from a memory location and then write > into that memory location. > i.e. > <i> > unsigned long * mem_pointer = 0x93700020; //Valid memory location on > my hardware
[...] Using physical addresses in C code is dirty hack. You should not do that. Also you should never use the nonsense like "unsigned long". 1. In the linker file, define a section "foobar" at the required location. 2. Place a variable there: #pragma section "foobar" u32 * mem_pointer; 3. Use this pointer as any other variable. Vladimir Vassilevsky DSP and Mixed Signal Consultant www.abvolt.com
"Vladimir Vassilevsky" <antispam_bogus@hotmail.com> writes:

> <aeroboro221@rediffmail.com> wrote in message > news:1192684583.602035.191600@z24g2000prh.googlegroups.com... >> I am trying to read an address from a memory location and then write >> into that memory location. >> i.e. >> <i> >> unsigned long * mem_pointer = 0x93700020; //Valid memory location on >> my hardware > > [...] > > Using physical addresses in C code is dirty hack. You should not do that. > Also you should never use the nonsense like "unsigned long". > > 1. In the linker file, define a section "foobar" at the required location. > 2. Place a variable there: #pragma section "foobar" u32 * mem_pointer; > 3. Use this pointer as any other variable.
Really? I've never heard of anyone doing that. It seems cumbersome; you could end up with hundreds of sections in the object file. And you each definition appears twice. gcc lets you define variables in the linker script PROVIDE( PORT0 = 0xE0020000); Then you can reference it with extern volatile unsigned long PORT0; I don't quite see the advantage over the usual #define PORT0 (*(volatile unsigned long *)(0xE0020000)) Except.... aha! It should appear nicely in the debugger! Must try this now! :) -- John Devereux
On 2007-10-19, Vladimir Vassilevsky <antispam_bogus@hotmail.com> wrote:
> ><aeroboro221@rediffmail.com> wrote in message > news:1192684583.602035.191600@z24g2000prh.googlegroups.com... >> I am trying to read an address from a memory location and then write >> into that memory location. >> i.e. >> <i> >> unsigned long * mem_pointer = 0x93700020; //Valid memory location on >> my hardware > > [...] > > Using physical addresses in C code is dirty hack. You should not do that. > Also you should never use the nonsense like "unsigned long". > > 1. In the linker file, define a section "foobar" at the required location. > 2. Place a variable there: #pragma section "foobar" u32 * mem_pointer;
That's even less portable than the OPs "hack". -- Grant Edwards grante Yow! I'm dressing up in at an ill-fitting IVY-LEAGUE visi.com SUIT!! Too late...
On 2007-10-19, John Devereux <jdREMOVE@THISdevereux.me.uk> wrote:

>> 1. In the linker file, define a section "foobar" at the required location. >> 2. Place a variable there: #pragma section "foobar" u32 * mem_pointer; >> 3. Use this pointer as any other variable. > > Really? I've never heard of anyone doing that. It seems > cumbersome; you could end up with hundreds of sections in the > object file. And you each definition appears twice.
I agree. Allocating a separate section for each location you want to "hard-wire" is overly complex and fragile.
> gcc lets you define variables in the linker script > > PROVIDE( PORT0 = 0xE0020000); > > Then you can reference it with > > extern volatile unsigned long PORT0; > > I don't quite see the advantage over the usual > > #define PORT0 (*(volatile unsigned long *)(0xE0020000)) > > Except.... aha! It should appear nicely in the debugger! Must > try this now! :)
In my experience, the former method using PROT0 = 0xE0020000 will be visible to the debugger, the latter might or might not. -- Grant Edwards grante Yow! Now I am depressed ... at visi.com
aeroboro221@rediffmail.com wrote:
> I am trying to read an address from a memory location and then write > into that memory location.
Which "that" --- the one you read the address from, or the one the address specifies?
> unsigned long * mem_pointer = 0x93700020; > unsigned long mem_value = *mem_pointer; > > The question is - How do i use the value at <i> mem_value </i> as an > address?
An address kept in a variable is called a pointer, in C programs. So mem_value should be of a pointer type, rather than an integer. Consequentially, mem_pointer should be a pointer-to-pointer-to-something type, e.g. some_type **mem_pointer = ...; some_type *mem_value = *mem_pointer; some_type the_actual_thing = *mem_value;
> Basically, I want to do something like. > *(unsigned char *)mem_value = 10.
And what's keeping you from doing exactly that? But this all hides a probably more important question: why do you think you should be doing this?

The 2024 Embedded Online Conference