1

I am following an example of a book in writing a function to reverse a string in C. This is the following problem. But when I execute it under ubuntu using gcc. I get a seg fault. I have tried debugging it, but I don't understand how can this line '*start++ = *end;' causing a seg fault.

I appreciate someone can help me understand the seg fault.

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

void myreverse(char* str) {
    int len = strlen(str);
    char tmp;

    char* start = str;
    char* end = str + (len -1);

    while (start < end) {
        tmp = *start;
        // this is causing Segmentation fault
        *start++ = *end;
        *end-- = tmp;
    }
}

int main(void) {
    char* test = "Hello World";
    puts(test);
    myreverse(test);
    puts(test);
    return EXIT_SUCCESS;
}
michael
  • 106,540
  • 116
  • 246
  • 346
  • possible duplicate of [Modifying C string constants?](http://stackoverflow.com/questions/480555/modifying-c-string-constants) – Mat Oct 26 '11 at 06:08
  • In general, `int` may be insufficient to hold the length of a C string. `size_t` is the best type in general, and it's what `strlen()` returns. – Alexey Frunze Oct 26 '11 at 06:08

6 Answers6

1

String literals are stored in a read only section of your executable. You could avoid that by changing char* test = "Hello World"; to char test[] = "Hello World";, where "Hello World" will be copied into the array test.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
0

Your string buffer is read only memory. String literals cannot be modified.

Use strcpy or strdup to make a buffer of writeable memory.

char* test = strdup("Hello World");

Remember to free the string when you are done with it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0
char* test = "Hello World";

That is a string literal. It's read only; you can't modify it.

You are trying to modify it :)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

See the answer here: Modifying a C string: access violation

Basically a C string literal is read only and can't be written to.

Community
  • 1
  • 1
sashang
  • 11,704
  • 6
  • 44
  • 58
0
char* test = "Hello World";

when you dynamically give a string to a character pointer, the string you give gets saved in Read only part of data segment..Then you are passing this address to your myReverse function where you are trying to change the contents of that location in the line

*start++ = *end;

which cause the segmentation fault..

Just change this line

char* test = "Hello World";

to

char temp[] = "Hello world";
char *test  = temp;

and it wouldn't crash..See this thread for more details..

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

Based on my experience, seg fault occurs when you are trying to access a memory location outside the scope of your variables. C does not have any bounds checking, so I'm guess somewhere in your code (*start++) is incrementing a pointer to a memory location that you have not reserved in your code. You can't just iterate through memory locations that you haven't allocated in your code because this could interfere with data that is being used for other purposes and will have unexpected results.

chrislgarry
  • 616
  • 5
  • 21
  • You just need to read the code to see that start is assigned to a valid memory location. The problem is it is only valid for reading. – David Heffernan Oct 26 '11 at 06:56
  • Right. "start" points to the first character. I can see that. I was saying that "start" is ending up at a location that the compiler doesn't want to give access to, in other words. I'm by no means an expert, but am trying to give some more insight based on my general experiences with seg fault and what it implies =) so I guess seg fault is being thrown here because the compiler doesn't allow access to the start variable, in this case you say it is a "write" access that the compiler isn't allowing since it is read only. Correct? – chrislgarry Oct 27 '11 at 23:36