From: "Edmund Horner" Newsgroups: comp.os.msdos.djgpp References: Subject: Re: memset behavior Lines: 52 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 Organization: Paradise Net Message-ID: <967089732.935725@shelley.paradise.net.nz> Cache-Post-Path: shelley.paradise.net.nz!unknown AT 203-96-154-214 DOT tnt7 DOT paradise DOT net DOT nz X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) Date: Thu, 24 Aug 2000 16:03:37 +1200 NNTP-Posting-Host: 203.96.152.26 X-Complaints-To: newsadmin AT xtra DOT co DOT nz X-Trace: news.xtra.co.nz 967089752 203.96.152.26 (Thu, 24 Aug 2000 16:02:32 NZST) NNTP-Posting-Date: Thu, 24 Aug 2000 16:02:32 NZST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "Radical NetSurfer" wrote in message news:s269qsgaqr5446ot429cat1q8vo7rq5t9c AT 4ax DOT com... > I had a line of code like this: > > char buffer[100]; > printf("%d %s\n", (char*)memset(buffer, '-', 64)); > > and what was discovered is: > > IF buffer already contains a string, "ABCD", > THEN memset() as above printed: > > "-------------------------------------------------------------BCD" > (truncated to fit here) > > OBSERVE: that the everything was MOVED DOWN > THEN: the dashed-line added. > > QUESTION: Why would this be ? You're in big trouble here! 1. Your printf comes with %d and %s, but you only provide one argument (memset's return value)! Perhaps the %d was a typo. 2. You can't rely on being able to use blocks of memory as strings! The results will depend on the next 0 byte encountered, and unless you're explicitly going to have one, your string could be any length at all -- and your program will misbehave. Thanks to the above, there is no defined behaviour for this program. > PROBLEM #2: > There's no such thing as a memcat() routine... > why? How can binary data (data containing 0x00) be > CONCATED to accurately and properly return the > size (pointer growth) of a buffer? > > (growing_pointer *)memcat(destination, source, n_bytes); > > such that "growing pointer" is simply something that provides > the buffer address of 'source', and its length. > > Any ideas here? Since a pointer doesn't know anything about the size of the data it's pointing to, memcat would probably need to use a terminating 0 byte -- just like strcat does. Try strcat. Better still -- figure out exactly what it is you want to do with your data! Edmund.