将 char 写入 char* 地址时出现分段错误
我的小 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否有机会像这样调用代码:
如果是这样,您的代码会尝试写入字符文字,这在 C 中是非法的。您应该这样做:
Are you by any chance calling the code like this:
If so, your code attempts to write to a character literal, which is illegal in C. You should rather do:
因为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?
我认为程序正在做什么,或者至少函数采用什么参数确实很重要。现在,问题似乎是您循环
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
untilpom + index
points to the end of the string, but then you try to accesspom + index + offset
, which is after the end of the string. Or perhapsoffset
is negative?真是一团糟!
无论如何,我认为问题来自于偏移。您甚至不检查 *(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).