0

I have written this code and am confused about why is it showing segmentation fault. I think my dynamic memory allocation is causing problems for me here. Can anyone tell me what is causing the segmentation fault in here and how to improve the code.

Also, please tell me if I can create the object with the ClassName obj(); and store it in the stack instead of the heap. Or would this implementation be needed in some problems

#include<bits/stdc++.h>
using namespace std;

class Queue
{
public:
    int rear, front, size,capacity;
    int* arr;
     
    Queue(int c)
    {
        capacity=c;
        rear=c-1;
        front=0;
        int *arr= new int[c*sizeof(int)];
    }
};


int isEmpty(Queue* queue)
{
    return (queue->size==0);
}

int isFull(Queue* queue)
{
    return (queue->size==queue->capacity);
}


void enqueue(Queue* queue, int x)
{

    if(isFull(queue))
        return;
    queue->rear=(queue->rear+1)%queue->capacity;
    queue->arr[queue->rear]=x;
    queue->size+=1;
}

int dequeue (Queue* queue)
{
    
    if(isEmpty(queue))
        return 0;
     int x = queue->arr[queue->front];
    queue->front= (queue->front+1)%queue->capacity;
   
    queue->size-=1;
}

int front (Queue* queue)
{

    if(isEmpty(queue))
        return 0;
    return queue->arr[queue->front];
}

int rear (Queue* queue)
{
    if(isEmpty(queue))
    return INT_MIN;
    return queue->arr[queue->rear];
}

int main()
{
    Queue* queue=new Queue();

    enqueue(queue,10);
    enqueue(queue,20);
    enqueue(queue,30);
    enqueue(queue,40); 
  
    cout << "Front item is "
         << front(queue) << endl; 
    cout << "Rear item is "
         << rear(queue) << endl; 
}
7
  • 3
    I got compilation error instead of Segmentation Fault. Commented Sep 1, 2020 at 15:40
  • What is causing this can you tell me? I doubt my dynamic allocation of Queue I feel. Commented Sep 1, 2020 at 15:41
  • please don't confuse different versions of your code / please only one question per question. I suppose Queue* queue=new Queue(); is something you added later and only without you got a segmentation fault. It is not possible to get a segfault with the posted code Commented Sep 1, 2020 at 15:44
  • By the way, why are you declareing the functions to deal with the queue as dependent functions instead of member functions of Queue? Commented Sep 1, 2020 at 15:48
  • The Queue queue = new Queue(); was always there and I am getting seg fault with it. Just tried running again and it is showing the same thing. Commented Sep 1, 2020 at 15:48

1 Answer 1

1

Your code have at least 3 problems:

Firstly,

Queue* queue=new Queue();

will lead to compilation error because no default constructor is defined and another constructor is defined in the class Queue.

To fix this, you should do one of:

  • Change this line to match the defined constructor like Queue* queue=new Queue(1024);
  • Add default constructor to the class Queue
  • Add default value of the argument c for the constructor of the class Queue like Queue(int c = 1024)

Secondly, the function dequeue have an execution path in which the execution reach at end of funciton without executing any return statement.

It seems return x; should be added at end of the function.

Thirdly, the line

int *arr= new int[c*sizeof(int)];

is bad because:

  • This stores the pointer to local variable that will vanish at end of this constructor instead of the member variable.
  • You don't need to multiply sizeof(int) because what to specify is the number of elements to allocate, not number of bytes.

The line should be

arr= new int[c];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.