void *pfree;
struct something *x;
x = (struct something *)pfree;
pfree = (void *)((char *)pfree + sizeof(struct something));
pfree is a scratch space of contigous bytes. To do
pointer arithmetic to it, I have to cast it into (char *)
and then addition with return value of sizeof() which
assumes that sizeof(char) == 1 byte. [The problem
I see here is that I am not using "char" as a type
but as a unit of memory]
It seems that what I am attempting to do would be a
a fairly common requirement, so I wondered how
other folks more adept at C would implement this,
thanks
Adrian
No cast is required here. Make sure you are using a C compiler,
not asking a C++ compiler to read C code as if it were C++ -- the
rules for C++ pointers differ from those for C. (In particular,
"void *" is affected, along with other less relevant things like
const qualifiers.)
> pfree = (void *)((char *)pfree + sizeof(struct something));
This line can be expressed more simply as:
pfree = x + 1;
(Again, you need to use a C compiler, not a C++ compiler, to do this.)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
El Cerrito, CA, USA Domain: to...@bsdi.com +1 510 234 3167
http://claw.eng.bsdi.com/torek/ (not always up) I report spam to abuse@.
"nos...@elf.eng.bsdi.com" *is* my address (one of many actually).
The casts to and from (void *) are not necessary, the conversion
happens implicitly without any casts.
As for your pointer arithmetic question, for any pointer to T,
adding 1 will make it point to the next T in the array. Thus, your
example can be simplified to
x = pfree;
pfree = (struct something *) pfree + 1;
There is a problem here: if you create different pointers into the
array pointed to by pfree, some of your increments might produce
pointers that are not correctly aligned for the next type. You
should force alignment (which can't be done completely portably
AFAIK) or use different arrays for different types.
> pfree is a scratch space of contigous bytes. To do
> pointer arithmetic to it, I have to cast it into (char *)
> and then addition with return value of sizeof() which
> assumes that sizeof(char) == 1 byte. [The problem
> I see here is that I am not using "char" as a type
> but as a unit of memory]
That alleged assumption is not an assumption, the C Standard
guarantees that sizeof (char) == 1. Using a pointer to char as a
byte pointer for pointer aritmetic, as you did, is legal as well.
Gergo
--
To whom it may concern: My e-mail address has changed. Please adjust
your killfiles and address books. Thanks.
I think his point was that pfree was defined as something like:
char storage[whatever];
void *pfree = &storage;
so it could be used for things other than struct somethings. So
the revisions to pfree do require the cast IMO. He needs to be
careful about alignent on the assignments to x on some machines.
His system is only useful for the mark/release flavor of
allocation, which may well be quite enough. He is probably using
it to avoid the overhead of malloc.
--
Chuck F (cbfal...@yahoo.com) (cbfal...@XXXXworldnet.att.net)
Available for consulting/temporary embedded and systems.
(Remove "XXXX" from reply address. yahoo works unmodified)
mailto:u...@ftc.gov (for spambots to harvest)
I'm not entirly clear about what you are doing.
For starters you must make sure pfree is pointing to memory that is correctly
aligned to be treated as a struct something. malloc()ing would be one way.
After that you appear to be trying to use the pfree area is an array of
struct somthings. Why not just do that using a struct something* to access the
area?
Assuming struct something has fields f1 and f2...
/* access beginning of pfree */
x[0].f1 = flub;
x[0].f2 = slub;
/* move onto next struct in pfree */
x[1].f1 = slub;
x[1].f2 = flub;
yes you may sometimes have to do horrors with void* arithmetic. But I suspect
less often than you think. Why not declare pfree like this:-
unsigned char *pfree;
'cos you are allowed to do ptr arithmatic on unsigned char*s
If I have completly misunderstood you, could you give a more complete example?
--
Nick Keighley
Bill Gates says Open Source is riding on the coat-tails of Windows' success.
That's like saying Osama has done his part to increase airport security.
...
>> This line can be expressed more simply as:
>>
>> pfree = x + 1;
>>
>> (Again, you need to use a C compiler, not a C++ compiler, to do this.)
>
>I think his point was that pfree was defined as something like:
>
> char storage[whatever];
> void *pfree = &storage;
&storage is OK here but curious, plain storage would do.
storage isn't guaranteed to be suitably aligned for anything other than
characters. Better here would be to use malloc().
>so it could be used for things other than struct somethings. So
>the revisions to pfree do require the cast IMO.
No cast is needed above, you can convert implicitly to and from void *
as long as you don't lose necessary qualifiers.
> He needs to be
>careful about alignent on the assignments to x on some machines.
>
>His system is only useful for the mark/release flavor of
>allocation, which may well be quite enough. He is probably using
>it to avoid the overhead of malloc.
Even if you do that it still makes sense to allocate your initial storage
space using malloc().
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------