segfault in pointer to string program


 
Thread Tools Search this Thread
Top Forums Programming segfault in pointer to string program
# 1  
Old 09-16-2011
segfault in pointer to string program

hello all,
my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str[20]) why this function gives me segfault Smilieand the other code executes comfortably though both code uses same pointer arithmetic.

Code:
#include<stdio.h>
#include<string.h>
void reverse(char*);
int
main()
{
 char str[20];

 scanf("%s",str);
 printf("original string is %s :\n",str);
 reverse(str);
 printf("reverse string is %s  :",str);
 return 0;
}

void reverse(char* s)
{
 int n,mark,temp;
 n = mark = strlen(s);

 for(;s < (s + n/2);++s)
    {
      temp = *s;
      *s = *(s + (--mark));
      *(s + mark) = temp;
    }

 return;
}

while the code below executes happily

Code:
void my_strrev(char* begin){
    char temp;
    char* end;
    end = begin + strlen(begin)-1;

    while(end>begin){
        temp = *end;
        *end = *begin;
        *begin = temp;
        end--;
        begin++;
    } 
}

main(){
    char string[20];
    scanf("%s",string);
    my_strrev(string);
    printf("%s", string);
}

# 2  
Old 09-16-2011
This loop never ends, you just walk right off the end of the string:

Code:
 for(;s < (s + n/2);++s)

n is a constant value, you keep incrementing s, and off you go.
This User Gave Thanks to achenle For This Post:
# 3  
Old 09-16-2011
Bug

got it Smilie thank you so much. do C through segfault for any particular type of error?? i wanna know when exactly C throws segfault?
# 4  
Old 09-16-2011
Quote:
Originally Posted by zius_oram
got it Smilie thank you so much. do C through segfault for any particular type of error?? i wanna know when exactly C throws segfault?
segfault is one kind of runtime error and there can be a number of reasons for it...the most common ones are buffer overruns or stepping into memory which is read only or dereferencing an invalid address pointer.
This User Gave Thanks to shamrock For This Post:
# 5  
Old 09-16-2011
Quote:
Originally Posted by zius_oram
got it Smilie thank you so much. do C through segfault for any particular type of error?? i wanna know when exactly C throws segfault?
All 'segfault' means is that your program attempted to access a memory page that either
1) doesn't exist, or
2) your program doesn't have permissions to access.

There's almost endless reasons why a program could end up doing that but it's often a logic error of some sort -- not checking the return value of something and dereferencing a NULL, going beyond array bounds and accidentally overwriting nearby variables, reusing a pointer you already free()'d and mangling whatever data(if anything) ended up in it later... You can even corrupt the stack frame itself so return jumps the program to invalid memory and bombs out long after the actual error was made. And lots more.

You went beyond array bounds, which would have started modifying the values of local stack variables. This can go wrong in many interesting ways... it could have kept going until you'd mangled your entire stack and hit the bottom of memory. Or (more likely) when you started mangling local stack variables, the value of s was set to some bizzare value which pointed to invalid memory.

Last edited by Corona688; 09-16-2011 at 04:04 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segfault When Parsing Delimiters In C

Another project, another bump in the road and another chance to learn. I've been trying to open gzipped files and parse data from them and hit a snag. I have data in gzips with a place followed by an ip or ip range sort of like this: Some place:x.x.x.x-x.x.x.x I was able to modify some code... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Programming

String pointer does not work

Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted. #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "kseq.h" // STEP 1: declare the type of file... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Programming

Scanf() string pointer problem

I have a problem with scanf() for string pointer as member of a struct. #include <stdio.h> #include <stdlib.h> struct Student { int studentNumber; int phoneNumber; char *studentName; //line 7 // char studentName; //line 8 }; int... (10 Replies)
Discussion started by: yifangt
10 Replies

4. Programming

String array iteration causing segfault

I am populating an array of string and print it. But it going in infinite loop and causing segfault. char Name = { "yahoo", "rediff", "facebook", NULL }; main(int argc, char* argv) { int j = 0; ... (7 Replies)
Discussion started by: rupeshkp728
7 Replies

5. Programming

How i use pointer as a string in c programing?

I'm newbie learner. My all friend use windows just only me use linux. so i can't solve any problem by myself. i need a solution. how can i use pointer as a string. #include<string.h> #include<stdio.h> int main() { char *s='\0'; gets(s); puts(s); return 0; } This code work on... (6 Replies)
Discussion started by: raihan004
6 Replies

6. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

7. Programming

C++ program is crashing on re-assigning const static member variable using an int pointer

Hi, Can any one tell me why my following program is crashing? #include <iostream> using namespace std; class CA { public: const static int i; }; const int CA::i = 10; int main() { int* pi = const_cast<int*>(&CA::i); *pi = 9; cout << CA::i << endl; } (6 Replies)
Discussion started by: royalibrahim
6 Replies

8. Programming

id3lib SEGFAULT

Hello everyone, I'm writing a program using the id3lib unfortunately I've encountered with memory issue that cause segmentation fault. I tried to rerun and analyze the program with valgrind but it doesn't point me anywhere. I really stuck on this one. Valgrind output: ==14716== Invalid read of... (2 Replies)
Discussion started by: errb
2 Replies

9. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

10. Programming

String and pointer problem

i am having a string like " X1 " ---> string lenght is 30 I have stored this to a chararry . ref so here ref = " X1 " now i trim the left space by my function . Si the string now becomes "X1 " ---> string lenght is 15... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies
Login or Register to Ask a Question