Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!orchid!kgdykes From: kgdykes@orchid.UUCP Newsgroups: comp.sys.ibm.pc Subject: Re: Turbo C memset problem Message-ID: <11701@orchid.waterloo.edu> Date: Fri, 13-Nov-87 05:47:08 EST Article-I.D.: orchid.11701 Posted: Fri Nov 13 05:47:08 1987 Date-Received: Sat, 14-Nov-87 18:34:02 EST References: <238@cunixc.columbia.edu> Reply-To: kgdykes@orchid.waterloo.edu (Ken Dykes) Organization: S.D.G. UofWaterloo Lines: 49 In article <238@cunixc.columbia.edu> nwc@cunixc.columbia.edu (Nick Christopher) writes: > >I had code that was very happy in the small model in which I blanked and >character array using memset as follows: > >char string[80] > >memset(string,NULL,80); > >however when I moved to large model this no longer worked, it began to have >the exact same 3 garbage characters at the begining of string. Is this the >fault of my format or the memset's fault. > >nwc%cunixc@columbia, columbia!cunixc!nwc BITNET: nwcus@cuvma Probably your fault. NULL is actually a POINTER, not a zero-value. most or would define it as either (void *)0 or (char *)0 but they are also free on some machines to use MAGIC VALUES. In your situation, it worked in the normal/small model because a null-small pointer happens to be a value of 0 with the sizeof(int) But when you go to large model, the NULL pointer includes segment-info or something (the same garbage chars you see), and also the size is no longer the sizeof(int) but probably twice the size. (1word for segment, 1 word for offset) If you checked futher, you would find that you werent getting 80 bytes processed either. A hypothetical stack frame for the working (small) model: would be: memset(ptr,NULL,80) --> | 1word-ptr | 1word-ptr-of-0 | 1word=80 | For the large model: memset(ptr,NULL,80) --> |2wd-ptr(seg+offset)|2wd-ptr(seg+offset)=NULL|1wd=80| then the code of memset assumes the 2nd arg is only a char-value (int actually) and scoops up the first part of the far-ptr-NULL as the "value" and the second part of the far-ptr-NULL as the 3rd-argument "length" to process. And the 80-value is simply not noticed. What you need to say is: memset(ptr, 0, 80); and only use NULL for POINTER usage, not CHAR or INT. In the modern age we are not allowed to assume that NULL equals ZERO The above theories do not come from DOS/turbid experience, but my general C compiler viewpoint. So i will protect myself by attaching the word "probably" to the above statements. What you should really say to work is: memset(ptr, 0, 80); -Ken -- - Ken Dykes, Software Development Group, U.of.Waterloo kgdykes@watmath.uucp kgdykes@water.bitnet kgdykes@waterloo.csnet