user328358
user328358

Reputation:

Segmentation fault on writing char to char* address

I've got problem with my little C program. Maybe you could help me.

char* shiftujVzorku(char* text, char* pattern, int offset){
    char* pom = text;
    int size = 0;
    int index = 0;
    while(*(text + size) != '\0'){
        size++;
    }
    while(*(pom + index) != '\0'){
        if(overVzorku(pom + index, pattern)){
            while(*pattern != '\0'){
                //vyment *pom s *pom + offset
                if(pom + index + offset < text + size){
                    char x = *(pom + index + offset);
                    char y = *(pom + index);
                    int adresa = *(pom + index + offset);
                    *(pom + index + offset) = y;   // SEGMENTATION FAULT
                    *(pom + index) = x;   
                    //*pom  = *pom - *(pom + offset);
                    //*(pom + offset) = *(pom + offset) + *pom;
                    //*pom = *(pom + offset) - *pom;
                }
                else{
                    *pom  = *pom - *(pom + offset - size);
                    *(pom + offset - size) = *(pom + offset - size) + *pom;
                    *pom = *(pom + offset - size) - *pom;
                }
                pattern++;
            }
            break;
        }
        index++;
    }
    return text;
}

Isn't important what's the programm doing. Maybe there's lot of bugs. But, why do I get SEGMENTATION FAULT (for destination see code) at this line? I'm, trying to write some char value to memory space, with help of address "pom + offset + index". Thanks for everything helpful. :)

Upvotes: 1

Views: 421

Answers (4)

canvas
canvas

Reputation: 1

What a mess!

Anyway, the problem comes from offset I think. You don't even check that *(pom + index + offset) is memory location you can use. Maybe it is after the '\0' of your text.

You should compare size and index+offset before trying to use *(pom + index + offset).

Upvotes: 0

anon
anon

Reputation:

Are you by any chance calling the code like this:

shiftujVzorku( "foobar", "xx", 0 );

If so, your code attempts to write to a character literal, which is illegal in C. You should rather do:

char buf[] = "foobar";
shiftujVzorku( buf, "xx", 0 );

Upvotes: 2

Tomer Vromen
Tomer Vromen

Reputation: 1812

I think it DOES matter what the program is doing, or at least what parameters the function takes. Right now, it looks like the problem is that you loop index until pom + index points to the end of the string, but then you try to access pom + index + offset, which is after the end of the string. Or perhaps offset is negative?

Upvotes: 0

Michael Kohne
Michael Kohne

Reputation: 12044

Because the address that pom+size+index points to is not a memory location that your program is allowed to write to.

Things to check: Is 'text' a legitimate buffer of some sort? It's just passed in, so there's no clue in the code given where it came from. Did you malloc it? Is it on the stack somewhere? Also, is it actually NUL terminated when it comes in?

Upvotes: 0

Related Questions