Reputation: 153
I made a code in which malloc()
is called, but it is returning a null pointer. When I call the same malloc()
in main()
and pass to the function, it is working totally fine. So please tell me what is the problem.
Here is my code. I am having problems with the malloc()
in the function reverse()
. The malloc()
s in other functions are working fine. So why is there problem with the one in that function. I have enough memory in my computer, so that's definitely not the problem.
#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;
}
Upvotes: 1
Views: 2075
Reputation: 340198
You aren't initializing all the fields of the *q
struct that you initiallay allocate in `main():
Queue *q = (Queue *)malloc(sizeof(Queue));
q->count = 0;
Then you pass that q
pointer to enQueue()
and do things like:
q->rear->next = temp;
I think you may also use q->front
without having initialized it.
These things are undefined behavior and in your case are probably corrupting the heap causing malloc()
to not work as you expect. If you're working on Linux valgrind might be useful.
Upvotes: 3
Reputation: 53006
Your program is hardly running out of memory, which is why malloc()
would return NULL
. Instead a combination of bad programming style and messy code, is causing problems related to access of uninitialized memory which is undefined behavior, once you trigger the UB you can't predict program's behavior anymore.
The first thing you need to fix, is avoiding this kind of construction
q->rear->next = temp;
because q->rear
might be NULL
and thus you would invoke UB if you dereference it.
Then you need to initialize the members of the struct explicitly, malloc()
only allocates memory for you to use, it does no initialization whatsoever, a good method to do it would be to create a function that allocates and initializes empty instances, like the following
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;
}
Also, don't mix declarations with code. I had to search for the definition of Queue
to write the above function, and I did so with the find/replace feature of my code editor.
Place all structure and type definitions together above all the code, to make it easy to find any one of them.
Upvotes: 5