delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/04/04:43:24

From: mail AT address DOT in DOT signature DOT net (Peter Karp)
Newsgroups: comp.os.msdos.djgpp
Subject: scope of auto vars and debugging problem
Date: Mon, 04 Jan 1999 09:34:23 GMT
Organization: Regional Computing Center, University of Cologne
Lines: 123
Message-ID: <369087e4.2704427@news.rrz.uni-koeln.de>
NNTP-Posting-Host: port54.dialin.fh-koeln.de
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi,
I'm posting here with a newbie question, which I think is related to
the behavior of the compiler/debugger.

I'm still having some trouble with debugging the programs and with the
understanding of auto vars.

I don't understand why the program count2 outputs
1 2 3 4 5 6 7 8 9 10

Here's the source of count2:

#include <iostream.h>

short counter()
{
    short count;
 
    count ++;
 
    cout << count << " ";

    return 0;
}
 
int main()
{
    short i;
 
    for (i = 0; i < 10; i ++)
        counter();

    return 0;
}

I would expect that it gives an arbitrary value and increases this
value *each round*, because an auto variable (not initalized by
default - this means not initalized at linking time and not
initialized at the function call, until a specific value is assigend
to that var. Is this right?) will have an arbitrary value at the
beginning of the function call, but the function counter will add 1 to
this value. Then when the memory address is not used till the next
function call it will hold the last value, although it could happen
that the value would be overwritten, when everything goes bad?


Here's the copy of tracing count3 (same program but using
static short count=0
for the variable declaration), which works like expected:


___________________________________
C:\c_quell\normal>trace count3
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for
details.
GDB 4.12 (go32), Copyright 1994 Free Software Foundation, Inc...
Breakpoint 1 at 0x158b: file count3.cc, line 18.

Breakpoint 1, main () at count3.cc:18
18          for (i = 0; i < 10; i ++)
(gdb) z
19              counter();
(gdb)
21          return 0;
(gdb)
22      }
(gdb)
0xe2bb in __crt1_startup ()
(gdb)
Single stepping until exit from function __crt1_startup,
which has no line number information.
1 2 3 4 5 6 7 8 9 10 go32_close called
(gdb)
______________________________________



and here the copy of tracing count2:




________________________________________
C:\c_quell\normal>trace count2
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for
details.
GDB 4.12 (go32), Copyright 1994 Free Software Foundation, Inc...
Breakpoint 1 at 0x1587: file count2.cc, line 18.

Breakpoint 1, main () at count2.cc:18
18          for (i = 0; i < 10; i ++)
(gdb) z
19              counter();
(gdb)
21          return 0;
(gdb)
22      }
(gdb)
0xe2b7 in __crt1_startup ()
(gdb)
Single stepping until exit from function __crt1_startup,
which has no line number information.
256 256 256 256 256 256 256 256 256 256 go32_close called
(gdb)
______________________________________________

The trace shows 10 times 256. Why is this happening here, while
running the program always seem to output 1 2 3 4 5 6 7 8 9 10, which
seems scary to me too?

I guess I'm missing an important point here...
Is the tracing working o.k. here, or what does happen here?

Thank you very much for your help!

Greetings from Cologne
Peter

E-Mail: karpfenteich AT gmx DOT de

- Raw text -


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