views:

167

answers:

4

Hi guys,

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. :)

A: 

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?

Michael Kohne
calling this function: char* a = "abcd"; char* b = "b"; shiftujVzorku(a, b, 1);in dbg mode, I can see that address "pom+size+index" sees something inside string "abcd", char x = *(pom + index + offset); //gets characted 'c'I think there's no reason to be out of accessible memory space.
Lukas Dojcak
A: 

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?

Tomer Vromen
params are definitely fine :) pls see my previous comment for detailed infos.
Lukas Dojcak
problem was, that from declaration of: char *x = "....", x is pointer to const sequence of characters, You cannost write or change one of these chars using *x
Lukas Dojcak
@Lukas: It's a bit late, but good to know that you solved the problem.
Tomer Vromen
+2  A: 

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 );
anon
very well, thanks a lot! I works.
Lukas Dojcak
A comment on conversion from "char const*" to "char*" may be useful to help explain why it compiled but then crashed.
Martin York
A: 

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).

canvas