为什么malloc在函数内部调用时返回空指针?

为什么malloc在函数内部调用时返回空指针?,c,stack,queue,malloc,C,Stack,Queue,Malloc,我编写了一个代码,其中调用了malloc(),但它返回了一个空指针。当我在main()中调用相同的malloc()并传递给函数时,它工作得非常好。所以请告诉我问题出在哪里 这是我的密码。我对函数reverse()中的malloc()有问题。其他函数中的malloc()s工作正常。那个么为什么那个函数中的那个有问题呢。我的电脑有足够的内存,所以这绝对不是问题所在 #include <stdio.h> #include <stdlib.h> typedef struct no

我编写了一个代码,其中调用了
malloc()
,但它返回了一个空指针。当我在
main()
中调用相同的
malloc()
并传递给函数时,它工作得非常好。所以请告诉我问题出在哪里

这是我的密码。我对函数
reverse()
中的
malloc()
有问题。其他函数中的
malloc()
s工作正常。那个么为什么那个函数中的那个有问题呢。我的电脑有足够的内存,所以这绝对不是问题所在

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int data;
    struct node *next;
} SNode;


typedef struct
{
    int count;
    SNode *top;
} Stack;

int isSEmpty(Stack *s)
{
    return (s->count==0);
}

void push(Stack *s, int x)
{
    SNode *temp = (SNode *)malloc(sizeof(SNode));
    temp->data = x;
    temp->next = s->top;
    s->top = temp;
    s->count++;
}

int pop(Stack *s)
{
    if (isSEmpty(s))
    {
        printf("Underflow");
        return -1;
    }
    SNode *temp = s->top;
    s->top = s->top->next;
    int t = temp->data;
    free(temp);
    s->count--;
    return t;
}
typedef struct qnode
{
    int data;
    struct qnode *next, *prev;
} QNode;

typedef struct
{
    QNode *front, *rear;
    int count;
} Queue;

int isQEmpty(Queue *q)
{
    return (q->count==0);
}

void enQueue(Queue *q, int x)
{
    QNode *temp = (QNode *)malloc(sizeof(QNode));
    temp->data = x;
    temp->prev=q->rear;
    temp->next = NULL;
    q->rear->next = temp;
    q->rear = temp;
    q->count++;
    if (q->count==1)
    {
        q->front = q->rear;
    }
}

int deQueue(Queue *q)
{
    if (isQEmpty(q))
    {
        printf("Underflow");
        return -1;
    }
    QNode *temp = q->front;
    q->front = q->front->next;
    int t = temp->data;
    free(temp);
    q->count--;
    return t;
}
void reverse(Queue *q)
{
    Stack *s = (Stack *)malloc(sizeof(Stack));
    s->count = 0;

    while (!isQEmpty(q))
    {
        push(s, deQueue(q));
    }
    while (!isSEmpty(s))
    {
        enQueue(q, pop(s));
    }
}

