Why does this dump core?

A

Army1987

What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {
strncat(line, " ", MAX_LEN - len - 1);
len++;
strncat(line, *argv, MAX_LEN - len - 1);
len += strlen(*argv);
}
#if DEBUG
puts(line);
return 0;
#endif
return system(line);
}
 
B

Barry Schwarz

What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.
strncat(line, " ", MAX_LEN - len - 1);
len++;
strncat(line, *argv, MAX_LEN - len - 1);
len += strlen(*argv);
}
#if DEBUG
puts(line);
return 0;
#endif
return system(line);
}


Remove del for email
 
A

Army1987

Barry Schwarz said:
What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.

Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
works. Of course argv itself will never become NULL. I wonder how
could I repeatedly fail to see that. I had even suspected that forù
some reasons one cannot assign to argv...
 
K

Kenneth Brody

Barry said:
What's wrong with this code? [...]
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.
[...]

Does the standard guarantee that argv[argc]==NULL (I know that the
implementations I use do add a NULL to the argv[] array), or do you
need to make sure you stop at argv[argc-1] ?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
F

Fred Kleinschmidt

Army1987 said:
Barry Schwarz said:
What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.

Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
works.
No, it doesn't. You are not checking whether argv is NULL.
Youi are incrementing it and THEN checking for NULL.
Of course argv itself will never become NULL.

Sure it can. One way is this:

int main( int argc, char **argv ) {
if ( argv!=NULL) {
main(0, NULL);
}
return 0;
}
 
D

Default User

Kenneth said:
Barry said:
What's wrong with this code? [...]
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.
[...]

Does the standard guarantee that argv[argc]==NULL

Yes. From the c99 draft:

[#2] If they are declared, the parameters to the main
function shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.


Brian
 
C

Coos Haak

Op Mon, 14 May 2007 13:41:47 -0400 schreef Kenneth Brody:
Barry said:
What's wrong with this code? [...]
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.
[...]

Does the standard guarantee that argv[argc]==NULL (I know that the
implementations I use do add a NULL to the argv[] array), or do you
need to make sure you stop at argv[argc-1] ?

Yes, n1124.pdf, paragraph 5.1.2.2.1 Program startup states
¡X argv[argc] shall be a null pointer.
 
A

Army1987

Fred Kleinschmidt said:
Army1987 said:
Barry Schwarz said:
What's wrong with this code?

#define DEBUG 1
#include <string.h>
#if DEBUG
#include <stdio.h>
#endif
#include <stdlib.h>
#define MAX_LEN 32767
int main(int argc, char **argv)
{
char line[MAX_LEN] = "gcc -ansi -pedantic -Wall -Wextra -O3";
size_t len = strlen(line);
while (++argv && len < MAX_LEN) {

You need to test *argv for NULL.

Yes. I replaced it with while (*++argv && len < MAX_LEN) and it
works.
No, it doesn't. You are not checking whether argv is NULL.
Youi are incrementing it and THEN checking for NULL.

So? argv looks like this:

argv
\/
|ptr. to program name|ptr. to 1st arg|...|ptr. to last arg|NULL|
After ptr. to last argument has been used, argv is increased, then
checked for NULL. Since it is NULL, the loop breaks. This is
exactly what I was trying to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top