cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/13/13:47:31

From: Kohn Emil Dan <emild AT cs DOT technion DOT ac DOT il>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c
Subject: Re: Copying memory to memory problems
Date: Mon, 13 Jul 1998 19:31:36 +0300
Organization: Technion, Israel Institute of Technology
Lines: 50
Message-ID: <Pine.GSO.3.95-heb-2.07.980713191456.14414C-100000@csd>
References: <35a7cf26 DOT 7861242 AT news DOT unisys DOT com DOT br>
NNTP-Posting-Host: csd.cs.technion.ac.il
Mime-Version: 1.0
In-Reply-To: <35a7cf26.7861242@news.unisys.com.br>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp



Hi!

On Sat, 11 Jul 1998, Bruno Barberi Gnecco wrote:

> I need to move a part of an array to some bytes ahead. I'd do it using:
>  for (i = size; i > x; i--) 
>      array[i+1] = array[i];
> But this is too slow. How can I do it copying directly from memory to memory? When
> I want to move a part of an array back, I'm using:
>   memcpy(&numbers[5], &numbers[7], n*sizeof(int));

Calling memcpy() when the source and destination memory areas overlap
invokes undefined behavior. You should use memmove() instead. It has the
same prototype like memcpy() but it is safe to use when the source and
destination areas overlap.

> instead of
>  for (i = x; i < size; i++) 
>      array[i] = array[i+1];
> 
> Is there a backwards memcpy available? If not, what would be the fastest way?

The  documentation of memcpy() does not specify how bytes are copied from
source to destination. And it does not need to as the source and
destination objects must not overlap. So the notion of forward or backward
version of memcpy() does not make any sense.

On the other hand memmove() guarantees that the copying process takes
place as if the source was copied to a temporary area that does not
overlap with any of the parameters and then the object is copied from the
temporary area back to the destination. How exactly this is achieved is a
matter of implementation and a quality implementation should provide a
version optimized for your particular system

Hope this helps.

						Best regards
								Emil

> Please note that the number of bytes to move ahead will not be constant...
> 
> "There's never enough time to do all the nothing you want" Bill Watterson 
> Bruno Barberi Gnecco <brunobg AT geocities DOT com> ICQ #1383173 - PGP 5.0i user 
> My other OS is Linux -=O=- Check my homepage at http://graphx.home.ml.org 
> 
> 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019