ShauryaH4X's blog

By ShauryaH4X, history, 11 days ago, In English
#include<stdio.h>
#include<stdlib.h>

typedef int itype;

typedef struct Cq{
    int size,count,front,rear;
    itype *a;
}Cq;

void initialize(Cq * ref,int n) {
    ref->count=0;
    ref->size=n;
    ref->front=0;
    ref->rear=-1;
    ref->a=(int *)malloc(n*sizeof(int));
    if(ref->a==NULL) {
        printf("Memory not allocated");
        exit(1);
    }
    printf("1");
}

char isfull(Cq *ref) {
    return ref->count==ref->size;
}

char isempty(Cq *ref) {
    return ref->count==0;
}

void insert(Cq *ref,itype x) {
    if(isfull(ref)) {
        printf("Queue full");
        exit(2);
    }
    ref->rear=(ref->rear+1)%(ref->size);
    ref->a[ref->rear]=x;
    ref->count=ref->count+1;
    printf("2");
}
int size(Cq *ref) {
    return ref->count;
}

int main() {
    Cq a;
    initialize(&a,5);
    insert(&a,10);
    insert(&a,20);
    insert(&a,30);
    insert(&a,40);
    return 0;
}

I was asked to make circular queue with count in my proficiency test today but it was giving me error. Can you help me find out error in this code.

Program 'Cq.exe' failed to run: Insufficient system resources exist to complete the requested serviceAt line:1 char:84
+ ... rogramming\codechef\" ; if ($$$?) { gcc Cq.c -o Cq } ; if ($$$?) { .\Cq }
+                                                                    ~~~~.
At line:1 char:84
+ ... rogramming\codechef\" ; if ($$$?) { gcc Cq.c -o Cq } ; if ($$$?) { .\Cq }
+                                                                    ~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

Looks like segmentation fault but where

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
11 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ShauryaH4X (previous revision, new revision, compare).

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Well -1 is not an index in c++.

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    First I am incrementing the rear then modulo by the size. After that I am assigning the value it shouldn't wrong.