By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

Where is the stupid error?

P: n/a
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[i]!='\0')
//{
//printf("Parola %d",word++);
//while (querystring[i]!='&')
//{
//printf("%c",querystring[i]);
//i++;
//}
//i++;
//}
}

Francesco

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lampa Dario <la**@dario.it> wrote:
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[i]!='\0')
//{
//printf("Parola %d",word++);
//while (querystring[i]!='&')


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
Nov 15 '05 #2

P: n/a
Lampa Dario wrote:
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
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.