Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Where is the value getting initialized in this case

Reply
Thread Tools

Where is the value getting initialized in this case

 
 
Chad
Guest
Posts: n/a
 
      09-18-2007
In the following code snippet, the value of 'lin' is somehow gettng
intialized to the value of 1.

int main(argc, argv)
int argc;
char **argv;
{
int err, tflg = 0, wflg = 0, slen, wr,
sum = 0, in, rflg = 0, i, j, lin, waiting = 0;
char str[256];

memset(str, '\t', sizeof (str));

slen = sizeof (str);

while ((err = getopt(argc, argv, "twrp:")) != -1)
switch (err) {
case 't':
tflg = 1;
strncpy(tsk[0].u.ut_line, optarg, sizeof
(tsk[0].u.ut_line));
break;

case 's':
strncpy(str, optarg, sizeof (str));
slen = strlen(str);
break;

case 'w':
wflg = 1;
break;

case 'r':
rflg = 1;
break;

case 'p':
#ifndef BSD
fprintf (stderr, "warn: this feature isn't
available"
"under this OS\n");
exit (1);
#endif
ptitle (optarg);
break;

case '?':

default :
usage();
}
argc -= optind;
argv += optind;

There is no other place were the value of 'lin' is used. Later on in
main(), the author starts to compare the value of 'lin'. This is what
struck my curiosity in the first place. It appears that he was
comparing the unitialized value of 'lin' to numbers.

However, when i step throug the debugger, I see the following:
Breakpoint 1, main (argc=4, argv=0xcfbdafac) at no.c:243
243 {
(gdb) display lin
1: lin = -809652604
(gdb) step

Breakpoint 2, main (argc=4, argv=0xcfbdafac) at no.c:244
244 int err, tflg = 0, wflg = 0, slen, wr,
1: lin = 1

Is there somewhere else I should try to look in the program? Or is
this just possibly undefined behavior?

Chad

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-18-2007
Chad <(E-Mail Removed)> writes:

> In the following code snippet, the value of 'lin' is somehow gettng
> intialized to the value of 1.
>
> int main(argc, argv)
> int argc;
> char **argv;
> {
> int err, tflg = 0, wflg = 0, slen, wr,
> sum = 0, in, rflg = 0, i, j, lin, waiting = 0;
> char str[256];
>
> memset(str, '\t', sizeof (str));
>
> slen = sizeof (str);


This is wrong. After filling str with 256 tabs it is not a string (no
null) so strlen(str) is an error.

>
> while ((err = getopt(argc, argv, "twrp:")) != -1)
> switch (err) {
> case 't':
> tflg = 1;
> strncpy(tsk[0].u.ut_line, optarg, sizeof
> (tsk[0].u.ut_line));


This may be OK provided you don't assume tsk[0].u.ut_line is a string.

> break;
>
> case 's':
> strncpy(str, optarg, sizeof (str));
> slen = strlen(str);


This code is wrong, though it may not be going wrong in any particular
execution of the program -- unlike the memset/strlen problem above
which always provokes undefined behaviour.

<snip>
> However, when i step throug the debugger, I see the following:
> Breakpoint 1, main (argc=4, argv=0xcfbdafac) at no.c:243
> 243 {
> (gdb) display lin
> 1: lin = -809652604
> (gdb) step
>
> Breakpoint 2, main (argc=4, argv=0xcfbdafac) at no.c:244
> 244 int err, tflg = 0, wflg = 0, slen, wr,
> 1: lin = 1


A small, complete example of the problem would be more useful than a
fragment and some debugger output -- and in trying to make such an
example you will often find the problem yourself.

--
Ben.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-18-2007
Ben Bacarisse said:

> Chad <(E-Mail Removed)> writes:
>

<snip>
>> slen = sizeof (str);

>
> This is wrong. After filling str with 256 tabs it is not a string (no
> null) so strlen(str) is an error.


That doesn't look like strlen to me.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-18-2007
Richard Heathfield <(E-Mail Removed)> writes:

> Ben Bacarisse said:
>
>> Chad <(E-Mail Removed)> writes:
>>

> <snip>
>>> slen = sizeof (str);

>>
>> This is wrong. After filling str with 256 tabs it is not a string (no
>> null) so strlen(str) is an error.

>
> That doesn't look like strlen to me.


Not to me, now. I have no idea what I was thinking! It it slightly
peculiar code, but not actually wrong.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How is entrySet getting initialized in HashMap.java ? ankur Java 2 02-23-2009 12:06 AM
why is var superValue not getting initialized to 911 in this code ankur Java 32 11-26-2008 01:21 PM
Session variables are not getting initialized in global.asa file Rahul ASP General 7 05-11-2006 02:51 PM
C changing the value in initialized read only data mohan C Programming 2 11-10-2003 10:05 AM
C changing the value in initialized read only data mohan C Programming 2 11-09-2003 06:54 PM



Advertisments