home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
- From: dkeisen@leland.Stanford.EDU (Dave Eisen)
- Subject: Re: Memory functions - memcpy & memmove
- Message-ID: <1992Nov7.165047.9553@leland.Stanford.EDU>
- Sender: news@leland.Stanford.EDU (Mr News)
- Organization: Sequoia Peripherals, Inc.
- References: <BxArt0.9pE@murphy.com> <BxB88A.1Au@portal.hq.videocart.com>
- Date: Sat, 7 Nov 92 16:50:47 GMT
- Lines: 39
-
- In article <BxB88A.1Au@portal.hq.videocart.com> dfuller@portal.hq.videocart.com (Dave Fuller) writes something like this about memmove:
-
- if (source > dest) {
- for(;size > 0; size --)
- *dest++ = *source++;
- } else {
- for(size --; size >= 0; size --)
- dest[size] = source[size];
- }
-
- I'd recommend the same thing with the caveat that this is not ANSI
- standard code and will not work on some systems. In fact, there is
- no reasonable way to write memmove in standard C; this is why the
- standard added this function.
-
- The problem with the above code is that it is not legal to compare
- two pointers (except for equality) unless the pointers point into
- the same object. Such comparisons give undefined behavior which
- means it can do anything at all it wants.
-
- That being said, I wouldn't worry too much about this problem. On
- any reasonable system (except one that is specifically designed
- to hunt down non-portabilities in code) this should work. All that
- is required is that the computer always return *some* answer when
- comparing two pointers, that it not core dump or explode or something.
-
- Because if the two pointers point into the same object the above
- code works. That is, after all, what it was designed to do. And if
- the two pointers don't point into the same object, then there is
- no problem with overlapping regions and you can copy either way
- you want to. Either way, you're covered.
-
-
-
- --
- Dave Eisen Sequoia Peripherals: (415) 967-5644
- dkeisen@leland.Stanford.EDU Home: (415) 321-5154
- There's something in my library to offend everybody.
- --- Washington Coalition Against Censorship
-