int main()
{
    char p = 'y';
    Queue *q = (Queue *)malloc(sizeof(Queue));

    q->count = 0;
    while (p =='y')
    {
        printf("Enter data to be Enqueued: ");
        int d;
        scanf("%d", &d);
        enQueue(q, d);
        printf("Do you want to enter more data? y/n:");
        scanf(" %c", &p);
    }
    printf("Original queue Front: %d Rear: %d\n", q->front->data, q->rear->data);
    reverse(q);
    printf("Reversed queue Front: %d Rear: %d", q->front->data, q->rear->data);
    return 0;
}
#包括
#包括
类型定义结构节点
{
int数据;
结构节点*下一步;
}斯诺德;
类型定义结构
{
整数计数;
SNode*top;
}堆叠;
int为空(堆栈*s)
{
返回(s->count==0);
}
无效推送(堆栈*s,整数x)
{
SNode*temp=(SNode*)malloc(sizeof(SNode));
温度->数据=x;
临时->下一个=s->顶部;
s->top=温度;
s->count++;
}
int-pop(堆栈*s)
{
如有空(
{
printf(“下溢”);
返回-1;
}
SNode*temp=s->top;
s->top=s->top->next;
INTT=温度->数据;
免费(临时);
s->计数--;
返回t;
}
类型定义结构qnode
{
int数据;
结构qnode*next,*prev;
}QNode;
类型定义结构
{
QNode*前,*后;
整数计数;
}排队;
int isQEmpty(队列*q)
{
返回(q->count==0);
}
无效排队(队列*q,整数x)
{
QNode*temp=(QNode*)malloc(sizeof(QNode));
温度->数据=x;
温度->前=q->后;
temp->next=NULL;
q->后->下一步=温度;
q->后部=温度;
q->count++;
如果(q->count==1)
{
q->前=q->后;
}
}
int出列(队列*q)
{
if(isQEmpty(q))
{
printf(“下溢”);
返回-1;
}
QNode*temp=q->front;
q->front=q->front->next;
INTT=温度->数据;
免费(临时);
q->计数--;
返回t;
}
无效反向(队列*q)
{
Stack*s=(Stack*)malloc(sizeof(Stack));
s->count=0;
而(!isQEmpty(q))
{
推送(s,出列(q));
}
而(!isSEmpty)
{
排队(q,pop);;
}
}
int main()
{
charp='y';
Queue*q=(Queue*)malloc(sizeof(Queue));
q->count=0;
而(p='y')
{
printf(“输入要排队的数据:”);
int d;
scanf(“%d”、&d);
排队(q,d);
printf(“是否要输入更多数据?是/否:”);
scanf(“%c”、&p);
}
printf(“原始队列前面:%d后面:%d\n”,q->Front->data,q->Rear->data);
反向(q);
printf(“反向队列前面:%d后面:%d”,q->Front->data,q->Rear->data);
返回0;
}

您的程序几乎没有耗尽内存,这就是
malloc()
将返回
NULL
的原因。相反,糟糕的编程风格和凌乱的代码的结合导致了与未初始化内存的访问相关的问题,这是未定义的行为,一旦触发UB,您就无法再预测程序的行为

首先需要修复的是避免这种构造

q->rear->next = temp;
因为
q->rear
可能是
NULL
,因此如果取消引用UB,您将调用它

然后需要显式初始化结构的成员,
malloc()
只分配内存供您使用,它不进行任何初始化,一个好的方法是创建一个分配和初始化空实例的函数,如下所示

Queue *queue_new(int count) 
{
    Queue *queue;
    queue = malloc(sizeof(*queue));
    if (queue == NULL)
        return NULL;
    queue->count = count;
    queue->front = NULL;
    queue->rear = NULL;
    return queue;
}
另外,不要将声明与代码混用。我必须搜索
Queue
的定义来编写上述函数,我使用代码编辑器的find/replace功能来完成这项工作


将所有结构和类型定义放在所有代码之上,以便于查找它们中的任何一个。

您的程序几乎没有耗尽内存,这就是
malloc()
将返回
NULL
的原因。相反,糟糕的编程风格和凌乱的代码的结合导致了与未初始化内存的访问相关的问题,这是未定义的行为,一旦触发UB,您就无法再预测程序的行为

首先需要修复的是避免这种构造

q->rear->next = temp;
因为
q->rear
可能是
NULL
,因此如果取消引用UB,您将调用它

然后需要显式初始化结构的成员,
malloc()
只分配内存供您使用,它不进行任何初始化,一个好的方法是创建一个分配和初始化空实例的函数,如下所示

Queue *queue_new(int count) 
{
    Queue *queue;
    queue = malloc(sizeof(*queue));
    if (queue == NULL)
        return NULL;
    queue->count = count;
    queue->front = NULL;
    queue->rear = NULL;
    return queue;
}
另外,不要将声明与代码混用。我必须搜索
Queue
的定义来编写上述函数,我使用代码编辑器的find/replace功能来完成这项工作


将所有结构和类型定义放在所有代码之上,以便于查找其中任何一个。

您没有初始化在`main()中初始化分配的
*q
结构的所有字段:

然后将该
q
指针传递到
enQueue()
并执行以下操作:

q->rear->next = temp;
我想您也可以使用
q->front
而不必初始化它


这些都是未定义的行为,在您的情况下,可能会破坏堆,导致
malloc()
无法按预期工作。如果您在Linux上工作,valgrind可能会很有用。

您没有初始化在`main()中初始化分配的
*q
结构的所有字段:

然后将该
q
指针传递到
enQueue()
并执行以下操作:

q->rear->next = temp;
我想您也可以使用
q->front
而不必初始化它

这些都是未定义的行为,在您的情况下,可能会破坏堆,导致
malloc()
无法按预期工作。如果您正在使用Linux,valgrind可能会很有用。

您有