Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange Error after printing argv values

Reply
Thread Tools

Strange Error after printing argv values

 
 
Matt
Guest
Posts: n/a
 
      07-19-2007
Hello. I've got a very strange problem. Basically I have a programme
where I wish to view all the strings in the argv array so I can see
what arguments are being passed to the programme.

However, when I insert the following line at the start of a FOR loop
early on the in the programme to do this:

printf("\nCommand line arguement %d: %s. \n", i , argv[i] );

I get back the first 3, then I get a segmentation fault followed by
all kinds of errors. When I comment this line out he programme runs
with no problems.

It seems that reading and outputting the values of argv is changing
something in the programme. How can just outputting the values of argv
to the screen cause this?

Kind Regards,

Matt

 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      07-19-2007
In article <(E-Mail Removed) .com>,
Matt <(E-Mail Removed)> wrote:
>Hello. I've got a very strange problem. Basically I have a programme
>where I wish to view all the strings in the argv array so I can see
>what arguments are being passed to the programme.
>
>However, when I insert the following line at the start of a FOR loop
>early on the in the programme to do this:
>
>printf("\nCommand line arguement %d: %s. \n", i , argv[i] );


I assume that you did:

for (i=1; i<=3; i++)
printf("\nCommand line arguement %d: %s. \n", i , argv[i] );

When you should have done:

for (i=0; i<argc; i++)
printf("\nCommand line argument %d: %s. \n", i , argv[i] );

 
Reply With Quote
 
 
 
 
mmcconnell17704@yahoo.com
Guest
Posts: n/a
 
      07-19-2007
Maybe you're trying to print too many? The loop should be
for (i = 0; i < argc; ++i) { ... }
For example, in the call
myprogram 1776.0 xyzzy
argc will be 3. argv[0] will be myprogram, argv[1] will be 1776.0,
and argv[2] will be xyzzy.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      07-19-2007
(E-Mail Removed) (Kenny McCormack) wrote:
>
> When you should have done:
>
> for (i=0; i<argc; i++)
> printf("\nCommand line argument %d: %s. \n", i , argv[i] );


The arguments can still be NULL; in particular,
argv[0] could be null. This happens even on the
most common OS's; e.g. when a task is invoked via
a call like execv where the caller specifies the
arguments, including argv[0].

So you should write:

for (i=0; i<argc; i++)
if ( argv[i] )
printf("Command line argument %d: %s.\n", i , argv[i] );

or perhaps :

for (i=0; i<argc; i++)
printf("Command line argument %d: %s.\n",
i , argv[i] ? argv[i] : "<null>");

 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      07-19-2007
On Thu, 19 Jul 2007 14:08:26 -0000, Matt <(E-Mail Removed)> wrote:

>Hello. I've got a very strange problem. Basically I have a programme
>where I wish to view all the strings in the argv array so I can see
>what arguments are being passed to the programme.
>
>However, when I insert the following line at the start of a FOR loop
>early on the in the programme to do this:
>
>printf("\nCommand line arguement %d: %s. \n", i , argv[i] );
>
>I get back the first 3, then I get a segmentation fault followed by
>all kinds of errors. When I comment this line out he programme runs
>with no problems.
>
>It seems that reading and outputting the values of argv is changing
>something in the programme. How can just outputting the values of argv
>to the screen cause this?
>

It can't. However, fetching the argv[i] can certainly cause a
segfault, if i is greater than or equal to the number of arguments you
actually have. I would guess that you're invoking the program with two
or less arguments (argv[0]) will be the name in most cases, but not
guaranteed.) The typical name for the count variable is argc. A
typical program will be something like

#include <stdio.h>

int main(int argc, char *argv[])
{
..
..
..
int i;
for (i = 0; i < argc; ++i)
{
/* do something with argv[i], such as: */
printf("\nCommand line argument %d: '%s'. \n", i , argv[i] );
}
..
..
..
}

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      07-20-2007
On Jul 19, 7:32 pm, Old Wolf <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Kenny McCormack) wrote:
>
> > When you should have done:

>
> > for (i=0; i<argc; i++)
> > printf("\nCommand line argument %d: %s. \n", i , argv[i] );

>
> The arguments can still be NULL; in particular,
> argv[0] could be null. This happens even on the
> most common OS's; e.g. when a task is invoked via
> a call like execv where the caller specifies the
> arguments, including argv[0].


While this is certainly true it breaks conformity with Standard C
(except when argc is 0). It is certainly not bad practice to write
code to anticipate such possibilities but if the values of the program
parameters are coerced in such a way one should expect to suffer the
consequences.

Robert Gamble

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      07-22-2007
The relevant part of my code reads as follows:

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

