jfjr@mitre-bedford.arpa (Freedman) (04/15/88)
I've asked similar questions so if this sound familiar please
bear with me.
Suppose I have some (rather large) type and I want to
dynamically allocate a (rather large) array of said type.
The natural way to do this is sort like this
malloc(some_large_integer*(sizeof(rather_large_type))).
But given some well known architectures this ties the
compiler hands - it must allocate contiguous storage.
Alternatively you could declare
typedef rather_large_type large_array[some_large_integer];
but I haven't had much luck with malloc(sizeof(large_array))
Now I can do this
typedef struct {
int dummy_field;
large_array large;
} large_structure;
malloc(sizeof(large_structure))
this seems to work and looks more portable than anything else but
its not pretty. Have I missed something??
signed
Continuously Confused
Jerry Freedman, Jr "Love is staying up all night
jfjr@mitre-bedford.arpa with a sick child,
(617)271-4563 or a healthy adult"
chris@mimsy.UUCP (Chris Torek) (04/15/88)
In article <12983@brl-adm.ARPA> jfjr@mitre-bedford.arpa (Freedman) writes: > Suppose I have some (rather large) type and I want to >dynamically allocate a (rather large) array of said type. > >malloc(some_large_integer*(sizeof(rather_large_type))). > >But given some well known architectures this ties the >compiler hands - it must allocate contiguous storage. All arrays are contiguous in C, by definition. As long as you are going to allocate a large array, it is going to take a big chunk of contiguous memory. >[or] typedef rather_large_type large_array[some_large_integer]; > >but I haven't had much luck with malloc(sizeof(large_array)) This should have precisely the same effect as before. >[or] typedef struct { > int dummy_field; > large_array large; > } large_structure; > >malloc(sizeof(large_structure)) This should just allocate somewhat *more* space than the previous two, and still require contiguous storage. If you are stuck with an 80[12]?86 (egrep syntax), you will have to use `huge model' to get arrays of >=65536 bytes. The only alternative is to break up the data structure so that each piece is < 65536 bytes. Think of the IBM PC as a bunch of very slow PDP-11s sharing memory. :-) -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
pablo@polygen.uucp (Pablo Halpern) (04/20/88)
From article <12983@brl-adm.ARPA>, by jfjr@mitre-bedford.arpa (Freedman): > Alternatively you could declare > > typedef rather_large_type large_array[some_large_integer]; > > but I haven't had much luck with malloc(sizeof(large_array)) > > Now I can do this > > typedef struct { > int dummy_field; > large_array large; > } large_structure; > > malloc(sizeof(large_structure)) > > this seems to work and looks more portable than anything else but > its not pretty. Have I missed something?? Your first alternative: typedef rather_large_type large_array[some_large_integer]; pointer = (rather_large_type *) malloc(sizeof(large_array)); SHOULD work but doesn't on some compilers which don't understand that an array is NOT a pointer, an array is CONVERTED to a pointer when indexed or de-referenced. Assuming you can't change to a better compiler, I suggest a slight varient on your second attempt: typedef struct { large_array large; char dummy_field; } large_structure; pointer = (rather_large_type *) malloc(sizeof(large_structure)); That way, you can simply say: pointer[index] instead of pointer->large[index] (Every compiler I know of the address of a struct is the same as the address of its first element. dpANS requires this.) However, I don't understand your objection to: pointer = (rather_large_type *) malloc(some_large_integer * sizeof(rather_large_type)); Which seems the same to me. Pablo Halpern | mit-eddie \ Polygen Corp. | princeton \ !polygen!pablo (UUCP) 200 Fifth Ave. | bu-cs / Waltham, MA 02254 | stellar /
meissner@xyzzy.UUCP (Michael Meissner) (04/27/88)
In article <143@polygen.UUCP> pablo@polygen.uucp (Pablo Halpern) writes: | (Every compiler I know of the address of a struct is the same as the address | of its first element. dpANS requires this.) | However, I don't understand your objection to: | | pointer = (rather_large_type *) malloc(some_large_integer * | sizeof(rather_large_type)); | | Which seems the same to me. No, no, no, no. A pointer to a structure is not the same as the pointer to it's first element because the types are different. If you convert them to the same type, I would agree then that the pointers are the same. On machines which have different representations for pointers (say a pointer to a word and a pointer to a byte), if the first member of the structure is a char or char array, the member's address would be a byte pointer, whereas the structure's address would be a word pointer. -- Michael Meissner, Data General. Uucp: ...!mcnc!rti!xyzzy!meissner Arpa/Csnet: meissner@dg-rtp.DG.COM