473,480 Members | 4,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

allocate memory at predefined address

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
5 12794
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4595
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
4
2561
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
7
6996
by: bijax | last post by:
hi, i am new to multidimensional array of c programming .pls help me to solve out this how to allocate memory for mutidimensional array? i.e a ;
4
4968
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is...
11
3142
by: Divick | last post by:
Hi, can somebody help me figure out how can I make write a function which inturn uses malloc routine to allocate memory which is 2^k aligned? The condition is such that there should not be any...
13
9654
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
14
2609
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
5
1777
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
1
2167
by: darkdai | last post by:
hey everyone im trying to write a code that will have different functions which will allocate memory, de-allocate, reallocate etc. this is my code #include<stdio.h> #include<stdlib.h> void main()...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.