I'm trying a C programming code which will convert a decimal number into binary number and the binary values will be stored in a stack
According to my code, when running it shows some error responses. When trying to display the binary number using peek method, the application runs without an end.
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef enum{FALSE, TRUE} boolean;
typedef struct stack{
int top;
int a[MAX];
} stack;
void CreateStack(stack *s){
s->top = -1;
}
boolean isEmpty(stack *s){
return (s->top == -1);
}
boolean isFull(stack *s){
return(s->top == MAX - 1);
}
void push(stack *s, int data){
if(isFull(s)){
exit(1);
}
else{
s->top = s->top + 1;
s->a[s->top] = data;
}
}
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
return s->a[s->top];
s->top = s->top - 1;
}
}
int peek(stack *s){
return s->a[s->top];
}
void binary(stack *s, int num){
int n;
while(num != 0){
if(!isFull(s)){
n = num % 2;
push(s,n);
num = num / 2;
}
else{
exit(1);
}
}
}
void main() {
stack s;
CreateStack(&s);
int num,n;
printf("Enter the decimal number: ");
scanf("%d",&num);
binary(&s,num);
printf("Top = %d\n",peek(&s));
while(!isEmpty(&s)){
printf("%d ",pop(&s));
}
}
pop() contains dead code — it returns value but top decrement placed after the return operator and will be newer reached.
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
return s->a[s->top];
s->top = s->top - 1; // <------------
}
}
Change it in following manner:
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
int temp = s->a[s->top];
s->top = s->top - 1;
return temp;
}
}
p.s. As for me, else keyword can be omitted here but it is taste issue:
int pop(stack *s) {
int temp;
if(isEmpty(s))
exit(1);
temp = s->a[s->top];
s->top = s->top - 1;
return temp;
}
Related
I have initialised two stacks using a structure with which I am creating a queue. But the stack is not able to store the values which is why enqueue or dequeue operations are not working properly.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int top;
int size;
int *s;
};
int isfull(struct stack *st){
if(st->top==st->size-1){
return 1;
}
return 0;
}
int isempty(struct stack *st){
if(st->top==-1){
return 1;
}
return 0;
}
void push(struct stack *st,int x){
if(isfull(st)){
printf("FULL!!\n");
}
else{
st->top++;
st->s[st->top]=x;
}
}
int pop(struct stack *st){
int x=-1;
if(isempty(st)){
printf("EMPTY!!\n");
}
else{
x=st->s[st->top];
st->top--;
}
return x;
}
void enqueue(struct stack s1,int x){
push(&s1,x);
}
int dequeue(struct stack s1,struct stack s2){
int x=-1;
if(isempty(&s2)){
if(isempty(&s1)){
printf("QUEUE IS EMPTY!!\n");
return x;
}
else{
while(!isempty(&s1)){
push(&s2,pop(&s1));
}
}
}
return pop(&s2);
}
void display(struct stack st){
int i;
for(i=0;i<=st.top;i++){
printf("%d",st.s[i]);
}
}
int main(){
int n,choice;
struct stack s1,s2;
printf("ENTER SIZE OF QUEUE:");
scanf("%d",&n);
s1.size=n;
s2.size=n;
s1.top=-1;
s2.top=-1;
s1.s=(int *)malloc(s1.size*sizeof(int));
s2.s=(int *)malloc(s2.size*sizeof(int));
while(1){
printf("1.ENQUEUE\n");
printf("2.DEQUEUE\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice){
case(1):
int x;
printf("ENTER DATA:");
scanf("%d",&x);
enqueue(s1,x);
break;
case(2):
int m;
m=dequeue(s1,s2);
printf("ELEMENT DELETED IS:%d\n",m);
break;
case(3):
display(s2);
break;
case(4):
exit(0);
}
}
return 0;
}
What is the error? I think there might be an issue with passing the values to the function.
The main issue is that the enqueue and dequeue don't take pointers as arguments, but struct stack. This means the function gets a copy of the given struct, and that the pointer you pass to push and pop (like &s1) is pointing to that local structure, not to the one in main. By consequence any update to the top member of that stack will not be seen by the caller.
I would suggest to:
Consistently pass pointers to struct typed arguments. This was well done for the push and pop functions, and there is no reason why it should not be done the same way for enqueue and dequeue functions.
Define a struct queue so that you abstract a bit that there are two stacks involved and don't have to pass both of them as argument to dequeue.
Create separate functions for:
creating a new stack
displaying a stack
creating a new queue
displaying a queue
checking if a queue is empty
Here is how your code would then look:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int top;
int size;
int *s;
};
struct stack* newstack(int size) {
struct stack *s = malloc(sizeof(struct stack));
s->size = size;
s->s = malloc(size*sizeof(int));
s->top = -1;
return s;
}
int isfull(struct stack *st) {
return st->top == st->size - 1;
}
int isempty(struct stack *st) {
return st->top == -1;
}
void push(struct stack *st, int x) {
if (isfull(st)){
printf("Full!\n");
} else {
st->top++;
st->s[st->top] = x;
}
}
int pop(struct stack *st) {
int x = -1;
if (isempty(st)){
printf("Empty!\n");
} else {
x = st->s[st->top];
st->top--;
}
return x;
}
void displaystack(struct stack *st) {
for(int i = 0; i <= st->top; i++) {
printf("%d ", st->s[i]);
}
}
struct queue {
struct stack *s1;
struct stack *s2;
};
struct queue* newqueue(int size) {
struct queue *q = malloc(sizeof(struct queue));
q->s1 = newstack(size);
q->s2 = newstack(size);
return q;
}
int isemptyqueue(struct queue *q) {
return isempty(q->s1) && isempty(q->s2);
}
void enqueue(struct queue *q, int x) {
push(q->s1, x);
}
int dequeue(struct queue *q) {
int x = -1;
if (isemptyqueue(q)) {
printf("Queue is empty!\n");
return -1;
}
if (isempty(q->s2)) {
while (!isempty(q->s1)) {
push(q->s2, pop(q->s1));
}
}
return pop(q->s2);
}
void displayqueue(struct queue *q) {
displaystack(q->s1);
printf("| ");
displaystack(q->s2);
printf("\n");
}
int main() {
int n, choice, x, m;
printf("Enter the size of the queue: ");
scanf("%d", &n);
struct queue *q = newqueue(n);
while (choice != 4) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &x);
enqueue(q, x);
break;
case 2:
m = dequeue(q);
printf("The deleted element is: %d\n", m);
break;
case 3:
displayqueue(q);
break;
}
}
return 0;
}
I have created a function which uses Linked List to check whether an expression is balanced or not. A balanced expression has no. of opening brackets equal to no. of closing brackets.
But the function Bracket Balancing always gives "unbalanced" as the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct LL {
char data;
struct LL *next;
};
int isEmpty(struct LL *top) {
if (top == NULL) {
return 1;
}
else {
return 0;
}
}
int isFull(struct LL *top) {
struct LL *n = malloc(sizeof(struct LL *));
if (n == NULL) {
return 1;
}
else {
return 0;
}
}
struct LL *push(struct LL *top, char x) {
if (isFull(top)) {
printf("Stack Overflow\n");
}
else {
struct LL *n = malloc(sizeof(struct LL));
n->data = x;
n->next = top;
top = n;
}
return top;
}
struct LL *pop(struct LL *top) {
if (isEmpty(top)) {
printf("Stack Underflow\n");
}
else {
struct LL *n = malloc(sizeof(struct LL));
n = top;
top = top->next;
free(n);
}
return top;
}
int BracketBalancing (char *exp) {
struct LL *top = malloc(sizeof(struct LL));
top->next = NULL;
for (int i = 0; exp[i] != '\0'; i++) {
if (exp[i] == '(') {
push(top, exp[i]);
}
else if (exp[i] == ')') {
if (isEmpty(top)) {
return 0;
}
pop(top);
}
}
if (isEmpty(top)) {
return 1;
}
else {
return 0;
}
}
MAIN:
int main(int argc, char const *argv[]) {
int n;
char *expression = (char *)malloc(sizeof(char));
printf("Enter the length of the expression for Bracket Balancing\n");
scanf("%d", &n);
printf("Enter the expression for Bracket Balancing\n");
for (int i = 0; i < n; i++) {
scanf("%c ", &expression[i]);
}
getchar();
if (BracketBalancing(expression)) {
printf("The expression is balanced\n");
}
else if (!BracketBalancing(expression)) {
printf("This expression is unbalanced\n");
}
return 0;
}
Example:
Input:
Enter the length of the expression for Bracket Balancing
4
Enter the expression for Bracket Balancing
1+()
Output:
This expression is unbalanced
In the above example, Despite the expression being balanced the output generated is "This expression is unbalanced".
Please correct my code.
This is how you initialize your list:
struct LL *top = malloc(sizeof(struct LL));
top->next = NULL;
And this is isEmpty():
int isEmpty(struct LL *top)
{
if (top == NULL)
{
return 1;
}
else
{
return 0;
}
}
But: top starts with a value != NULL, so isEmtpy() will not return 1, although our list should be empty in the beginning.
Your implementation of push() should work fine when you pass NULL, so you can just initialize struct LL *top = NULL; instead of creating the first element rightaway.
there other bugs in your code, e.g.:
in pop() you do
struct LL *n = malloc(sizeof(struct LL));
n = top;
thus, the result of malloc() is directly overwritten() in the next line
in isFull() you produce a memory leak as you call malloc() and never use or free() the buffer returned. That function doesn't make sense anyway, just check the result of malloc()s where your really want to use the buffer returned.
** Edit **
What I haven't seen before, you also never use the return value of push() and pop() so the new top determined by these function is lost. Replace push(top, ...); by top = push(top,...); and pop(top); by top = pop(top);
This is a menu-driven program that carries out basic stack operations using arrays in the C programming language. The functions that are performed are push, pop, peep,isempty and isfull.
#include<stdio.h>
#include<stdlib.h>
struct stack
{
long int top;
long int size;
char* key;
};
int is_empty(struct stack *s) //check if its empty
{
if(s->top==-1)
{
return -1;
}
else
{
return 1;
}
}
int is_full(struct stack *s) //check if its full
{
if (s->top ==s->size-1)
{
return -1;
}
else
{
return 1;
}
}
void push(struct stack *s, char x) //pushes into stack
{
int check;
check = is_full(s);
if(check==-1)
{
printf("-1\n");
}
else
{
s->top = s->top+1;
s->key[s->top]=x;
}
}
void pop(struct stack *s) //deletes the last element
{
int check;
check = is_empty(s);
if(check==-1)
{
printf("-1\n");
}
else
{
char k;
k = s->key[s->top];
printf("%c\n",k);
s->top--;
}
}
void peep(struct stack *s) //prints the last element without deleting
{ int check;
char k;
check = is_empty(s);
if (check == -1)
{
printf("-1\n");
}
else
{
k = s->key[s->top];
printf("%c \n",k);
}
}
int main()
{
char ch;
char x;
long int n;
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
s->top = -1; //setting as -1 base case
s->key= (char *)malloc(n*sizeof(char)); //dynamic allocation of keys
while(1)
{
scanf("%c ",&ch);
switch(ch)
{
case 'i':
scanf("%c ",&x);
push(s,x);
break;
case 'd':pop(s);
break;
case 'p':peep(s);
break;
case 't':exit(0); //termination case
}
}
return 0;
}
This is a C program that is working for me in some online compilers but in VScode and other compilers, it's showing a segmentation fault without any output. This is an implementation of stack using arrays. Is it a problem with any of the scanf functions?
You have created a pointer variable s and then access the size field on that struct.
struct stack *s;
scanf("%ld ", &n);
s->size = n; //initialise the size
Except s doesn't actually point to anything at this point. You need to either statically or dynamically allocate memory for that struct.
struct stack s;
Or:
struct stack *s = malloc(sizeof(struct stack));
My code is basically functions used for making/using a stack. I've tried almost everything, but I don't know why my program is displaying this error:
Error: Syntax error before 'struct'
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 128
struct stack_struct {
ElemType items[CAPACITY];
int top;
};
StackPtr stk_create(){
StackPtr s = malloc(sizeof(struct stack_struct));
s->top = -1; // stack initially empty
return s;
}
// TODO
StackPtr stk_clone(StackPtr s) {
return NULL; // temporary placeholder
}
void stk_free(StackPtr s) {
free(s);
}
int stk_push(StackPtr s, ElemType val){
if(s->top == CAPACITY - 1)
struct stack_struct * temp;
temp = (struct stack_struct*)malloc(sizeof(struct stack_struct));
s->top++;
s->items[s->top] = val;
return 1;
}
ElemType stk_pop(StackPtr s){
if(s->top == -1)
abort(); // library function which terminates program!!!
s->top--;
return s->items[s->top+1];
}
int stk_is_full(StackPtr s){
return s->top == CAPACITY-1;
}
int stk_is_empty(StackPtr s){
return s->top == -1;
}
int stk_size(StackPtr s) {
return s->top+1;
}
void stk_clear(StackPtr s){
s->top = -1;
}
void stk_print(StackPtr s) {
int i;
printf("\n----TOP-----\n");
for(i=s->top; i>=0; i--) {
printf(FORMAT_STRING, s->items[i]);
}
printf("---BOTTOM---\n");
}
int main() {
StackPtr sptr;
sptr = stk_create();
stk_push(sptr, 1.7);
stk_push(sptr, 3.14);
stk_print(sptr);
stk_pop(sptr);
stk_print(sptr);
stk_free(sptr);
}
As I could see, function stack_push should look like this
int stk_push(StackPtr s, ElemType val){
if(stk_is_full(s))
return -1; // stack already full, we couldn't push new elem
s->top++;
s->items[s->top] = val;
return 1;
}
I think error in this line (line 35 in your source code):
struct stack_struct * temp;
. Let's try
typedef struct stack_struct * temp;
or change declare struct
struct stack_struct {
ElemType items[CAPACITY];
int top;} stack;
and then call
stack* temp;
in line 35.
I wrote this code and compiled it,
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Data Structures
typedef struct process{
char jobName;
int arrivalTime;
int execTime;
struct process *next;
} P;
typedef struct result{
char Name;
struct result *next;
} result;
// End of Data Structures
int quantum;
int jobCounts=0;
int ResponseTime=0;
double timeEsp=0;
// Function Prototypes
int gettimeofday(struct timeval *tp, void * tzp);
void add_to_process_list(P **List,P *data);
void add_to_result_list(result **List,char name);
P *readData(char* fileName);
void writeData(result *res,char *out);
P *removeFromList(P **head,P **El);
int jobCount(P *head);
double get_time(void);
// End of Function Prototypes
int main(int argc,char *argv[]){
P *pList=NULL,*tmpNode=NULL;
result *RR_result=NULL,*JR=NULL;
double start, stop;
int counter=0;
printf("Reading Data ... ");
pList=readData(argv[1]);
printf("Complete\n");
jobCounts=jobCount(pList);
start = get_time();
printf("Algorithm Started ...\n");
if(pList!=NULL){
tmpNode=pList;
while(tmpNode!=NULL){
if(tmpNode->arrivalTime<=counter){
JR=(result *)malloc(sizeof(result *));
printf("Giving Quantum to %c\n",tmpNode->jobName);
Sleep(quantum*1000); //simulate process time
add_to_result_list(&RR_result,tmpNode->jobName);
tmpNode->execTime-=quantum;
if(tmpNode->execTime==0){
ResponseTime+=((get_time()-start)-tmpNode->arrivalTime);
tmpNode=removeFromList(&pList,&tmpNode);
}else
tmpNode=tmpNode->next;
counter++;
}else
tmpNode=tmpNode->next;
}
printf("Algorithm Finished.\n");
stop= get_time();
timeEsp=(stop-start);
printf("Writing data ... ");
writeData(RR_result,argv[2]);
printf("Completed.\n");
return 0;
}else{
return 1;
}
}
void add_to_process_list(P **List,P *data){
P *new=malloc(sizeof(P));
new->arrivalTime=data->arrivalTime;
new->execTime=data->execTime;
new->jobName=data->jobName;
if((*List)==NULL){
new->next=new;
*List=new;
}else{
P *tmp;
tmp=*List;
while(tmp->next!=(*List)) tmp=tmp->next;
tmp->next=new;
new->next=*List;
}
}
void add_to_result_list(result **List,char name){
result *new=malloc(sizeof(result));
new->Name=name;
new->next=NULL;
if(*List==NULL){
*List=new;
}else{
result *tmp;
tmp=*List;
while(tmp->next!=NULL) tmp=tmp->next;
tmp->next=new;
}
}
P *readData(char* fileName){
P *head=NULL,*cur=NULL;
char Name[20],tmp[255];
int AT,ET;
FILE *iF;
if((iF=fopen(fileName,"r"))>0){
fgets(tmp,255,iF);
sscanf(tmp,"Interval:%d\n",&quantum);
fgets(tmp,255,iF); //waste
while(!feof(iF) && fgets(tmp,255,iF)){
sscanf(tmp,"%20c %20d %20d",Name,&AT,&ET);
cur=(P *)malloc(sizeof(P *));
cur->jobName=Name[0];
cur->arrivalTime=AT;
cur->execTime=ET;
add_to_process_list(&head,cur);
}
fclose(iF);
return head;
}else{
printf("Fatal error:\n\tError openning input file.\n");
return NULL;
}
}
void writeData(result * res ,char *out){
result *tmp=res;
FILE *oF;
int tmpCounter=1;
double throughput=jobCounts/timeEsp;
double RR=ResponseTime/jobCounts;
if((oF=fopen(out,"w"))>0){
fprintf(oF,"%-15s %-15s\n","Job Name","Execution Time");
while(tmp!=NULL){
if(tmp->next!=NULL && tmp->Name==tmp->next->Name)
tmpCounter++;
else{
fprintf(oF,"%-15c %-15d\n",tmp->Name,tmpCounter*quantum);
tmpCounter=1;
}
tmp=tmp->next;
}
fprintf(oF,"Response Time: %0.2f\n",RR);
fprintf(oF,"Throghput: %0.2f\n",throughput);
fclose(oF);
}else{
printf("Fatal error:\n\tError openning output file.\n");
}
}
P *removeFromList(P **head,P **El){
P *tmp=*head;
if((*El)->next==*El){
free(*El);
return NULL;
}else{
while(tmp->next!=NULL && tmp->next!=*El) tmp=tmp->next;
tmp->next=tmp->next->next;
free(*El);
return tmp->next;
}
}
int jobCount(P *head){
P *tmp=head;
int count=1;
if(tmp->next==tmp){
return 1;
}
while(tmp->next!=head) {
tmp=tmp->next;
count++;
}
return count;
}
double get_time(void){
struct timeval stime;
gettimeofday (&stime, (struct timezone*)0);
return (stime.tv_sec+((double)stime.tv_usec)/1000000);
}
it worked correctly but when I chenged it to the code bellow (removing program arguments and use static file names) I can't run program and to pauses on reading data ! (the strange thing is when I run second code with argument (I mean just pass argument in execution) it works!!
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Data Structures
typedef struct process{
char jobName;
int arrivalTime;
int execTime;
struct process *next;
} P;
typedef struct result{
char Name;
struct result *next;
} result;
// End of Data Structures
int quantum;
int jobCounts=0;
int ResponseTime=0;
double timeEsp=0;
// Function Prototypes
int gettimeofday(struct timeval *tp, void * tzp);
void add_to_process_list(P **List,P *data);
void add_to_result_list(result **List,char name);
P *readData(char* fileName);
void writeData(result *res,char *out);
P *removeFromList(P **head,P **El);
int jobCount(P *head);
double get_time(void);
// End of Function Prototypes
int main(){
P *pList=NULL,*tmpNode=NULL;
result *RR_result=NULL,*JR=NULL;
double start, stop;
int counter=0;
char *in="input.txt";
char *out="output.txt";
printf("Reading Data ... ");
pList=readData(in);
printf("Complete\n");
jobCounts=jobCount(pList);
start = get_time();
printf("Algorithm Started ...\n");
if(pList!=NULL){
tmpNode=pList;
while(tmpNode!=NULL){
if(tmpNode->arrivalTime<=counter){
JR=(result *)malloc(sizeof(result *));
printf("Giving Quantum to %c\n",tmpNode->jobName);
Sleep(quantum*1000); //simulate process time
add_to_result_list(&RR_result,tmpNode->jobName);
tmpNode->execTime-=quantum;
if(tmpNode->execTime==0){
ResponseTime+=((get_time()-start)-tmpNode->arrivalTime);
tmpNode=removeFromList(&pList,&tmpNode);
}else
tmpNode=tmpNode->next;
counter++;
}else
tmpNode=tmpNode->next;
}
printf("Algorithm Finished.\n");
stop= get_time();
timeEsp=(stop-start);
printf("Writing data ... ");
writeData(RR_result,out);
printf("Completed.\n");
return 0;
}else{
return 1;
}
}
void add_to_process_list(P **List,P *data){
P *new=malloc(sizeof(P));
new->arrivalTime=data->arrivalTime;
new->execTime=data->execTime;
new->jobName=data->jobName;
if((*List)==NULL){
new->next=new;
*List=new;
}else{
P *tmp;
tmp=*List;
while(tmp->next!=(*List)) tmp=tmp->next;
tmp->next=new;
new->next=*List;
}
}
void add_to_result_list(result **List,char name){
result *new=malloc(sizeof(result));
new->Name=name;
new->next=NULL;
if(*List==NULL){
*List=new;
}else{
result *tmp;
tmp=*List;
while(tmp->next!=NULL) tmp=tmp->next;
tmp->next=new;
}
}
P *readData(char* fileName){
P *head=NULL,*cur=NULL;
char Name[20],tmp[255];
int AT,ET;
FILE *iF;
if((iF=fopen(fileName,"r"))>0){
fgets(tmp,255,iF);
sscanf(tmp,"Interval:%d\n",&quantum);
fgets(tmp,255,iF); //waste
while(!feof(iF) && fgets(tmp,255,iF)){
sscanf(tmp,"%20c %20d %20d",Name,&AT,&ET);
cur=(P *)malloc(sizeof(P *));
cur->jobName=Name[0];
cur->arrivalTime=AT;
cur->execTime=ET;
add_to_process_list(&head,cur);
}
fclose(iF);
return head;
}else{
printf("Fatal error:\n\tError openning input file.\n");
return NULL;
}
}
void writeData(result * res ,char *out){
result *tmp=res;
FILE *oF;
int tmpCounter=1;
double throughput=jobCounts/timeEsp;
double RR=ResponseTime/jobCounts;
if((oF=fopen(out,"w"))>0){
fprintf(oF,"%-15s %-15s\n","Job Name","Execution Time");
while(tmp!=NULL){
if(tmp->next!=NULL && tmp->Name==tmp->next->Name)
tmpCounter++;
else{
fprintf(oF,"%-15c %-15d\n",tmp->Name,tmpCounter*quantum);
tmpCounter=1;
}
tmp=tmp->next;
}
fprintf(oF,"Response Time: %0.2f\n",RR);
fprintf(oF,"Throghput: %0.2f\n",throughput);
fclose(oF);
}else{
printf("Fatal error:\n\tError openning output file.\n");
}
}
P *removeFromList(P **head,P **El){
P *tmp=*head;
if((*El)->next==*El){
free(*El);
return NULL;
}else{
while(tmp->next!=NULL && tmp->next!=*El) tmp=tmp->next;
tmp->next=tmp->next->next;
free(*El);
return tmp->next;
}
}
int jobCount(P *head){
P *tmp=head;
int count=1;
if(tmp->next==tmp){
return 1;
}
while(tmp->next!=head) {
tmp=tmp->next;
count++;
}
return count;
}
double get_time(void){
struct timeval stime;
gettimeofday (&stime, (struct timezone*)0);
return (stime.tv_sec+((double)stime.tv_usec)/1000000);
}
What's the problem? is it buffer overflow?!
Thanks
There appears to be at least one overflow. The following allocation is only allocating 4 bytes (in a 32-bit system):
cur=(P *)malloc(sizeof(P *));
It should probably be:
cur=(P *)malloc(sizeof(P));