_debug_memcpy - バイトのコピー

形式

#include <string.h>
void *_debug_memcpy(void *dest, const void *src, size_t count, const char *file,
                    size_t line);

目的

これは memcpy のデバッグ・バージョンです。 memcpy と同様、srccount バイトを dest にコピーします。 ここで、オーバーラップしたオブジェクト間でコピーすると、未定義な振る舞いを引き起こすことになります。

_debug_memcpy は、コピー先にバイトをコピーしたのちにヒープを妥当性検査して、 コピー先がヒープ内にある場合にのみこのチェックを行います。 _debug_memcpy は、暗黙の _heap_check 呼び出しを 行います。 _debug_memcpy が、_heap_check を呼び出したときに破壊されたヒープを検出すると、 _debug_memcpy はそのファイル名 file と行番号 line をメッセージで報告します。

戻り値

ポインターを dest へ戻します。

次の例には、プログラミング・エラーがあります。 ターゲット・ロケーションを初期化するために使用される memcpy 呼び出しでは、カウントがターゲット・オブジェクトのサイズを超えているため、 memcpy 操作は割り当てられたオブジェクトの最後を超えてバイトをコピーします。

/*   _debug_memcpy.c  */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define  MAX_LEN       10

int main(void)
{
   char *source, *target;

   target = (char*)malloc(MAX_LEN);
   memcpy(target, "This is the target string", 11);

   printf("Target is ¥"%s¥"¥n", target);
   return 0;

}

出力は以下に類似した表示になります。

1546-503 End of allocated object 0x2001D9B0 was overwritten at 0x2001D9BA.
1546-514 The first eight bytes of the object (in hex) are: 5468697320697320.
1546-519 This object was (re)allocated at line 12 in _debug_memcpy.c.
               _debug_umalloc + D0
          _debug_umalloc_init + 3C
                _debug_malloc + 5C
                         main + 24
1546-512 Heap state was valid at line 12 in _debug_memcpy.c.
           _int_debug_umalloc + 54
               _debug_umalloc + D0
          _debug_umalloc_init + 3C
                _debug_malloc + 5C
                         main + 24
1546-511 Heap error detected at line 13 in _debug_memcpy.c.
1546-522 Traceback:
           d0c018a4 = _debug_memcpy + 0x50
           100003bc = main + 0x48