segmentation error
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: segmentation error

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    segmentation error

    Hello!
    I was using the scanf function for inputtig the character.
    scanf("%s",&ch); in gcc, program shows no fault while compilation, but gives "Segmentation Error" while executing.
    What is this "Segmentation Error" means?
    waitting to hear you
    Thanx for youe time and patience
    Regards
    Harbir
    U get What U pay for.

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    How did you declare ch? If your code isn't that big post everything here. It'll make it easier for us to review it.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    char ch;
    U get What U pay for.

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Code:
    char ch;
    Declared like this ch can only contain 1 (one) character. If you try to put more in there you'll be overwriting other parts (segments) of memory. This is what the segmentation fault is telling you.

    Code:
    char ch[20];
    This will reserve 20 bytes (characters) of memory. If you try to write more in it you'll get the segmentation fault again.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    the following code works fine with Turbo C, but with gcc it give the error "Segmentation Fault" at the run time and do not repeate as it should be with the do-while loop.
    --------------------------------------------------------------------------------------------------------------------------------------------
    #include<stdio.h>
    #define si 10

    int main()
    {
    int search(int num,int ke,int x[]);
    char ch='y';
    int i,j,key;
    int a[si]={0};
    i=j=key=0;



    printf("\nEnter the elements in the array: ");

    for(i=0;i<10;i++)
    {
    scanf("%d",&a[i]);
    }

    do
    {
    printf("\n________\nEnter the value to be searched: ");

    scanf("%d",&key);

    j=search(si,key,a);

    if(j != -1)
    {
    printf("\n~~~~~~~\nThe valu is at a[%d] element",j);
    }
    else
    {
    printf("\n~~~\n Sorry the value does not exist");
    }

    printf("\nDo u wish t continue: ");

    scanf("%s",&ch);

    }while(ch != 'n');

    return 0;
    }
    int search(int num,int ke,int x[])
    {
    int i=0;
    for(i=0;i<num;i++)
    {
    if(ke==x[i])
    {
    return i;
    }
    }
    return -1;
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------
    U get What U pay for.

  6. #6
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    i just want to enter one character
    U get What U pay for.

  7. #7
    Banned
    Join Date
    Jun 2003
    Posts
    1,302
    Dude can you use code blocks please or spread your code up, I am having a hell of a time reading it, all clumped together like that.

    The reason that you might get a problem with one compiler and not another is, because different compilers work differently. (While rather have different rules, they use.)

    GCC IMHO is to damn up tight.

    Never used turbo so I can't tell you about that one.

  8. #8
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Originally posted here by harbir
    the following code works fine with Turbo C, but with gcc it give the error "Segmentation Fault" at the run time and do not repeate as it should be with the do-while loop.
    Well, personally I think scanf("%s",&ch) should not work with any compiler. If the user entered more than one character, it will override the buffer allocated for ch, since %s will treat ch as char array instead of single char. You should use the safer scanf("%c",&ch).

    OK, I've done quick research (by entering more than one character) on your code:
    with Borland BCC 5.5 - OK (compiles OK, runs OK)
    with Dev-CPP gcc 3.2 - OK (compiles OK, runs OK)
    with Cygwin gcc 3.3.1 - Segmentation fault (compiles OK, core dumped when run)

    I wonder why it runs OK with BCC and gcc 3.2. Do they quietly provide buffer overflow protection? Looks like Borland compilers are pretty loose, while newer gcc is quite strict.

    Kinda reminds me of the saying: if a buffer overflow happens, Java immediately crashes, while with C/C++ the result is undeterminable.

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  9. #9
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    so does that means we have found something here?
    U get What U pay for.

  10. #10
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    Have you tried debugging both versions (1 compiled with TurboC and 1 with gcc) to see what actually happens?
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides