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

allocate memory at predefined address

P: n/a
Hello,
I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
Thanks
Mike

Oct 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11**********************@m7g2000cwm.googlegroups. com>,
<am******@gmail.comwrote:
>I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
There is nothing in the Standard that would definitely allow you to
do this, and there is nothing in the Standard Library that allows
you to request that dynamic memory be allocated (or memory
permissions be granted to it) at any specific address.

There is, in other words, no fully portable method of doing what you ask.

However, it is within the terms of the Standard to -allow- implementations
to support casting integers to become pointers; what happens when you
do so is implementation defined.

For example, it is allowed for implementations to make meaningful

NVram_ptr = (unsigned char *)0x03E00000;
strcpy( &NVram_ptr[10], "nOW iS tHE tIME" );

but what this actually does would be up to the implementation.

(It wouldn't necessarily write at physical location 0x03E0000A --
it would be legitimate for an implementation to interpret
casting 0x03E00000 as a pointer to mean something like
"the address is at offset 0xE00000 from segment register #3". Or worse.)
Some operating systems provide tools that allow extern variables to
be placed at particular locations; for anything like that you would
need to look closely at the documentation of your linker.
--
All is vanity. -- Ecclesiastes
Oct 10 '06 #2

P: n/a

am******@gmail.com wrote:
Hello,
I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
Thanks
Mike
wait a sec, maybe you dont want what you're asking for.

you don't really want to malloc() anything, as malloc() uses the heap
in RAM memory.

You may be saying in the address space of your target system there is
some ROM (more likely flash memory) which is mapped into the address
space at some fixed, or findable address.

Now on a LOT of computers, C pointers correspond to memory addresses,
so you can OFTEN, but not everywhere:

volatile char * TheFlashMemoryChip[16384];

TheFlashMemoryChip = (volatile char *) 0x20000000; // chip select on
addr line 30

for( i = 0 ; i < 16384 ; i++ ) TheFlashMemoryChip[ i ] = '!'; //
initialize the chip

Oct 10 '06 #3

P: n/a
am******@gmail.com wrote:
I would like to allocte memory, but I want it to start at a predefined
address.
I have a program that writes data to ROM in an embedded device and I
would like to state where it goes.
Can this be done and how?
Why would you like to allocate memory when you already have memory
to which you're allowed to write to and you know its address? Just
get yourself a pointer, set it to the physical address and use it
as you need to. Like in

int main( void )
{
char *rom = 0xDEADBEEF; /* pysical address to write to */
char *data = "Does this end up in ROM?";

while ( *rom++ = *data++ )
/* empty */ ;
return 0;
}

Of course, this only works if you're allowed to write directly to
physical memory (and you're not too concerned about portability;-)
but on an embedded device that probably wouldn't be that uncommon.
The question is only how you are supposed to write to ROM - wouldn't
you need WOM for that?
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 10 '06 #4

P: n/a
am******@gmail.com wrote:
Can this be done and how?
Explore the memory management capabilities and/or the memory map of your
hardware, and/or your OS, and/or your system monitor.

If you can represent a value that is guaranteed to be a specific memory
address as constrained by your system, as a C pointer, than you can do
what you suggest. You may be forced to do this in assembly, and even
then, on many operating systems, you have to build something like a
kernel driver in order to allocate anything other than virtual memory.

That said, memory mapped I/O is still quite common. but memory-mapped
I/O bound to a specific hardware address space is really not something
you can talk about strictly in the context of C. This is a question for
comp.arch, or for some forum devoted to your hardware or OS platform.

There are linux kernel drivers that deal with memory-mapped I/O in all
kinds of devices, and might not be too hard to analyze. I'd look at
some of the ISA drive controllers or old sound cards for likely places
to find examples (but I am just guessing).


Oct 10 '06 #5

P: n/a
Ancient_Hacker wrote:
Now on a LOT of computers, C pointers correspond to memory addresses,
so you can OFTEN, but not everywhere:

volatile char * TheFlashMemoryChip[16384];
Are you sure you meant an array of 16384 pointers to char?
TheFlashMemoryChip = (volatile char *) 0x20000000; // chip select on addr line 30
This code is invalid. You can't assign to an array.
for( i = 0 ; i < 16384 ; i++ ) TheFlashMemoryChip[ i ] = '!'; // initialize the chip
Here you try to write integer values (characters) into pointer objects
without a cast.

I think defining TheFlashMemoryChip as:
volatile char * TheFlashMemoryChip;
would make a lot more sense.

--
Simon.
Oct 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.