首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么可能是错误的原因"malloc:*对象0x7b:指针未被分配“

什么可能是错误的原因"malloc:*对象0x7b:指针未被分配“
EN

Stack Overflow用户
提问于 2012-10-14 21:14:28
回答 2查看 757关注 0票数 1

我正在为一堂课写这个程序。这个程序是一个简单的括号检查应用程序使用堆栈。在本例中,我使用了静态数组来实现堆栈数据结构。这个程序构建得很好,但是我遇到了这个运行时错误,到目前为止还没有能够看到问题的根源所在。

以下是我几乎不了解的信息,即问题可能是由于试图释放未使用malloc分配的内存造成的。但是在下面的代码中,我不知道这会发生在哪里。

下面是使用静态数组的堆栈的接口实现代码。我还添加了堆栈接口代码以在堆栈上添加字符,并添加了驱动程序来测试例程。

代码语言:javascript
复制
#include "stack.h"
#include "stdlib.h" /* malloc, free */
#include "stdio.h"

#define MAXSTACKSIZE 10

struct stack_record {
  generic_ptr base[MAXSTACKSIZE];
  generic_ptr * top;
};

unsigned int num_elements(stack * const p_S)
{
  unsigned int numels = ((*p_S)->top - (*p_S)->base);
  /*  return ((*p_S)->top - (*p_S)->base); */
  printf("number of elements in the stack is: %u\n", numels);
  return numels;
}
status init_stack(stack * const p_S)
{
  stack_record * record = (stack_record *)malloc(sizeof(stack_record));
  if(record == NULL) {
    return ERROR;
  }
  record->top = record->base;
  *p_S = record;
  return OK;
}
bool empty_stack(stack * const p_S)
{
  if(num_elements(p_S) == 0)
    {
      printf("stack is EMPTY!\n");
      return TRUE;
    }
  else
    {
      printf("stack NOT Empty!\n");
      return FALSE;
    }
}
status push(stack * const p_S, generic_ptr const data)
{
  if (num_elements(p_S) == MAXSTACKSIZE)
    return ERROR;

  *( (*p_S)->top ) = data;
  (*p_S)->top++;
   return OK;
}

status pop(stack * const p_S, generic_ptr * const p_data)
{
  if (empty_stack(p_S) == TRUE)
    return ERROR;
  *p_data = *((*p_S)->top);
  (*p_S)->top--;
  /*  ((*p_S)->top)--; */
  return OK;
}

status top(stack *const p_S, generic_ptr * const p_data)
{
   if (pop(p_S, p_data) == ERROR)
    return ERROR;

  return push(p_S, *p_data);
}

void destroy_stack(stack * const p_S, void (* const p_func_f)())
{
  if ((p_func_f) != NULL) {
    generic_ptr * curr;    
    for(curr = (*p_S)->top;
    curr != (*p_S)->base;
    ++curr)
          (*p_func_f)(*curr);
  }
  free(*p_S); /*free the dynamically allocated memory on the heap */
  *p_S = NULL;
 }

以下是字符的堆栈接口例程:

代码语言:javascript
复制
/*************************************************************************/
/* adapted from Esakov and Weiss, Section 5.2                            */
/*************************************************************************/

#include "char_stack.h"
#include "stdlib.h" /* malloc */
#include "stdio.h"
#define DEBUG 1 

status push_char(stack * const p_S, char const c)
{
  /*
   *     Push the character c onto the stack.
   */
  char * p_c = (char *) malloc(sizeof(char));

  if(p_c == NULL)
    return ERROR;
  *p_c = c;

  if(DEBUG)
  /* Debug code: begin */
  {
    printf("Character to PUSH on the stack: %c\n", *p_c);
  }
  /* Debug code: end */

  if (push(p_S, (generic_ptr)p_c) == ERROR) {
    if(DEBUG)
      printf("char_stack: push_char: failed to push the character on the stack!\n");
    free(p_c);
    return ERROR;
  }
  return OK;
}

status pop_char(stack * const p_S, char * const p_c)
{
  /*
   *     Pop the stack. Return the character in p_c.
   */

  generic_ptr p_data;

  if( pop(p_S, &p_data) == ERROR)
    return ERROR;

  *p_c = *((char*)p_data);

  if(DEBUG)
  /*Debug code: begin */
printf("char_stack.c::pop_char: Character to POP on the stack: %c\n", *p_c);

  /*Debug code: end */

   free(p_data);
   return OK;
}

