分段故障调试帮助

分段故障调试帮助

我正在尝试实现一个简单的堆栈。它在MacOSX上运行良好,但在linux(Ubuntu)上崩溃。有人可以帮我知道为什么吗?

MYStack.h

#ifndef MYSTACK_H_
#define MYSTACK_H_

typedef struct Element *element;
typedef struct Stack *stack;

struct Element {
    struct Element *next;
    void *data;
};

struct Stack {
    struct Element *head;
    unsigned int count;
    void (*dump) (void *);
};

/* Define boolean type */
typedef signed char     bool;
#define YES             (bool)1
#define NO              (bool)0

/* utility macro */
#define ELEMENT_NEXT(E)     ((E) = (E)->next)
#define ELEMENT_DATA(E)     ((E)->data)
#define STACK_HEAD(S)       ((S)->head)
#define STACK_SIZE(S)       ((S)->count)

/* Functions prototypes */
bool push( stack , void * );
bool pop( stack , void ** );
bool create_stack( stack , void (*) (void*) );
bool delete_stack( stack  );
void dump_stack( stack );

#endif /* MYSTACK_H_ */


MYStack.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "MYStack.h"

/* Creating the stack */
bool create_stack( stack new_stack, void (*dump_function) (void*))
{
    new_stack->head = NULL;
    new_stack->dump = dump_function;
    new_stack->count = 0;

    return YES;
}

/* Deleting the stack */
bool delete_stack( stack this_stack )
{
    element next_element;

    while (this_stack->head) {
        next_element = this_stack->head->next;
        free(this_stack->head->next);
        this_stack->head = next_element;
    }

    return YES;
}

/* Dump the stack */
void dump_stack(stack this_stack)
{
    element e;
    int i;

    e = STACK_HEAD(this_stack);

    if (this_stack->dump ) {
        for (i = 0; i < this_stack->count; i++) {
            (this_stack->dump) (e->data);
            if (e->next != NULL)
                e = e->next;
        }
    }
}

/* Adding element to the stack */
bool push( stack this_stack, void *data )
{
    element new_element;
    void (*temp_dump) (void *);

    this_stack->dump = temp_dump;

    new_element = (element ) malloc(sizeof(element *));

    if (new_element == NULL) {
        fprintf(stdout, "malloc() new element failed : %d\n", errno);

        return NO;
    }

    new_element->data = data;
    new_element->next = (this_stack)->head;
    (this_stack)->head = new_element;
    this_stack->dump = temp_dump;
    (this_stack)->count++;

#ifdef DEBUG
    fprintf(stdout, "Inserting at the stack in the address %p\n", new_element->data);
#endif

    return YES;
}

/* Remving element from the stack */
bool pop( stack this_stack, void **data )
{
    element delete_me;
    void (*temp_dump) (void *);

    this_stack->dump = temp_dump;
    delete_me = this_stack->head;
    if (delete_me == NULL) {
        fprintf(stdout, "stack is empty\n");
        return NO;
    }

    *data = delete_me->data;
    this_stack->head = delete_me->next;
    this_stack->dump = temp_dump;
    this_stack->count--;

    free(delete_me);

#ifdef DEBUG
    fprintf(stdout, "Removing from the stack in the address %p\n", delete_me->data);
#endif

    return YES;
}

/* Dump function test */
void dump_ints(void* data)
{
    int* int_data_ptr = (int*)data;
    printf("We are dumping an int data : %d\n", *int_data_ptr);
}

/* To test our stack */
int main (int argc, char const **argv)
{
    stack new_stack;
    void (*dump_func_ptr) (void*);
    int i;
    int stack_ints[] = {1, 2, 3, 4};
    void *deleted_item;

#ifdef DEBUG
    fprintf(stdout, "We are in debug mode...\n");
#endif

    new_stack = (stack) malloc(sizeof(stack *));
    dump_func_ptr = dump_ints;
    create_stack( new_stack, dump_func_ptr);
    /* Insert to the stack */
    for (i = 0; i < 4; i++)
        push( new_stack, &stack_ints[i]);

    /* Print the number of elements */
    printf("Our stack contain %d elements\n", new_stack->count);
    /* Dump the stack */
    dump_stack(new_stack);
    /* Removing some data */
    pop(new_stack, &deleted_item);
    /* Dump the stack */
    dump_stack(new_stack);
    /* Deleting the stack */
    //delete_stack(new_stack);

    free(new_stack);

    return 0;
}

最佳答案

有几个不正确的分配。它们仅分配一个指针大小的内存(在32位系统上为4个字节),并且随后对那些内存的初始化将覆盖已分配内存的末尾。我注意到的是:

new_element = (element ) malloc(sizeof(element *));




new_stack = (stack) malloc(sizeof(stack *));


它们应该是这样的:

new_element = (element ) malloc(sizeof(struct Element));
new_stack = (stack) malloc(sizeof(struct Stack));

关于c - 分段故障调试帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7211476/

10-10 18:22