Valgrind에서 alloc 갯수가 다릅니다.

ijs0430의 이미지

valgrind에서 아주 간단한 코드를 작성했어요.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   int i;
   char *ch;
 
   i=100;
   printf("%d", i");
   ch=malloc(10);
   return(0);
}

작성해서 컴파일 하고 valgrind 돌려서 leak 부분 확인해보았는데
heap summary 부분에서 보면 계속 total heap usage : 2 allocs, 1 frees 라고 뜹니다.
저는 malloc 해준 부분이 한 군데 밖에 없는데 free는 당연히 안해주었고
그래서 1 alloc, 0 free라고 떠야 맞다고 생각하는데
이부분에 대해서 아시는 분 자세히 설명좀 부탁드립니다.

김정균의 이미지

일단 올려주신 코드에 syntax error 있고요.

[root@an3 shm]$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@an3 shm]$ rpm -q valgrind
valgrind-3.11.0-24.el7.x86_64
[root@an3 shm]$
[root@an3 shm]$ valgrind --tool=memcheck --leak-check=full --leak-resolution=high ./a.out
==27787== Memcheck, a memory error detector
==27787== Copyright (C) 2002-2015, and GNU GPL''d, by Julian Seward et al.
==27787== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==27787== Command: ./a.out
==27787==
100==27787==
==27787== HEAP SUMMARY:
==27787==     in use at exit: 10 bytes in 1 blocks
==27787==   total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==27787==
==27787== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==27787==    at 0x4C27BE3: malloc (vg_replace_malloc.c:299)
==27787==    by 0x4005A9: main (in /dev/shm/a.out)
==27787==
==27787== LEAK SUMMARY:
==27787==    definitely lost: 10 bytes in 1 blocks
==27787==    indirectly lost: 0 bytes in 0 blocks
==27787==      possibly lost: 0 bytes in 0 blocks
==27787==    still reachable: 0 bytes in 0 blocks
==27787==         suppressed: 0 bytes in 0 blocks
==27787==
==27787== For counts of detected and suppressed errors, rerun with: -v
==27787== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
[root@an3 shm]$

이렇게 나오네요.

ijs0430의 이미지

valgrind --leak-check=full --leak-resolution=high ./a.out
==19779== Memcheck, a memory error detector==19779== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==19779== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info==
19779== Command: ./a.out==19779== 100==19779== ==19779== HEAP SUMMARY:==19779== in use at exit: 10 bytes in 1 blocks==19779==

total heap usage: 2 allocs, 1 frees, 1,034 bytes allocated==19779== ==19779== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1==19779== at 0x402E23C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)==19779== by 0x804846F: main (in /home/tos/a.out)==19779== ==19779== LEAK SUMMARY:==19779== definitely lost: 10 bytes in 1 blocks==19779== indirectly lost: 0 bytes in 0 blocks==19779== possibly lost: 0 bytes in 0 blocks==19779== still reachable: 0 bytes in 0 blocks==19779== suppressed: 0 bytes in 0 blocks

백연구원의 이미지

파일 저장을 다시 해서 컴파일 해보시고 테스트 해보세요. 그리고 동일한 증상이 발생하면 코드 새로 올려주셔야 합니다. 올려주신 소스와 @ijs0430 님이 테스트 하는 코드가 다를겁니다.


소곤소곤

ymir의 이미지

http://stackoverflow.com/questions/38165164/valgrind-stdio-hs-function-puts-allocates-memory

glibc-2.22 부터 printf 와 같이 stdout 으로 출력할 때..
내부적으로 mmap 대신 malloc 을 사용하도록 바뀌었다는 것 같습니다.

glibc-2.19 를 쓰는 x86_64 와 arm 에서는 malloc 을 사용하지 않는데..
glibc-2.23 을 쓰는 i686 에서는 malloc 이 한 번 발생하네요.
printf 대신 fputs, puts 로 바꿔도 마찬가지입니다.

아래는 glibc-2.19 (x86_64).. (arm 도 비슷)

(gdb) bt
#0  mmap64 () at ../sysdeps/unix/syscall-template.S:81
#1  0x00007ffff7a7f82c in __GI__IO_file_doallocate (fp=0x7ffff7dd4400 <_IO_2_1_stdout_>) at filedoalloc.c:128
#2  0x00007ffff7a8d4b5 in __GI__IO_doallocbuf (fp=fp@entry=0x7ffff7dd4400 <_IO_2_1_stdout_>) at genops.c:418
#3  0x00007ffff7a8c868 in _IO_new_file_overflow (f=0x7ffff7dd4400 <_IO_2_1_stdout_>, ch=-1) at fileops.c:837
#4  0x00007ffff7a8b5b1 in _IO_new_file_xsputn (f=0x7ffff7dd4400 <_IO_2_1_stdout_>, data=<optimized out>, n=4) at fileops.c:1332
#5  0x00007ffff7a5bd10 in _IO_vfprintf_internal (s=0x7ffff7dd4400 <_IO_2_1_stdout_>, format=0x4005e4 "n = %d\n", ap=ap@entry=0x7fffffffde08) at vfprintf.c:1340
#6  0x00007ffff7a663d9 in __printf (format=<optimized out>) at printf.c:33
#7  0x0000000000400550 in main () at mal.c:7

이건 glibc-2.23 (i686) ..

(gdb) bt
#0  __brk (addr=0x0) at ../sysdeps/unix/sysv/linux/i386/brk.c:33
#1  0xb7ee4f62 in __GI___sbrk (increment=135168) at sbrk.c:43
#2  0xb7e7923c in __GI___default_morecore (increment=135168) at morecore.c:47
#3  0xb7e74a3d in sysmalloc (nb=nb@entry=1032, av=av@entry=0xb7fb8780 <main_arena>) at malloc.c:2482
#4  0xb7e75829 in _int_malloc (av=av@entry=0xb7fb8780 <main_arena>, bytes=bytes@entry=1024) at malloc.c:3825
#5  0xb7e76e45 in __GI___libc_malloc (bytes=1024) at malloc.c:2911
#6  0xb7e76e7a in __GI___libc_malloc (bytes=1024) at malloc.c:2907
#7  0xb7e6392e in __GI__IO_file_doallocate (fp=0xb7fb8d60 <_IO_2_1_stdout_>) at filedoalloc.c:127
#8  0xb7e711c7 in __GI__IO_doallocbuf (fp=0xb7fb8d60 <_IO_2_1_stdout_>) at genops.c:398
#9  0xb7e70561 in _IO_new_file_overflow (f=0xb7fb8d60 <_IO_2_1_stdout_>, ch=-1) at fileops.c:820
#10 0xb7e6f684 in _IO_new_file_xsputn (f=0xb7fb8d60 <_IO_2_1_stdout_>, data=0x8048600, n=6) at fileops.c:1331
#11 0xb7e64dfc in __GI__IO_fwrite (buf=0x8048600, size=1, count=6, fp=0xb7fb8d60 <_IO_2_1_stdout_>) at iofwrite.c:39
#12 0x08048528 in main () at mal.c:10

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.