【问题标题】:Two Digit Pointer? How did this happen?两位数指针?这怎么发生的?
【发布时间】:2012-11-05 08:08:50
【问题描述】:

所以在我正在进行的一个项目中,我遇到了以下错误:

*** glibc detected *** ./social_network: munmap_chunk(): invalid pointer: 0x09a913b0 ***

一些 printf 语句显示我正在释放的导致此错误的结构的内存地址为....17。那么这怎么可能呢?会发生什么让这看起来...?

这是崩溃前的一些输出:

temp is: 162075592
temp user is  162075408
After free of temp
inside while loop, first line
before free of temp
temp is: 162075760
temp user is  162075480
After free of temp
inside while loop, first line
before free of temp
temp is: 162075568
temp user is  17

这是输出输出的代码

 21 void destroy() {
 22 
 23     struct user_node *node;
 24 
 25     struct friend_node *temp;
 26     struct friend_node *next_temp;
 27     
 28     //iterate over the list of users
 29     while (*head != NULL) {
 30         printf("before a_friend\n");
 31         temp = (*head)->a_friend;
 32         
 33         //Iterate over friends of users, free friend nodes
 34         while (temp != NULL) {
 35             printf("inside while loop, first line\n");
 36             next_temp = temp->next_friend;
 37             printf("before free of temp\n");
 38             printf("temp is: %d\n", (int) temp);
 39             printf("temp user is  %d\n", (int)temp->user);
 40             free(temp); <==================================SEGFAULT IS HERE!!!!!!!!!!!!!
 41             printf("After free of temp\n");
 42             temp = next_temp;
 43             next_temp = NULL;
 44         }
 45 
 46         node = (*head)->next_user;
 47         printf("before free of: %s\n", (*head)->name);
 48         free(*head);
 49         printf("after free of that person...\n");
 50         *head = node;
 51         printf("*head = node afterwards\n");
 52         node = NULL;
 53     }
 54     printf("before final free...\n");
 55     free(head);
 56 }

编辑:

结构朋友节点

 23 //node used to store a pointer to a user_node and next friend
 24 struct friend_node {
 25     struct friend_node *next_friend;
 26     struct user_node *user;
 27 }; 

结构用户节点

 12 //Struct definitions
 13 //node used to store information about a user
 14 struct user_node {
 15     char name[21];
 16     int age;
 17     int gender;
 18     char location[21];
 19     struct friend_node *a_friend;
 20     struct user_node *next_user;
 21 };
 22    

【问题讨论】:

  • 请问struct friend_node的定义好吗?
  • 确定,上面添加了structfriend_node和struct user_node定义
  • 如果您有用户 A、B、C - C 是 A 和 B 的朋友。在为用户 A 和用户 B 创建朋友列表时,您是否为 C 为 A 创建单独的朋友包装器& B 还是你在两个地方都重复使用同一个朋友包装器?
  • 是的,使用了不同的朋友包装器。
  • @Ethan 附注:当您打印指针时,请使用“%p”。否则,您假设指针适合 int

标签: c pointers memory-management free


【解决方案1】:

这通常发生在您使用 NULL 指针时。你看,当你访问一个结构的成员时,它的偏移量被添加到指针上,然后被取消引用:

somestruct *p = NULL;
printf("%d", p->member);

如果成员处于偏移量,例如0xC,那么你就不会收到“尝试访问0x0处的内存”这样的错误,而是“尝试访问0xC处的内存”。

编辑:正如其他人所建议的,这可能不是您的问题的原因。但这是在“两位数指针”处收到错误的常见原因之一。

【讨论】:

  • 如果p 等于NULL free(p) 不会导致分段冲突,因为标准free(NULL) 是有效的。
  • AndreyT:我说的是代码中的错误消息(段错误),而不是打印的值。您收到有关在 0xC 访问内存的错误。
  • @AndreyT 通过偏移结构指针not 传递给printf() 的值究竟会如何利用所述指针中的地址来获取值?在上面的示例中,由于在 NULL+(成员的偏移量)处的内存访问,会发生错误 。它不会发生在 printf();它会发生在 this 调用代码中。
  • @alk free(NULL) 当然是定义的行为。 temp = ((struct node *)(NULL))-&gt;next_node 在此之前肯定不是免费的,我相信这是 kuba 的观点。这实际上是您通过 NULL 指针访问 member-var 所获得的结果,并且几乎总是在崩溃日志中的位置“0+offsetofmember”处报告为访问错误。
  • @WhozCraig:好的,很公平。但在那种情况下,我看不到相关性。首先,我们知道指针不为空。其次,尝试访问该字段没有崩溃(即成功读取了17 的垃圾值)。 IE。我们不会在这里处理将抵消 null (至少不是立即)。
【解决方案2】:

确保在创建/分配struct 时将所有struct 成员初始化为指向NULL 的指针。

【讨论】:

    【解决方案3】:

    可能发生了什么让这个出现

    有很多种未定义的行为。缓冲区溢出、对已释放对象的访问、未初始化的变量都会浮现在脑海中。

    在寻找这些东西时,valgrind 是你最好的朋友。尽早并经常使用它。

    【讨论】:

      【解决方案4】:

      我在您的代码中没有看到任何暗示您正在释放地址17 的结构,正如您似乎错误地相信的那样。

      崩溃消息和您的转储都清楚地表明您正在尝试释放指向地址162075568 (0x09a913b0) 的指针temp。该指针指向无效内存,这就是free 崩溃的原因。这也是为什么您可能会看到 17 存储在该内存中的原因。那个内存是无效的,所以它存储随机垃圾也就不足为奇了。

      忘记那个17。它与任何事情无关。你的问题是162075568 (0x09a913b0)。此值无效,这就是问题的根源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多