Hey everyone,
I was assigned a project where we have to simulate virtual memory page replacement algorithms. I decided to use a Queue ADT I made in a previous project, and modify it to enqueue page reference strings of a process, character by character. However, when I initially create the Queue, I get a Bus Error, which is weird, because I haven't changed the create() function in my queue. I was wondering if you could take a look. I'll include a couple of the necessary headers, main.c, and the parts of queue.c i modified as well as the create() function, which I did not modify.

Thanks
Here is main.c

#include "process.h"
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
    process *p;
    p->pid = 1;
    p->reference_string = "000111222";
    
    Queue q = create();
    int i = 0;
//Debugging the bus error, was found two lines up
    //while(p->reference_string[i] != '\0'){
      //  enqueue(q, p, i);
      //  printf("%c\n", p->reference_string[i]);
     //   i++;
   // }
    //printf("%s\n", p->reference_string);
    //print_queue(q);
    return 0;
}

Here is queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
#include <stdio.h>
#include "process.h"

typedef struct linked_list *Queue;


Queue create(void);
void destroy(Queue q);
bool is_empty(Queue q);
bool is_full(Queue q);
int get_size(Queue q);
void enqueue(Queue q, process *p, int index);
char dequeue(Queue q);
void print_queue(Queue q);
#endif

here is process.h

#ifndef PROCESS_H
#define PROCESS_H

typedef struct{
    int pid;
    struct page_table *pt;
	char * reference_string;
}process;

#endif

and the parts of queue.c :

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include "process.h"
#include "queue.h"

struct node{
    char element;
    struct node *next;
};

struct linked_list{
    struct node *head;
    struct node *tail;
    int size;
};

struct page_table{
    int pages[256];
    bool paged[256];
};

struct page_frame_table{
    int pages[256];
    bool used [256];
};

bool free_frame_table[256];

Queue create(void)
{
    Queue q = malloc(sizeof(struct linked_list));
    if(q == NULL)
        terminate("Error in malloc: could not allocate space for a Queue");
    q->head = NULL;
    q->tail = NULL;
    q->size = 0;
}

/*
 *enqueue: adds an element to the back of the queue
 */
void enqueue(Queue q, process *p, int index)
{
    struct node *new_node = malloc(sizeof(struct node));
    if(new_node == NULL)
        terminate("Error in enqueue: couldn't allocate memory for a node");
    new_node->element = p->reference_string[index];
    if(is_empty(q)){
        q->head = new_node;
        q->tail = new_node;
        q->size ++;
        new_node->next = NULL;
    }
    else{
        q->tail->next = new_node;
        q->tail = q->tail->next;
        q->tail->next = NULL;
        q->size++;
    }
}

/*
 * dequeue: removes an element from the head of the queue
 */
char dequeue(Queue q)
{
    if(is_empty(q)){
        printf("Queue is empty\n");
        return -1;
    }
    struct node *n = q->head;
    if(get_size(q) == 2){
        q->head = q->head->next;
        q->head->next = NULL;
        q->tail = q->head;
        q->size--;
    }
    if(get_size(q) == 1){
        q->head = NULL;
        q->tail = NULL;
        q->size--;
    }
    else{
        q->head = q->head->next;
        q->size--;
    }
    char p = n->element;
    return p;
}

Recommended Answers

All 5 Replies

The problem is obvious. Actually it's too obvious, which implies you don't know C at all and trying to fool us by stating you've originally written this code.
Here you go:

int main()
{
    process *p;
    p->pid = 1;
    p->reference_string = "000111222";

That tells you don't know about pointers in C.

Queue q = create();
...
Queue create(void)
{
    Queue q = malloc(sizeof(struct linked_list));
    if(q == NULL)
        terminate("Error in malloc: could not allocate space for a Queue");
    q->head = NULL;
    q->tail = NULL;
    q->size = 0;
}

That tells you don't know about functions, return values, automatic variables, scopes... You're unaware that you're corrupting memory (also creating garbage).
Good luck on your future copy-paste programming career.

commented: Don't be a dick. -1

actually, i did write this code. i'm not as proficient in C as I am in other languages, but i'm learning. i'm sorry if you woke up on the wrong side of the bed this morning or something, but theres no need to be a giant dick and flame me because you don't "believe" that i wrote the code. go fuck yourself.

You might not like the previous posting but its accurate...Your using a pointer that's pointing to nothing..

int main()
{
    process *p;/*this points to nothing*/
    p->pid = 1;
    p->reference_string = "000111222";

You might not like the previous posting but its accurate

The thing I took issue with was the fact that he immediately assumed I was running some giant con where I copied the code but claimed I wrote it to save face. Such an accusation says more about him than it does me.

As for the section in my code where i declare a pointer, and try and manipulate the data that it points to without first allocating memory for it, for that I feel dumb that I missed such an obvious mistake. As I said before, I'm still learning C and am shaky with it, even with some of those mundane things that everyone should know.

I was under the impression that this was a community where everyone could learn from one another, without fear of being belittled or accused of plagiarism.

The one important thing I learned about public forums is...You need thick skin. It just a post, get past it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.