status top_char(stack * const p_S, char * const p_c)
{
  /*
   *     Return the top character from the stack in p_c
   */
  generic_ptr p_data;

  if (top(p_S, &p_data) == ERROR)
    return ERROR;

  *p_c = *((char *)p_data); 
  return OK;
}

应用程序的驱动程序:

代码语言:javascript
复制
/**************************************************************************/
/* adapted from Esakov and Weiss, Section 5.2                             */
/**************************************************************************/

#include "stdio.h"
#include "stdlib.h"
#include "char_stack.h"

char matching_symbol(char const c)
{
  switch(c) {
    case '(': return ')';
    case ')': return '(';
    case '}': return '{';
    case '{': return '}';
    case '[': return ']';
    case ']': return '[';
  }
  return 0;
}

status consume_char(stack * const p_S, char input)
{
  switch (input) {
    case '(':
    case '{':  
    case '[':
      return push_char(p_S, input); 
    case ')':
    case '}':
    case ']':
      { 
        char c;    
        if (pop_char(p_S, &c) == ERROR || c != matching_symbol(input)) { 
          return ERROR;
        } else {
          return OK;
        }
      }
    default:
      return OK; 
  }
}

int main(int argc, char * argv[])
{

  if (argc == 1) {
    exit(EXIT_SUCCESS);
  }

  {
    stack S;

    init_stack(&S);

    {
      char *ptr;
      for (ptr = argv[1];
           *ptr != '\0' && consume_char(&S, *ptr) == OK;
           ++ptr); 
      if (*ptr == '\0' && empty_stack(&S)) { 

    destroy_stack(&S,free);
        exit(EXIT_SUCCESS);
      } else {
    destroy_stack(&S,free);
        exit(EXIT_FAILURE);
      }
    }
  }  

  exit(EXIT_SUCCESS);  
}

我想补充的是,在驱动程序中,我缩小了对销毁堆栈的调用是触发此问题的原因。但是我已经检查了这个代码,并且相信这个代码很好,问题在其他地方。

编辑1:为了完整起见,我附加了调用这些函数的驱动程序代码。编辑2:添加了字符的堆栈接口例程。

EN

回答 2

Stack Overflow用户

发布于 2012-10-14 21:20:27

错误消息告诉您将0x7b值传递给free。这么小的数目永远不会是从malloc返回的内存块的地址。因此,大致来说,有三种可能性:

  1. 您有一个完全未初始化的指针,该指针恰好包含了0x7b,并且将其传递给free
  2. 从包含0的指针开始,将其增量为0x7b,并将其传递给free
  3. 您有一些更有创造性的内存损坏错误,它用0x7b替换了一个有效的指针值。

最快的解决办法是运行你的程序,让它告诉你这一切是在哪里发生的。您还可以在gdb中到处放置断点,并查看所看到的内容。

票数 1
EN

Stack Overflow用户

发布于 2012-10-15 05:04:24

main函数的末尾,将free作为函数指针传递给destroy_stack的第二个参数。这样做的目的是free当前堆栈上的所有数据元素(如果还有剩余的话)。但是,只有当push函数在将项添加到堆栈时使用malloc分配内存时,才允许释放此内存。

在给定的实现中,堆栈不负责为项分配内存,因此它可能不应该试图释放该内存。在任何一种情况下,查找差异的位置可能都在函数push_char中。

看起来,push_char正在尝试推送一个简单的“{”字符(BTW这是0x7B),而不是像堆栈所期望的那样指向malloc创建的内存的指针。

更新

删除我在上一段中提到的关于push_char的内容--问题是在destroy_stack中,这一行:

代码语言:javascript
复制
(*p_func_f)(*curr);    /* Incorrect */

curr是指向为单个字符分配的内存的指针(由push_char分配),*p_func_ffree()函数。你能看出问题所在吗?

当将curr传递给*p_func_f时,取消引用是一个错误。相反,只需传递原始指针:

代码语言:javascript
复制
(*p_func_f)(curr);     /* Correct */

发现这一点的关键与我前面提到的一样:“内存地址”0x7b实际上是字符'{',它是存储在堆栈上的数据--这强烈地表明free函数接收的是原始数据,而不是指向该数据的指针。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12886591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档