复制指针时出现段错误
下面的代码为什么会导致段错误?基本上在我将头指针复制到 temp 后,头指针就消失了。
typedef struct address * paddress; // defines struct pointer
void addAddressToList(paddress head, int addr[])
{
if (head == NULL) {
//head->addrArray = addr; // if list is initially empty
} else {
paddress temp;
temp = head;
while (temp->right != NULL) {
temp = temp->right; // go to end of the list
}
paddress newAddress = (paddress)malloc(sizeof(paddress*));
newAddress->intAddr = addr;
newAddress->right = NULL;
newAddress->left = temp; // connect the new address
temp->right = newAddress;
}
}
main() {
paddress addressListHead;
addressListHead = (paddress)malloc(sizeof(paddress*));
int intAddr1[] = {1,2,3,4,5,6,7};
char hexAddr1[] = "123456";
int intAddr2[] = {16,14,13,12,11};
char hexAddr2[] = "fedcb";
addressListHead->intAddr = intAddr1;
addressListHead->hexAddr = hexAddr1;
addAddressToList(addressListHead, intAddr2);
}
How come the following code result in seg fault? Basically after I copy the head pointer to temp, the head pointer gone.
typedef struct address * paddress; // defines struct pointer
void addAddressToList(paddress head, int addr[])
{
if (head == NULL) {
//head->addrArray = addr; // if list is initially empty
} else {
paddress temp;
temp = head;
while (temp->right != NULL) {
temp = temp->right; // go to end of the list
}
paddress newAddress = (paddress)malloc(sizeof(paddress*));
newAddress->intAddr = addr;
newAddress->right = NULL;
newAddress->left = temp; // connect the new address
temp->right = newAddress;
}
}
main() {
paddress addressListHead;
addressListHead = (paddress)malloc(sizeof(paddress*));
int intAddr1[] = {1,2,3,4,5,6,7};
char hexAddr1[] = "123456";
int intAddr2[] = {16,14,13,12,11};
char hexAddr2[] = "fedcb";
addressListHead->intAddr = intAddr1;
addressListHead->hexAddr = hexAddr1;
addAddressToList(addressListHead, intAddr2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它似乎摆脱了编译错误,您已将 malloc 返回到
paddress
的内容进行了类型转换。addressListHead
是一个指针,这意味着它可以保存对象的地址,但不能指针的地址。 malloc here 语句不会创建对象。您需要在main
和addAddressToList
函数中将其更改为-
。
分段错误:
我知道
paddress::right
是一个指针,您将其与 NULL 进行比较。但是temp::right
被初始化为什么。它指向一些垃圾地址,因此您不能要求它与NULL进行比较。使其指向有效的内存位置。It seems to get rid of the compilation error, you have type casted what malloc is returning to
paddress
.addressListHead
is a pointer, which means it can hold the address of an object but not the address of a pointer. The malloc here statement doesn't create an object. You need to change this -to
in
main
andaddAddressToList
functions.Segmentation fault :
I understand
paddress::right
is a pointer with the fact you are comparing it to NULL. But what istemp::right
is initialized to. It is pointing to some garbage address and so you cannot ask for it to compare with NULL. Make it point to a valid memory location.您的代码中存在多个问题。
首先,通常的建议:停止将
sizeof
与类型名称一起使用(尽可能多)。将sizeof
与表达式一起使用,而不是类型。类型名称属于声明而不是其他地方。如果您使用此
malloc
习惯用法,即
sizeof
应该应用于*p
,其中p
是指向您要分配的数组的指针,n
是该数组中的元素总数。这样,您就不必猜测应该在sizeof
下指定什么类型名称(这样您的代码就变得与类型无关)。在您的情况下,您只分配一个对象,因此代码应该如下所示
(并且不要强制转换
malloc
的结果 - 这样做绝对没有意义)。其次,当你成为列表的头元素时,你需要初始化所有字段。然而,您永远不会在 head 元素中初始化
right
(或left
)。因此,即使分配了正确的内存量也会崩溃。There is more than one problem in your code.
Firstly, the usual advice: stop using
sizeof
with type names (as much as possible). Usesizeof
with expressions, not types. Type names belong in declarations and nowhere else.Your problem with memory allocation could have been prevented if you used this
malloc
idiomi.e.
sizeof
should be applied to*p
, wherep
is the pointer to the array you are allocating andn
is the total number of elements in that array. That way you never have to guess what type name you should specify undersizeof
(an that way your code becomes type-independent).In your case you are allocating just one object, so the code should look as
(And don't cast the result of
malloc
- there's absolutely no point in doing that).Secondly, when you the head element of the list, you need to initialize all the fields. Yet you never initialize
right
(orleft
) in the head element. Hence the crash even when the correct amount of memory is allocated.在 main() 中,您希望
确保获得足够的字节来保存地址。
In main(), you want
That makes sure you get enough bytes to hold an address.
第一个错误:
paddress* 表示指向 paddress 的指针,它本身是指向结构地址的指针。因此 paddress* 是一个指向结构体地址的指针。你会想做:
另外,我发现你昨天犯了类似的错误。 为什么会出现段错误?我想将一个 char 数组指针放入一个结构体
正确理解指针的概念很重要。我肯定会推荐您阅读一些有关指针的教程。如果您需要这方面的帮助,请告诉我。
First error:
paddress* means a pointer to paddress which itself is a pointer to struct address. Hence paddress* is a pointer to a pointer to struct address. You would want to do:
Also, I see that you made a similar mistake yesterday. Why do I get a seg fault? I want to put a char array pointer inside a struct
It's important to understand the concept of pointers properly. I would definitely recommend you to go through some tutorials on pointers. If you need help with that, let me know.