复制指针时出现段错误

发布于 2024-12-08 06:06:59 字数 1014 浏览 0 评论 0原文

下面的代码为什么会导致段错误?基本上在我将头指针复制到 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 技术交流群。

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

发布评论

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

评论(4

情绪操控生活 2024-12-15 06:06:59
paddress addressListHead;
addressListHead = (paddress)malloc(sizeof(paddress*));

它似乎摆脱了编译错误,您已将 malloc 返回到 paddress 的内容进行了类型转换。 addressListHead 是一个指针,这意味着它可以保存对象的地址,但不能指针的地址。 malloc here 语句不会创建对象。您需要在 mainaddAddressToList 函数中将其更改为

addressListHead = (paddress)malloc(sizeof(paddress*));

-

addressListHead = (paddress)malloc(sizeof(struct address));

分段错误:

else {
  paddress temp;
  temp = head;
  while (temp->right != NULL) {
    temp = temp->right;  // go to end of the list
}

我知道 paddress::right 是一个指针,您将其与 NULL 进行比较。但是 temp::right 被初始化为什么。它指向一些垃圾地址,因此您不能要求它与NULL进行比较。使其指向有效的内存位置。

paddress addressListHead;
addressListHead = (paddress)malloc(sizeof(paddress*));

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 -

addressListHead = (paddress)malloc(sizeof(paddress*));

to

addressListHead = (paddress)malloc(sizeof(struct address));

in main and addAddressToList functions.

Segmentation fault :

else {
  paddress temp;
  temp = head;
  while (temp->right != NULL) {
    temp = temp->right;  // go to end of the list
}

I understand paddress::right is a pointer with the fact you are comparing it to NULL. But what is temp::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.

红ご颜醉 2024-12-15 06:06:59

您的代码中存在多个问题。

首先,通常的建议:停止将 sizeof 与类型名称一起使用(尽可能多)。将 sizeof表达式一起使用,而不是类型。类型名称属于声明而不是其他地方。

如果您使用此 malloc 习惯用法

T *p = malloc(n * sizeof *p);

,即 sizeof 应该应用于 *p,其中 p 是指向您要分配的数组的指针,n 是该数组中的元素总数。这样,您就不必猜测应该在 sizeof 下指定什么类型名称(这样您的代码就变得与类型无关)。

在您的情况下,您只分配一个对象,因此代码应该如下所示

paddress newAddress = malloc(sizeof *newAddress);

(并且不要强制转换 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). Use sizeof 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 idiom

T *p = malloc(n * sizeof *p);

i.e. sizeof should be applied to *p, where p is the pointer to the array you are allocating and n is the total number of elements in that array. That way you never have to guess what type name you should specify under sizeof (an that way your code becomes type-independent).

In your case you are allocating just one object, so the code should look as

paddress newAddress = malloc(sizeof *newAddress);

(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 (or left) in the head element. Hence the crash even when the correct amount of memory is allocated.

挽清梦 2024-12-15 06:06:59

在 main() 中,您希望

addressListHead = (paddress)malloc(sizeof(address));

确保获得足够的字节来保存地址。

In main(), you want

addressListHead = (paddress)malloc(sizeof(address));

That makes sure you get enough bytes to hold an address.

夜吻♂芭芘 2024-12-15 06:06:59

第一个错误:

addressListHead = (paddress)malloc(sizeof(paddress*));

paddress* 表示指向 paddress 的指针,它本身是指向结构地址的指针。因此 paddress* 是一个指向结构体地址的指针。你会想做:

addressListHead = (paddress)malloc(sizeof(struct address));

另外,我发现你昨天犯了类似的错误。 为什么会出现段错误?我想将一个 char 数组指针放入一个结构体

正确理解指针的概念很重要。我肯定会推荐您阅读一些有关指针的教程。如果您需要这方面的帮助,请告诉我。

First error:

addressListHead = (paddress)malloc(sizeof(paddress*));

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:

addressListHead = (paddress)malloc(sizeof(struct address));

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.

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