[英]Segmentation Fault with Malloc
I needed to implement and specific ADT, strqueue, for my CS class today and so I wrote up two functions: create_StrQueue(), and add_to_back(StrQueue sq, const char* str). 今天,我需要为我的CS类实现特定的ADT strqueue,因此我编写了两个函数:create_StrQueue()和add_to_back(StrQueue sq,const char * str)。 Unfortunately, when I call create_StrQueue in add_to_back I get a seg fault, and I am unable to figure out why exactly.
不幸的是,当我在add_to_back中调用create_StrQueue时,出现了段错误,并且我无法弄清楚为什么。 Here is the code that I wrote for these two functions:
这是我为这两个函数编写的代码:
[edit] I should probably malloc tempWord in add_to_back. [edit]我可能应该在add_to_back中分配tempWord。
#include <stdlib.h>
// A strqueue is an ADT consisting of words
struct strqueue{
StrQueue back; // last StQueue in queue
StrQueue next; // next StrQueue in queue
char* word; // stored string
int length; // length of entire queue
};
typedef struct strqueue* StrQueue;
StrQueue create_StrQueue(void){
StrQueue retq = malloc(sizeof(struct strqueue)); // get memory for a new strqueue
retq->word = malloc(sizeof(char*));
retq->word = NULL;
retq->back = retq; // set back pointer to itself
retq->next = NULL; // nothing after this strqueue yet
return retq;
}
void add_to_back(StrQueue sq, const char* str){
char* tempWord;
sq->length++;
for(int i=0; str[i]; ++i) tempWord[i]=str[i]; // copy string for the new strqueue
if(sq->word==NULL) sq->word = tempWord; // input strqueue was empty
// input StrQueue was not empty, so add a new StrQueue to the back
StrQueue new = create_StrQueue(); // results in seg fault
new->word = tempWord;
sq-back->next = new; // swaping pointers around to add malloced StrQueue to the back
sq->back = next;
}
I'm at a loss, and so I'm hoping someone can clarify what exactly is going on, because when I run main like so; 我很茫然,所以我希望有人能弄清楚到底发生了什么,因为当我像这样运行main时;
int main(void){
char* str1 = "Hello";
StrQueue sq = create_StrQueue(); // does not cause seg fault
add_to_back(sq, str1);
}
calling create_StrQueue() for the first time works just fine. 第一次调用create_StrQueue()效果很好。
char*
in the struct is a pointer to a character array. 结构中的
char*
是指向字符数组的指针。 retq->word = malloc(sizeof(char*));
is not the right way to allocate a string; 不是分配字符串的正确方法; what this actually does is it assigns a tiny array to
word
, essentially useless, and then you overwrite what you just allocated by assigning NULL
to word
, leaking the memory. 这实际上是在给
word
分配一个很小的数组 ,实际上是无用的,然后您通过为word
分配NULL
来覆盖刚分配的内容,从而泄漏内存。 All memory allocated by malloc
must be manually released later on with free
. 由
malloc
分配的所有内存必须稍后使用free
手动释放。 You are dealing with a pointer. 您正在处理一个指针。 Assigning data to it has no magic involved in C, you are simply replacing the value of the pointer itself.
向其分配数据没有C的魔力,您只需替换指针本身的值即可。
In add_to_back
, you need to allocate space to tempWord before copying data into it: 在
add_to_back
,需要在将数据复制到tempWord之前为其分配空间:
tempWord = malloc( strlen(str)+1 );
You add 1 to accommodate the null terminator in the string. 您添加1以容纳字符串中的空终止符。 Use
strcpy
to copy into tempWord
instead of writing your own string copying method there, your method does not add a null terminator. 使用
strcpy
复制到tempWord
而不是在那里编写自己的字符串复制方法,该方法不会添加空终止符。
An even better solution would be to have create_StrQueue accept a const char*
parameter, and do the string allocation and copying in there. 更好的解决方案是让create_StrQueue接受
const char*
参数,然后在其中进行字符串分配和复制。
You should also avoid using the word new
since that looks a bit confusing to a c++ programmer. 您还应该避免使用“
new
”一词,因为这会使C ++程序员感到困惑。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.