{

printf("\narg = %d\n" , argc );

printf("\nCommand line arguement %d: %s. \n", i , *argv[i] );

and the stdio.h header is loaded at the start. You may notice I have
added a * before the argv[i] as I realised I wished to print the value
the pointer points at, was this a correct thing to do?

Something else that is rather worrying is that if I comment this bit
out, later in the programme when it tries to read argv[1], I get
another segmentation fault. I assume this only happens when argv[1] =
NULL, but since I am almost certain the programme to supplied with at
least one argument, and that the error occurs when I try to print
argv[0] (which is the one thing that cannot be NULL!), something else
must be wrong here.

Kind Regards,

Matt

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      07-22-2007
> I assume that you did:
>
> for (i=1; i<=3; i++)
> printf("\nCommand line arguement %d: %s. \n", i , argv[i] );
>
> When you should have done:
>
> for (i=0; i<argc; i++)
> printf("\nCommand line argument %d: %s. \n", i , argv[i] );


The latter is what I did, as shown in my previous post.


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      07-22-2007
On Jul 23, 10:48 am, Matt <(E-Mail Removed)> wrote:
> The relevant part of my code reads as follows:
>
> printf("\nCommand line arguement %d: %s. \n", i , *argv[i] );
>
> and the stdio.h header is loaded at the start. You may notice I have
> added a * before the argv[i] as I realised I wished to print the value
> the pointer points at, was this a correct thing to do?


No. Read the documentation for printf, specifically
%s. You will see that it expects a pointer to char.
This is because the printf function has to be able
to increase the pointer until it reaches the end-
of-string marker.

As it stands, your code causes undefined behaviour
because you did not pass a pointer-to-char for
%s, so anything could happen. A segfault is most
likely; garbage output is another possibility.

> Something else that is rather worrying is that if I comment this bit
> out, later in the programme when it tries to read argv[1], I get
> another segmentation fault.


Post the exact program that causes the fault.

> I assume this only happens when argv[1] = NULL


Why assume? Add in code to check if this is happening.


 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      07-22-2007
> No. Read the documentation for printf, specifically
> %s. You will see that it expects a pointer to char.
> This is because the printf function has to be able
> to increase the pointer until it reaches the end-
> of-string marker.
>
> As it stands, your code causes undefined behaviour
> because you did not pass a pointer-to-char for
> %s, so anything could happen. A segfault is most
> likely; garbage output is another possibility.


Just before your reply I got rid of the *, and now I get the value of
argv[0] printed to the screen, followed by a segmentation fault.

> > Something else that is rather worrying is that if I comment this bit
> > out, later in the programme when it tries to read argv[1], I get
> > another segmentation fault.

>
> Post the exact program that causes the fault.


The programme in its entirity is far too large I'm afraid. Basically
the bit of troublesome code arises from my implementation of the
getopt_long function from the GNU library:

http://www.gnu.org/software/libc/man...t-Long-Options

with the example code:

http://www.gnu.org/software/libc/man...Option-Example

My implemtation is as follows:

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

{

printf("\nargc = %d\n" , argc );

printf("\nCommand line arguement %d: %s. \n", i , argv[i] );

while (1)

{

static struct option long_options[] =

{

// These options set a flag.
{"stack" , no_argument , &stack_mode , 1}, // Enable
stack (convolution) mode

// These options don't set a flag. We distinguish them
by their indices.
{"configfile" , required_argument , 0 , 'c'},
{0 , 0 , 0 , 0}

};

// getopt_long stores the option index here.
int option_index = 0;

*getopt_val = getopt_long (argc, argv, "c:", long_options,
&option_index);

// Detect the end of the options.
if (*getopt_val == -1)

{

break;

}

switch (*getopt_val)

{

case 0:
// If this option set a flag, do nothing else now.
if ( long_options[option_index].flag != 0)

{

break;

}

printf ("option %s", long_options[option_index].name);

if (optarg)

{

printf (" with arg %s", optarg);

}

printf ("\n");
break;

case 'c':
printf ("option -c with value `%s'\n", optarg);
break;

case '?':
/* getopt_long already printed an error message. */
break;

default:
abort ();

}

}

/* Print any remaining command line arguments (not options). */
if (optind < argc)

{

printf ("non-option ARGV-elements: ");

while (optind < argc)

{

printf ("%s ", argv[optind++]);

}

putchar ('\n');

}

}


>
> > I assume this only happens when argv[1] = NULL

>
> Why assume? Add in code to check if this is happening.


Good point. I have checked and argv[1] = NULL.



 
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
char **argv & char *argv[] jab3 C Programming 5 12-08-2004 08:15 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
difference(s) between char** argv and char* argv[] David C Programming 10 09-15-2003 06:58 AM
char **argv vs. char* argv[] Bret C Programming 21 09-03-2003 03:24 AM
sys.argv[0] - 'module' object has no attribute 'argv' =?ISO-8859-1?Q?Thomas_N=FCcker?= Python 0 06-30-2003 02:07 PM



Advertisments