Where is the stupid error?

L

Lampa Dario

Hi, where is this stupid error in this program? When I execute it, i
receive a segmentation fault error.

#include <stdio.h>
int main(int argc, char *argv[], char *env[]) { int i=0; int l=0;
int word=0;
char *querystring;

querystring=malloc(sizeof(char)*100000);
if (getenv("QUERY_STRING")==NULL)
strcpy(querystring,"QUERY_STRING\0");
else
strcpy(querystring,(char *) getenv("QUERY_STRING"));
//l=strlen((char *)getenv("QUERY_STRING"));
//printf("%d \n",l);
//strcpy(querystring,(char *) getenv("QUERY_STRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n");
//printf("QUERY_STRING=%s\n",querystring);
//while (querystring!='\0')
//{
//printf("Parola %d",word++);
//while (querystring!='&')
//{
//printf("%c",querystring);
//i++;
//}
//i++;
//}
}

Francesco
 
R

Richard Bos

Lampa Dario said:
Hi, where is this stupid error in this program?

There are several stupid errors in this program.
When I execute it, i receive a segmentation fault error.

Then you're lucky. It could've gone on to scribble over random memory.
#include <stdio.h>
int main(int argc, char *argv[], char *env[]) { int i=0; int l=0;

The first stupid error is the use of a non-Standard declaration for
main() - what's worse, you don't even use the non-Standardness.

The second stupid error is a very bad indentation style, or rather, no
indentation style at all. Do you want to be able to still read your code
next month? Then lay it out legibly.
int word=0;
char *querystring;

querystring=malloc(sizeof(char)*100000);

The third stupid error is not providing a declaration for malloc() (and
later, similar for getenv()). <stdlib.h> exists for a reason - use it.

The fourth stupid error is either using a compiler which doesn't check
for such errors, or asking a perfectly good compiler not to check.

The fifth stupid error is not checking that malloc() succeeded.

Both the last error, and the third aided by the fourth, could have
caused the segfault you saw. They could just as easily have caused the
program to appear to work on your computer, but fail ignominously on
your boss's or teacher's system.
if (getenv("QUERY_STRING")==NULL)
strcpy(querystring,"QUERY_STRING\0");
else
strcpy(querystring,(char *) getenv("QUERY_STRING"));

The sixth stupid error is casting away a warning. You did not get that
warning for nothing. There is no need to cast a char * to a char * in
order to pass it to a function expecting a char *, and no useable
compiler will warn about it. Since you did (apparently - else why the
cast?) get a warning, clearly something else is wrong. It is related to
one of the errors above - guess which.
The same error occurs again later.
//l=strlen((char *)getenv("QUERY_STRING"));
//printf("%d \n",l);
//strcpy(querystring,(char *) getenv("QUERY_STRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n");
//printf("QUERY_STRING=%s\n",querystring);
//while (querystring!='\0')
//{
//printf("Parola %d",word++);
//while (querystring!='&')


The seventh stupid error is not checking whether you run over the end of
a string. If this program gets used the way I think it will, this _will_
happen here.

That's seven - that should be enough cardinal sins for one post.

Richard
 
M

Martin Ambuhl

Lampa said:
Hi, where is this stupid error in this program? When I execute it, i
receive a segmentation fault error.

The usual wild point the last 4000 times analogous questions were posted
to this newsgroup.

A well-behaved person will before posting to any newsgroup,
a) Check the FAQ -- you obviously haven't
b) follow the newsgroup for a while -- you obviously haven't.

You should learn to indent your code, lose superfluous casts, learn that
sizeof(char) == 1 always, and use a portable form of main().
And don't comment out code with C++ style '//' comments: not only are
they not legal in C89 and cause problems with broken lines on posted
code, but they are less readable than the conventional idiom of
#if 0
commented out code
#endif
 

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,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top