Portablity using structures and malloc - Help
Vaughn Vernon
vlv at drux1.UUCP
Thu Jul 18 07:18:06 AEST 1985
No first time guessers please! I need help.
I've really been wondering about the way in which the UNIX 'C' compiler
handles structures and memory allocation across processors.
I recently ported a large program from a VAX to a 3B2 and had some
real problems with it. I would like to propose an example and some
questions about the simple concept that was used. I will also give
my (probably wrong) proposed answer.
struct line {
char n[81];
double abc; /* no alignment by me! */
int x, /* here either */
y,
z;
char *xyz;
} *lines[MAXLINES];
...
if((lines[i] = (struct line *)malloc(sizeof(struct line)))\
==(struct line *)NULL)
...
if((lines[i]->xyz = malloc(sizeof(lines[i]->xyz)))==(char *)NULL)
...
Look closely. I'm allocating memory for the structure and getting a
pointer back. The pointer returned on a 3B or 68K may or may not be
on a word boundary. Right? In either case, what will this do
to the xyz character pointer and the int's? Malloc() does not know it's
dealing with a structure pointer so will xyz be aligned? What about the
address being returned to the xyz pointer? I would think that as long as
xyz is on a word boundary then the pointer returned to the character array
would not have to be aligned since they are only characters. Right?
The 'C' programmers (K&R) manual says (speaking of bit fields):
" ... an unnamed field with a width of 0 specifies alignment of the
next field at word boundary. The "next field" presumably is a field,
not an ordinary structure member, (***) because in the latter case
the alignment would have been automatic (***)."
I would not think that this would apply to malloc's since a character
pointer can point to a an odd address and not matter since characters
are contiguous. Does malloc() know the object that it's working on?
Do I need to know what processor I'm on and how much extra memory
I need to allocate so I can sloppily adjust the struct pointer forward to
the even address I need? How do I get xyz into an even word address?
Can someone please tell me a set of rules to follow for each processor
(ie. 3B && 68K etc.) ?!
I would hate to think that I would have to take the (*) out from
in front of the lines array and let the compiler handle it! There's
a whole lot of memory that I would not be used at any one time.
Proposed partial answer: use unions to get alignment.
union aln_int { char c; int x; };
union aln_ptr { char c; char *xyz; };
union aln_dbl { char c; double d; };
struct line {
char n[81];
union aln_dbl abc;
union aln_int x,
y,
z;
union aln_ptr xyz;
} *lines[MAXLINES];
Does this help at all? Or is malloc still going to give me a problem?
Thanks in advance,
Vaughn Vernon
AT&T ISL
Denver, CO
ihnp4!drutx!drux1!vlv
I will post answers to the net.
Unix is AT&T's Trademark
VAX is Digital's Trademark
All that disclaimer stuff. Besides, my intelligence is artificial!
More information about the Comp.lang.c
mailing list