C언어

valgrind test

고요한하늘... 2014. 6. 3. 10:56

bounds checker 와 valgrind 성능 차이 얘기중에

아래와 같은 케이스에 valgrind가 리포팅이 되는지 확인하고자 테스트


확인할 부분 : 

* 0~7까지의 char 배열을 선언하고

-1을 접근하는 경우와

7을 넘어서는 접근에 대한 포인터 연산에 대해 리포팅


#include <string.h>

#include <stdio.h>

#include <stdlib.h>


void pointer_test( char *p)

{

    int i;


    *(p - 1) = '\0';

    for( i = 0; i <= 8; i++ )

        *p++ = '*';

    *p = '\0';

}


int main()

{

    char *p;

    p = malloc(sizeof(char)*8);


    pointer_test(p);

    free(p);

}


*(p-1) 에 대한 메세지

==22168== Invalid write of size 1

==22168==    at 0x4004E8: pointer_test (test.c:9)

==22168==    by 0x400531: main (test.c:20)

==22168==  Address 0x4c2303f is 1 bytes before a block of size 8 alloc'd

==22168==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)

==22168==    by 0x400524: main (test.c:18)


*p++ = '*';  에 대한 메세지
==22168== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
==22168== Invalid write of size 1
==22168==    at 0x4004F8: pointer_test (test.c:11)
==22168==    by 0x400531: main (test.c:20)
==22168==  Address 0x4c23048 is 0 bytes after a block of size 8 alloc'd
==22168==    at 0x4A05E1C: malloc (vg_replace_malloc.c:195)
==22168==    by 0x400524: main (test.c:18)


'C언어' 카테고리의 다른 글

const 포인터( pointer )  (0) 2014.06.25
suggest parentheses around assignment used as truth value  (0) 2014.06.11
memchr vs strchr  (0) 2014.04.15
C언어 고급기능  (0) 2014.02.20
GNU LIB 관련 링크  (0) 2014.01.28