gdb 관련문의

kikle1303의 이미지

안녕하세요. gdb 공부하다 문의드립니다.
(참고로 12라인코드가 문제 있습니다)

아래 코드에서 12라인의 if(num=0) 을 통과하게 되면 num 값이 0으로 변경되야 할것으로 이해되는데
GCC 사용했을시 16라인까지 넘어가도 num값이 그대로 1입니다. --;
Visual studio에서 확인시에는 12라인 통과시 num값이 0으로 변경되는것 확인했습니다.
제가 뭘 놓치고 있는것 일까요.
왜 GCC로 컴파일했을시 12라인에서 num이 0으로 할당이 안되는지 조언 부탁드립니다.

감사합니다.
-------------------------
1 #include
2 int main() {
3 int count;
4 int fac;
5 for (count = 1; count < 10; count++) {
6 fac = faculty(count);
7 printf("count: %u, fac: %u\n", count, fac);
8 }
9 return 0;
10 }
11 int faculty(int num) {
12 if (num = 0) {
13 return 1;
14 }
15 else {
16 return num * faculty(num - 1);
17 }
18}
-------------------------

라스코니의 이미지

음.. 제 생각에는

첫째,,, if(num = 0) 이라는 코드는 써서는 안되는 코드로 보이고, 따라서 왜 num 이 0, 1 이 되는지 고민할 필요가 없다고 보여지고

둘째,,, 혹시 정말 불가결한 이유에 의해서 써야 된다고 하더라도 함수 파라미터로 받은 것을 바로 쓴다는 것이 걸리고

int t = num;
if( t = 0 ) 이런 식으로 쓰는게 좋음

이라고 생각되네요.

kikle1303의 이미지

답글 감사합니다.
if(num=0) 는 디버깅을 위한 trace를 위해 일부러 작성된 코드입니다. num 값을 따라가면서 변경되는지 확인해 볼려구요.
감사합니다~

dgkim의 이미지

질문의도는 == 비교 대신에 = 대입을 하면 문제가 발생하지 않냐는 의도로 올리신 것 같은데,

저도 gcc 특징까지 알려드릴 내공은 없네요.

일반 문장으로 대입을 해도 안되는 것 같네요.
(즉, if 구문 앞 문장으로 대입문만 수행해도)

포인터로도 한번 테스트 해보시지요.

--
http://www.dgkim.net/

kikle1303의 이미지

네 저도 일반 문장으로 대입을 했는데도 gdb 상에서는 변경이 없었습니다.
아래분 결과를 봐서는 실행시에는 예상했던대로 정상동작하는데
gdb로 trace시 값이 정상적으로 표시가 안되는것 처럼 보입니다.
감사합니다.

익명 사용자의 이미지

아래 코드를 실행시키고 결과를 알려주세요.

#include<stdio.h>
int faculty(int num)
{
    if (num = 0) {
        fprintf( stderr, "iftrue num=%d\n", num );
        return 1;
    }
    else {
        fprintf( stderr, "iffalse num=%d\n", num );
        return 0;
    }
}
int main()
{
    faculty(1);
    return 0;
}

$ gcc -o a a.c -Wall
a.c: In function 'faculty':
a.c:5:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (num = 0) {
 
$ ./a
iffalse num=0
 
$ gcc --version
gcc (GCC) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
kikle1303의 이미지

안녕하세요.
확인결과 님께서 하신 결과와 같이 'iffalse num=0' 이 나옵니다만
아래와 같이 gdb로 step 실행시 display num 했을시 값은 1에서 변경이 없네요.
gdb 연동 문제인지.. --;

감사합니다.
------------------------------------------------------------------------------
p10636@ubuntu:~/workspace/kldp$ cat a.c
#include
int faculty(int num)
{
if (num = 0) {
fprintf( stderr, "iftrue num=%d\n", num );
return 1;
}
else {
fprintf( stderr, "iffalse num=%d\n", num );
return 0;
}
}
int main()
{
faculty(1);
return 0;
}
p10636@ubuntu:~/workspace/kldp$ gcc a.c -o a -Wall -g
a.c: In function ‘faculty’:
a.c:4:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

p10636@ubuntu:~/workspace/kldp$ gdb a
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/p10636/workspace/kldp/a...done.
(gdb) b main
Breakpoint 1 at 0x8048468: file a.c, line 15.
(gdb) r
Starting program: /home/p10636/workspace/kldp/a

Breakpoint 1, main () at a.c:15
15 faculty(1);

(gdb) s
faculty (num=1) at a.c:4
4 if (num = 0) {

(gdb) display num
1: num = 1

(gdb) s
9 fprintf( stderr, "iffalse num=%d\n", num );
1: num = 1

(gdb) s
iffalse num=0
10 return 0;
1: num = 1

(gdb) s
12 }
1: num = 1

(gdb) s
main () at a.c:16
16 return 0;

(gdb) s
17 }

(gdb) s
0xb7e3b4d3 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6
(gdb)

kikle1303의 이미지

혹시나 싶어 gcc 버전도 같이 올려봅니다.

p10636@ubuntu:~/workspace/kldp$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

익명 사용자의 이미지

익명 사용자의 이미지


추가로
int faculty(int num) 를 아래로 바꿔서 해보세요~
int faculty(volatile int num)

kikle1303의 이미지

감사합니다.
알려주신 블로그 내용은 이해가 되었는데, GDB 계속 사용시마다 의심이 계속 갈거 같습니다. --;

추가로
int faculty(volatile int num) 일시 GDB에서 정상 동작 확인했습니다.
volatile이 해당대상에 대해 compile 최적화 하지 말라는 내용으로 이해하고 있는데, gcc -O0로 컴파일일때와
동작에 차이가 있는게 이해가 잘 안됩니다.(gcc -O0 옵션으로 컴파일시에도 GDB에서 정상 동작하지 않았습니다)
공부할게 참 많은거 같습니다.
감사합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.