absolute variable positioning

Jim Harkins jharkins at sagpd1.UUCP
Fri Sep 8 08:54:10 AEST 1989


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 */

etc etc

Needless to say, this seems somewhat cumbersome and prone to error.  Especially
to the poor sucker that has to maintain the code.

To try to cut down on the number of well-meaning but frivolous responces:

1.  If I made a stupid mistake in this message, assume I know what I'm doing
    and I made a typo.  (I seem to get more responces from typos than anything).
2.  If I have to resort to the scheme above I'll encapsulate it in a function.
3.  So far I haven't needed to de-allocate variables once allocated, and they
    all get allocated on power up.  But this may change...
4.  I don't have a malloc available to do this unless I write one myself.
    (I know, one's in K&R).


                                                Jim
                                            "Only dead fish go with the flow"



More information about the Comp.lang.c mailing list