在链表中使用INT_MAX查找两个最小值会导致分割错误



我正试图从昨天开始在链表中找到最小两个数字,但仍然无法做到,因此决定在stackoverflow询问。

我这样做的逻辑是:

(1)首先设置min1->freq和min2->freq为INT_MAX,它们都是节点型指针。

(2)然后我设置第二个最小的,然后是第一个最小的,像这样:

while (temp != NULL) 
{
    printf("ncheck2n");       
    if ((temp) -> freq < min2 -> freq)
    {
        printf("check3n");
        min1 = min2;
        min2 = temp;
    } 
    else if ((temp) -> freq < min1 -> freq && (temp) -> freq != min2 -> freq)
    {
        printf("check4n");
        min1 = temp;
    }
    temp = temp -> next;
}
* lmin1 = min1; 
* lmin2 = min2;
错误:

The error i am getting is this :
enter the size of node
4
start entering the number of elements until your size
0
-1
-5
8
Printing linked list
0-> -1-> -5-> 8-> 
check0
Segmentation fault (core dumped)

我通过printf语句手动调试,我发现min2 -> freq = INT_MAX;的初始化正在产生问题(请参阅下面的完整代码),因为它不能打印"check1",只打印"check0"

如果你想看看我的完整代码,那么请找到下面:

#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h>
#include <string.h> 
#include <limits.h>
struct node 
{
    int freq;
    struct node * next;
};
typedef struct node node;
node * tree;
//Problem creating area is below (code for finding minimum two elements)
void find_two_min(node * * List, node * * lmin1, node * * lmin2) 
{
    node * temp = * List;
    node * min1;
    min1 -> freq = INT_MAX;
    node * min2;
    printf("ncheck0n");
    min2 -> freq = INT_MAX;  //This initialisation of INT_MAX to min2->freq creates problem because printf() statment above it works well but don't work below it.
    printf("check1n");
    while (temp != NULL) 
    {
        printf("ncheck2n");
        if ((temp) -> freq < min2 -> freq)
        {
            printf("check3n");
            min1 = min2;
            min2 = temp;
        } 
        else if ((temp) -> freq < min1 -> freq && (temp) -> freq != min2 -> freq)
        {
            printf("check4n");
            min1 = temp;
        }
        temp = temp -> next;
    }
    * lmin1 = min1; 
    * lmin2 = min2;
    printf("check5n"); 
}
//Problem creating area is above//
void main() 
{
    int size, data;
    node * min1;
    node * min2;
    int count = 0; //this count flag is to check is it's first node or not inside the do-while loop.
    tree = NULL;
    printf("enter the size of noden");
    scanf("%d", & size);
    printf("start entering the number of elements until your sizen");
    node * prev;
    do {
        scanf("%d", & data);
        if (count == 0)
        {
            node * temp;
            temp = (node * ) malloc(sizeof(node));
            temp -> freq = data;
            temp -> next = NULL;
            prev = temp;
            tree = prev;
        }
        else 
        {
            node * temp;
            temp = (node * ) malloc(sizeof(node));
            temp -> freq = data;
            temp -> next = NULL;
            prev -> next = temp;
            prev = prev -> next;
        }
        size--;
        ++count;
    }
    while (size > 0);
    printf("Printing linked listn");
    node * temp1;
    temp1 = tree;
    while (temp1 != NULL) 
    {
        printf("%d-> ", temp1 -> freq);
        temp1 = temp1 -> next;
    }
    node * temp5 = tree;
    find_two_min( & temp5, & min1, & min2);
    printf("n The two minimum numbers are  min1 :%d   and min2 : %dn", min1 -> freq, min2 -> freq);
}

谁能帮我纠正在c/c++由于

min2为未分配内存的指针。使用new来分配内存,使用delete来释放内存。

你写的方式:

node * min2;
printf("ncheck0n");
min2 -> freq = INT_MAX;  //This initialisation of INT_MAX to min2->freq creates problem because printf() statment above it works well but don't work below it.
printf("check1n");

解释:

let `min2` be a pointer to a memory area holding a node. The pointer is random.
initialize the member `freq` of the structure pointed to by min2.

结果是尝试写入随机内存,可能会导致段错误。

相关内容

  • 没有找到相关文章

最新更新