-
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
-
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.
-
-
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.
This will reserve 20 bytes (characters) of memory. If you try to write more in it you'll get the segmentation fault again.
-
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;
}
-------------------------------------------------------------------------------------------------------------------------------------------------
-
i just want to enter one character
-
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.
-
Quote:
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>
-
so does that means we have found something here?
-
Have you tried debugging both versions (1 compiled with TurboC and 1 with gcc) to see what actually happens?