将 char 写入 char* 地址时出现分段错误

发布于 2024-09-05 21:35:42 字数 1399 浏览 3 评论 0原文

我的小 C 程序有问题。也许你可以帮助我。

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;
}

程序在做什么并不重要。也许有很多bug。但是,为什么我会在这一行收到 SEGMENTATION FAULT(有关目的地,请参阅代码)?我试图在地址“pom + offset + index”的帮助下将一些 char 值写入内存空间。感谢您提供的一切帮助。 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

[浮城] 2024-09-12 21:35:42

您是否有机会像这样调用代码:

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

如果是这样,您的代码会尝试写入字符文字,这在 C 中是非法的。您应该这样做:

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

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 );
送你一个梦 2024-09-12 21:35:42

因为pom+size+index指向的地址不是你的程序允许写入的内存位置。

要检查的事情:“文本”是否是某种合法的缓冲区?它刚刚传入,因此代码中没有任何线索表明它来自哪里。你malloc了吗?它在堆栈的某个地方吗?另外,它进来时实际上是 NUL 终止的吗?

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?

下壹個目標 2024-09-12 21:35:42

我认为程序正在做什么,或者至少函数采用什么参数确实很重要。现在,问题似乎是您循环 index 直到 pom + index 指向字符串的末尾,但随后您尝试访问 pom + index + offset,位于字符串末尾之后。或者也许 offset 是负数?

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?

久隐师 2024-09-12 21:35:42

真是一团糟!

无论如何,我认为问题来自于偏移。您甚至不检查 *(pom + index + offset) 是否是您可以使用的内存位置。也许它位于文本的“\0”之后。

在尝试使用 *(pom + index + offset) 之前,您应该比较大小和索引+偏移量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文