Results 1 to 3 of 3

Thread: Segmentation fault C programming

  1. #1
    Join Date
    Aug 2011
    Location
    Assam, India
    Beans
    3
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Segmentation fault C programming

    I've tried to run the following code in netbeans 7 in ubuntu ...
    build successful 194 ms, run failed 487ms
    directly tried on gcc from terminal, segmentation fault, but works fine in Dev C++ in MS Windows. Somebody please explain why....

    #include <stdio.h>
    #include <stdlib.h>

    /*
    *
    */
    int main(int argc, char** argv) {

    char *s = "hello, world";
    char *t, *temp=t;

    while(*t++=*s++);
    while(*temp)
    printf("%c", *temp++);

    return (EXIT_SUCCESS);
    }

  2. #2
    Join Date
    May 2006
    Beans
    1,790

    Re: Segmentation fault C programming

    Quote Originally Posted by sgogoi View Post
    I've tried to run the following code in netbeans 7 in ubuntu ...
    build successful 194 ms, run failed 487ms
    directly tried on gcc from terminal, segmentation fault, but works fine in Dev C++ in MS Windows. Somebody please explain why....

    #include <stdio.h>
    #include <stdlib.h>

    /*
    *
    */
    int main(int argc, char** argv) {

    char *s = "hello, world";
    char *t, *temp=t;

    while(*t++=*s++);
    while(*temp)
    printf("%c", *temp++);

    return (EXIT_SUCCESS);
    }
    You need to allocate memory for 't'. That it works somewhere is pure luck.

  3. #3
    Join Date
    Nov 2009
    Beans
    28

    Re: Segmentation fault C programming

    You're stomping all over memory with those uninitialized pointers. temp points to a random memory location.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •