-
July 26th, 2004, 11:43 AM
#1
Senior Member
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.
-
July 26th, 2004, 11:44 AM
#2
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.
-
July 26th, 2004, 03:21 PM
#3
Senior Member
U get What U pay for.
-
July 26th, 2004, 03:28 PM
#4
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.
Oliver's Law:
Experience is something you don't get until just after you need it.
-
July 26th, 2004, 04:37 PM
#5
Senior Member
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.
-
July 26th, 2004, 04:40 PM
#6
Senior Member
i just want to enter one character
U get What U pay for.
-
July 26th, 2004, 06:11 PM
#7
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.
-
July 27th, 2004, 10:26 AM
#8
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
-
July 27th, 2004, 04:23 PM
#9
Senior Member
so does that means we have found something here?
U get What U pay for.
-
July 28th, 2004, 10:51 AM
#10
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
-
Forum Rules
|
|