B
Ben Bacarisse
James Brown said:Harald van Dijk said:They could be the same, but this is not guaranteed. Even for systems
where the size and representation are the same, compilers'
optimisations may cause your code to not function the way you want.
If you could be more specific about what you're trying to do,
preferably using a short code snippet, someone may be able to suggest
a way to avoid the issue.
Thankyou for your interest. Your comment about 'optimization' is
appreciated also. I will try to explain what I am attempting, but I
have no actual 'code' yet - I am still in the 'is this possible'
stage, hence my original question.
I guess what I am trying to do is 'flatten' arbitrary types and
maintain their type information 'out of band'. For example:
1. A (char *) pointer (to an array of characters) would be represented
as-is.
2. An array of pointers-to-char ( char *argv[] for example) would have
each string in the array 'flattened' in turn.
3. A three-level pointer (char ***) would be treated similarly.
My current intention is to write a function that takes a generic
pointer type (void* I guess), along with an array of type-information
that describes each level of indirection in terms of it's size and
length. This generic function would then flatten the specified
array/pointer/type/whatever according to the type information. There
might be one function per 'base type' - i.e. one that handled
chars,char*,char**, one that handled int,int* etc.
For example (note this is not a complete/compilable fragment).
enum TYPE { NONE, ARRAY, POINTER };
struct TYPEINFO
{
enum TYPE type;
int elements;
};
void marshall(struct TYPEINFO *ti, void *ptr);
int main(int argc, char *argv[])
{
/* describe the argv[] array for marshalling purposes */
struct TYPEINFO ti[] = { { ARRAY, argc }, { POINTER, -1 }, { NONE } };
marshall(ti, argv);
return 0;
}
If this needs to portable you will have trouble as has been discussed
elsethread -- but you have a way out. You have an IDL (is the design
under your control?) and you generate C, so you can generate short
stubs that do the work for specific pointer types.
To marshal[1] a char *** that your IDL tells you is an array you can
generate:
void marshal_char_ppp(char ***p, size_t n)
{
while (n--) marshal_char_pp(*p++);
}
and so on.
I did something much like this for a portable RPC mechanism in the
days before ANSI C (not that the '89 standard would have helped all
that much in this case).