Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site drux1.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!mtuxo!drutx!drux1!vlv From: vlv@drux1.UUCP (Vaughn Vernon) Newsgroups: net.unix,net.lang.c Subject: Portablity using structures and malloc - Help Message-ID: <81@drux1.UUCP> Date: Wed, 17-Jul-85 17:18:06 EDT Article-I.D.: drux1.81 Posted: Wed Jul 17 17:18:06 1985 Date-Received: Thu, 18-Jul-85 08:07:12 EDT Organization: AT&T Information Systems Laboratories, Denver Lines: 84 Xref: watmath net.unix:5059 net.lang.c:5632 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!