gold@sdsu.UUCP (Dan Gold) (12/16/87)
I having a problem allocating memory with malloc. It's running on an Zenith 248 w/ SCO 2.1.3 O.S., and 2.1 develop. sys ( me thinks). Machine has 3Mb RAM. the problem is malloc returns negative when the address is > 32K. I tried the following code: #include<stdio.h> struct message { char foo[260]; } *msgs[900]; main() { int i; for (i=0;i < 900;i++) { msg[i]=(struct message *)malloc(sizeof(struct message)); printf("i= %d, msg[i] = %ld\n",i,msg[i]); } } malloc returns up to 326xx fine, then turns over and I get -32xxx. I've compiled the code with "cc -M2e" and the memory model flag, either large or huge. I've tried both, used various combos of "far" in places, nothing seems to work. I know the memory isn't linear addressing, but I don't think a valid pointer is too much to ask. I also know the O.S and compiler may be behind, but this we're going to get a 386 (sometime soon, prob. w/SCO 386), and don't want to update if we're not going to need it. (OR esp if it won't help.) I called SCO and the software support basically said get the latest update, there were some bug fixes. Were they relevant to this? In the meantime, anyone got any ideas ? thanx, Dan UUCP: {...sdcsvax,ucsdhub}!sdsu!gold ARPA:(almost) : sdsu!gold@sdcsvax Disclaimer: Real microprocessors don't do segmentation.
chris@mimsy.UUCP (Chris Torek) (12/16/87)
In article <2866@sdsu.UUCP>, gold@sdsu.UUCP (Dan Gold) writes:
-#include<stdio.h>
-struct message {
- char foo[260];
-} *msgs[900];
-
-main()
-{
- int i;
- for (i=0;i < 900;i++)
- {
- msg[i]=(struct message *)malloc(sizeof(struct message));
- printf("i= %d, msg[i] = %ld\n",i,msg[i]);
- }
-}
By calling malloc without first declaring it, you implicitly
declare it (int). (Shades of FORTRAN!) Try putting in a
char *malloc();
somewhere before the first call.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
jpp@slxsys.specialix.co.uk (John Pettitt) (12/19/87)
In article <2866@sdsu.UUCP> gold@sdsu.UUCP (Dan Gold) writes: >I having a problem allocating memory with malloc. >the problem is malloc returns negative when the address is > 32K. >I tried the following code: >#include<stdio.h> >struct message { char foo[260]; } *msgs[900]; >main() >{ > int i; > for (i=0;i < 900;i++) > { > msg[i]=(struct message *)malloc(sizeof(struct message)); > printf("i= %d, msg[i] = %ld\n",i,msg[i]); > } >} malloc returns a pointer - you are asking printf to print a long int and a signed one at that ! Pointers don't have a sign, there is no problem with malloc - just the printf doing what you told it to do. P.S. pointers are 16 bit unless you use the -M flag to say otherwise and they are ALWAYS unsigned I A M A L O N G M S G -- John Pettitt UUCP :{backbone}!mcvax!ukc!pyrltd!slxsys!jpp Specialix Systems Domain :jpp%slxsys@pyra.co.uk London, UK. (Where else ? :-) Voice : +44 1 398 9422 (GMT) rn: core dumped