Where is the stupid error?

Discussion in 'C Programming' started by Lampa Dario, Sep 5, 2005.

  1. Lampa Dario

    Lampa Dario Guest

    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
     
    Lampa Dario, Sep 5, 2005
    #1
    1. Advertising

  2. Lampa Dario

    Richard Bos Guest

    Lampa Dario <> 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!='\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
     
    Richard Bos, Sep 5, 2005
    #2
    1. Advertising

  3. 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
     
    Martin Ambuhl, Sep 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Child

    Stupid error - Radiobox list...

    Child, Dec 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    404
    Natty Gur
    Dec 30, 2003
  2. =?Utf-8?B?U21vb3RoY29kZXI=?=

    ASP.NET brand new and untouched project crashes - stupid error

    =?Utf-8?B?U21vb3RoY29kZXI=?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    500
    =?Utf-8?B?U21vb3RoY29kZXI=?=
    Jul 18, 2005
  3. =?Utf-8?B?bWVuc3V1cg==?=

    stupid annoying form error

    =?Utf-8?B?bWVuc3V1cg==?=, Mar 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    301
    =?Utf-8?B?bWVuc3V1cg==?=
    Mar 4, 2006
  4. Brandon McCombs
    Replies:
    4
    Views:
    519
    Richard Wheeldon
    Aug 28, 2006
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,024
Loading...

Share This Page