Array at specific address

D

Don

Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};

For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)

How do I declare this?

I cant't do MyArray[2048] = {0x00} @ 0xFFFF1199 :-(

I am using the gnu compiler.

Best Regards
Don
 
J

Joona I Palaste

Don said:
Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};
For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)
How do I declare this?

You can't. Not under ISO standard C. On some implementations, you can't
declare it at all.
I cant't do MyArray[2048] = {0x00} @ 0xFFFF1199 :-(

No, you can't.
I am using the gnu compiler.

It depends on more than your compiler. Some operating systems don't
allow specific addresses at all, due to security reasons. What happens
if some other process is already using that memory?
Please ask in gnu.gcc.help or a system-specific newsgroup.
 
D

Derk Gwen

# Hi I have an array of unsigned chars, like:
# MyArray[2048] = {0x00};

If it _has_ to be an array instead of (unsigned char*), many linkers allow you to
specify the address of extern objects. You might try checking what your linker
permits.
 
K

Kevin Easton

Derk Gwen said:
# Hi I have an array of unsigned chars, like:
# MyArray[2048] = {0x00};

If it _has_ to be an array instead of (unsigned char*), many linkers allow you to
specify the address of extern objects. You might try checking what your linker
permits.

You can do it without linker magic also:

#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

- Kevin.
 
D

Darrell Grainger

Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};

For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)

How do I declare this?

I cant't do MyArray[2048] = {0x00} @ 0xFFFF1199 :-(

I am using the gnu compiler.

If possible, just use:

unsigned char *MyArray = (unsigned char *)0xFFFF1199;

and then use MyArray as if you declared it as an array. The only
difference is that sizeof(MyArray) will not return 2048.

If you have to have it as an array then check with a newsgroup that deals
with your compiler. You can sometimes define a section of data to a linker
section name (.text, .bss. data, etc.) and then instruct the linker to put
that section at a specific memory location. This is highly dependent on
your linker and completely off topic for comp.lang.c.

--
darrell at cs dot toronto dot edu
or
main(){int j=1234;char t[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
 
M

Mark McIntyre

Derk Gwen said:
# Hi I have an array of unsigned chars, like:
# MyArray[2048] = {0x00};

If it _has_ to be an array instead of (unsigned char*), many linkers allow you to
specify the address of extern objects. You might try checking what your linker
permits.

You can do it without linker magic also:

#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.
 
M

Martijn

#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))
When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.

liar
 
M

Micah Cowan

Joona I Palaste said:
Don said:
Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};
For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)
How do I declare this?

You can't. Not under ISO standard C. On some implementations, you can't
declare it at all.

Of course you can.

memcpy((void*)0xFFFF1199, MyArray, sizeof MyArray);

Now, whether or not this does what you want is
implementation-defined, but you certainly can do it, if it's
doable. The Standard specifically says so.

-Micah
 
D

Default User

Martijn said:
#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.

liar


It would be just like the DS9000 to print out a bogus recipe. That's the
kind of thing it likes to do.




Brian Rodenborn
 
M

Micah Cowan

J

Jack Klein

Joona I Palaste said:
Don said:
Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};
For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)
How do I declare this?

You can't. Not under ISO standard C. On some implementations, you can't
declare it at all.

Of course you can.

memcpy((void*)0xFFFF1199, MyArray, sizeof MyArray);

Now, whether or not this does what you want is
implementation-defined, but you certainly can do it, if it's
doable. The Standard specifically says so.

-Micah

Exactly how does this declare an array of 2048 unsigned characters at
all?

BTW, you're another one who could use a proper signature separator.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

James

Hi,

To declare an array in C:
{
char temp[100] ;

}

Compiler must know its size. If the array is declared in a context the
compiler places it in stack and if a global it places it in heap.



Now how to place an array at a specified location.

I think a static array u can't place it.


With a pointer u can do it.

char * p = (char *) 1000 ;


Now u can use p like u use an array.

p [10] works



Hope this helps,



Regards,
James
 
K

Kevin Easton

Mark McIntyre said:
Derk Gwen said:
# Hi I have an array of unsigned chars, like:
# MyArray[2048] = {0x00};

If it _has_ to be an array instead of (unsigned char*), many linkers allow you to
specify the address of extern objects. You might try checking what your linker
permits.

You can do it without linker magic also:

#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.

A legal preprocessor directive did all that? I think you need to get a
conforming implementation (I'm reliably informed that one exists for the
DS9000).

- Kevin.
 
M

Mark McIntyre

Mark McIntyre said:
#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.

