Segmentation fault

Discussion in 'C Programming' started by somenath, Mar 6, 2007.

  1. somenath

    somenath Guest

    Hi All,
    Can any body explain why the bellow mention crashes wwhen I try to
    execute.

    #include<stdio.h>
    #include<stdlib.h>
    void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP);

    int main(void )
    {
    char * s ="34567";
    int x = 5;
    int *iptrP=NULL;
    convert_cpp_string_to_int(s,iptrP);
    printf("INTEGER = =%d \n", *iptrP);
    return 0;
    }
    void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
    {
    *integer_valueP = atoi( cpp_string);

    }

    But If i modify my code as mention bellow it does not crash
    #include<stdio.h>
    #include<stdlib.h>
    void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP);

    int main(void )
    {
    char * s ="34567";
    int x = 5;

    int *iptrP=&x;
    convert_cpp_string_to_int(s,iptrP);
    printf("INTEGER = =%d \n", *iptrP);
    return 0;
    }
    void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
    {
    *integer_valueP = atoi( cpp_string);

    }

    Regards,
    Somenath
     
    somenath, Mar 6, 2007
    #1
    1. Advertisements

  2. You've explicitly said "This pointer doesn't point anywhere", so
    naturally bad things are bound to happen when you dereference it. If
    you make it point somewhere useful, say for example to enough memory
    to store an int,

    int *iptrP;
    /* ... */
    iptrP=malloc( sizeof *iptrP );

    , all will be well.

    Note that in the second program you posted,
    you've said "The pointer points at x", which is fine.
     
    Christopher Benson-Manica, Mar 6, 2007
    #2
    1. Advertisements

  3. somenath

    somenath Guest

    If I change the code as following will it cause any problem ?
    #include<stdio.h>
    #include<stdlib.h>
    void convert_cpp_string_to_int(char * cpp_string,int
    *integer_valueP);


    int main(void )
    {
    char * s ="34567";
    int x = 5;
    int iptrP;
    convert_cpp_string_to_int(s,&iptrP);
    printf("INTEGER = =%d \n", iptrP);
    return 0;


    }


    void convert_cpp_string_to_int(char * cpp_string,int *integer_valueP)
    {
    *integer_valueP = atoi( cpp_string);


    }
     
    somenath, Mar 7, 2007
    #3
  4. somenath

    santosh Guest

    Please remove sig blocks, like the above, unless you're commenting on
    them.
    Your names are exceedingly verbose, spoiling readability.
    Why do you want to encapsulate a trivial function like atoi? Atleast
    with strtoXX, I can understand.
     
    santosh, Mar 7, 2007
    #4
  5. No. Again, you're passing a pointer to a real int, so there is no
    problem.
     
    Christopher Benson-Manica, Mar 7, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.