absolute variable positioning

Larry Jones scjones at sdrc.UUCP
Sat Sep 9 07:38:47 AEST 1989


In article <471 at sagpd1.UUCP>, jharkins at sagpd1.UUCP (Jim Harkins) writes:
> Due to some hardware considerations I have a special block of memory that I
> have to use for storage of some variables.  Lets say it starts at address FOO.
> In C, whats the best way to say "integer fred in normal mem, struct wilma
> somewhere after FOO, struct barney somewhere after FOO, struct bam_bam in
> normal mem", etc?  The best I can come up with is
> 
> pointer = FOO
> struct a *wilma = pointer
> pointer += sizeof(struct a)
> struct b *barney = pointer
> pointer += sizeof(struct b) * NUMBER_OF_BARNEYS     /* array of barneys */

I would suggest packaging all of the variables you want to be in
the special memory into a single struct and then declaring a
pointer to that struct that is initialized appropriately.

	struct special {
	   struct a wilma;
	   struct b barney;
	   } *p = (struct special *)FOO;

That solves the problem for static allocation, if you ever need
dynamic allocation then you still have to write a malloc.
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"I have plenty of good sense.  I just choose to ignore it."
-Calvin



More information about the Comp.lang.c mailing list