A legal preprocessor directive did all that?

The message appeared at runtime. The result of the directive is just a
tad implementation defined, I believe.
I think you need to get a
conforming implementation (I'm reliably informed that one exists for the
DS9000).

The one I have is as conforming as it gets, if somewhat
pathological...
 
M

Micah Cowan

Jack Klein said:
Joona I Palaste said:
Don <[email protected]> scribbled the following:
Hi I have an array of unsigned chars, like:
MyArray[2048] = {0x00};

For memory-mapping purposes I need to store this array at a specific address
(0xFFFF1199)

How do I declare this?

You can't. Not under ISO standard C. On some implementations, you can't
declare it at all.

Of course you can.

memcpy((void*)0xFFFF1199, MyArray, sizeof MyArray);

Now, whether or not this does what you want is
implementation-defined, but you certainly can do it, if it's
doable. The Standard specifically says so.

-Micah

Exactly how does this declare an array of 2048 unsigned characters at
all?

I never said it did. It does, however, answer the question "I
need to store this array at a specific address." He could use
memset() to better effect, though. And, of course, if he wants to
access it literally as an array, he can always do:

unsigned char (*MyArray)[2048] = (unsigned char (*)[2048])0xFFFF1199;

and dereference MyArray.
BTW, you're another one who could use a proper signature separator.

Huh. I'd always been under the impression that it was mainly for
those with big long sigs.
 
M

Micah Cowan

Hi,

To declare an array in C:
{
char temp[100] ;

}

Compiler must know its size. If the array is declared in a context the
compiler places it in stack and if a global it places it in heap.

That's not mandated by the standard, so it's
implementation-specific information (even if most existing
implementations happen to implement that way).
Now how to place an array at a specified location.

I think a static array u can't place it.

You can't place any array, regardless of whether static, dynamic
or automatic allocation is used. If you can write to the memory
at location "1000", you haven't allocated at all: it's simply there.
With a pointer u can do it.

char * p = (char *) 1000 ;


Now u can use p like u use an array.

p [10] works

No, you can't use p quite like an array. You use it more like a
pointer-to-char (which incidentally, is pretty much how you use
arrays most of the time). But among other things, you can't make
a call:

foo(&p)

to a function prototyped as:

void foo(char (*p)[1000]);

-Micah
 
K

Kevin Easton

Mark McIntyre said:
Mark McIntyre said:
On Thu, 25 Sep 2003 10:02:07 GMT, in comp.lang.c , Kevin Easton

#define MyArray (*(volatile unsigned char (*)[2048])(0xdeadbeef))

When I tried this on my DS9K, it said "moo" and printed out a recipe
for making cow from hamburger.

A legal preprocessor directive did all that?

The message appeared at runtime. The result of the directive is just a
tad implementation defined, I believe.

No, the result of the directive is to introduce a macro called MyArray.
That's all.
The one I have is as conforming as it gets, if somewhat
pathological...

Not if merely defining a macro caused unrequested output at runtime.

- Kevin.
 
R

Richard Heathfield

James said:
Now how to place an array at a specified location.

I think a static array u can't place it.


With a pointer u can do it.

char * p = (char *) 1000 ;


Now u can use p like u use an array.

p [10] works

char *p = (char *)10000; is legal C, but C offers absolutely no guarantees
that the address you've forced p to point to is a good place for storing
data. This is the kind of trick that it is best to avoid. For one thing,
it's considerably less portable than Brazil. For another thing, even if
it's legal to write to that space on your system (and it probably isn't, on
modern systems), where do you draw the line? p[100]? p[1000]?

(On the other hand, so-called "badly-behaved" MS-DOS programs used to do
this sort of thing all the time - for an excellent-at-the-time reason which
has become more or less irrelevant nowadays.)
 
M

Morris Dovey

Richard said:
char *p = (char *)10000; is legal C, but C offers absolutely
no guarantees that the address you've forced p to point to is
a good place for storing data. This is the kind of trick that
it is best to avoid. For one thing, it's considerably less
portable than Brazil. For another thing, even if it's legal to
write to that space on your system (and it probably isn't, on
modern systems), where do you draw the line? p[100]? p[1000]?

(On the other hand, so-called "badly-behaved" MS-DOS programs
used to do this sort of thing all the time - for an
excellent-at-the-time reason which has become more or less
irrelevant nowadays.)

It's not unusual to see this kind of code used in embedded
systems for which I/O and/or control spaces have been memory
mapped. I agree that it's not portable; but have found it
extremely useful within that limited context.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top