使用免费功能链接列表时出现内存错误

时间:2019-06-17 21:34:05

标签: c

内存错误,释放了一个包含int和next的链表。

我尝试了代码开头显示的以下功能。

该程序接收链表中节点的数量以及每个节点的值。接下来,程序要求列表的旋转数(k),并将列表k数向左旋转。该功能在释放分配的内存之前效果很好。 该错误发生在空闲(临时)行“调试错误HEAP CORRUPTION DETECTED”中。

#include <stdio.h>
#include <stdlib.h>

typedef struct IntNode
{
    int val;
    struct IntNode* next;
} IntNode;


void printList(IntNode* list);
void freeList(IntNode* head);
IntNode* createNode(int val);
void moveKPlaces(IntNode** list, int k);
int numNodes = 0;
int main(void)
{
    IntNode* list = NULL;
    IntNode* curr = list;
    IntNode* newNode = NULL;
    int i = 0, num = 0, k = 0;
    printf("How many nodes in list? ");
    scanf("%d", &numNodes);
    getchar();
    for (i = 0; i < numNodes; i++)
    {
        printf("Enter number: ");
        scanf("%d", &num);
        getchar();
        if (i == 0)//head of the list
        {
            newNode = createNode(num);
            list = newNode;
            curr = list;
        }
        else
        {
            while (curr->next != NULL)
            {
                curr = curr->next;
            }
            newNode = createNode(num);
            curr->next = newNode;
            newNode->next = NULL;
        }
    }
    printf("Choose a number k, and the list will be rotated k places to the left: ");
    scanf("%d", &k);
    getchar();
    printf("The rotated list:\n");
    moveKPlaces(&list, k);
    printList(list);
    freeList(list);
    getchar();
    return 0;
}

/*
    This function recieves a pointer to a pointer to the head of a list and
    a number (k) and rotate the list k places to the left.
    input:
    a pointer to a pointer to the head of a list and
    a number (k)
    output:
    none
*/
void moveKPlaces(IntNode** list, int k)
{
    IntNode* curr = *list;
    IntNode* last = NULL;
    IntNode* head = *list;
    int placeNode = 0;
    while (curr->next != NULL)
    {
        curr = curr->next;
    }
    curr->next = head;//turn it to a circular list
    while (placeNode < k)
    {
        curr = curr->next;
        placeNode++;
    }
    *list = curr->next;// the k node will be the head
    curr->next = NULL;// the one before will be the last

}
//************************************
// Method:    printList
// Returns:   void
// Description: prints list recursively
// Parameter: IntNode * list
//************************************
void printList(IntNode* list)
{
    if (list)
    {
        printf("%d  ", list->val);
        printList(list->next);
    }
    else
    {
        printf("\n");
    }
}


    void freeList(IntNode* head)
    {
        IntNode* temp = NULL;
        IntNode* curr = head;
        while (curr)
        {
            temp = curr;
            curr = (curr)->next;
            free(temp);
        }

        head = NULL;
    }
IntNode* createNode(int val)
{
    IntNode* newNode = (IntNode*)malloc(sizeof(newNode));//will alocate every person node dinamically
    newNode->val = val;
    // insert all details
    newNode->next = NULL;
    return newNode;
}

期望免费,没有任何错误 “调试错误已检测到HEAP CORRUPTION”。

1 个答案:

答案 0 :(得分:3)

IntNode* newNode = (IntNode*)malloc(sizeof(newNode));//will alocate every person node dinamically

那没有分配足够的空间。应该是sizeof(*newNode)sizeof(IntNode)