Originally Posted by
Salem
Or even just
int buffer;
dequeue(&circularQueue, &buffer);
> why this doesn't seem to work in C/C++
Not thinking about C and C++ as being interchangeable would be a good start.
C is one language, C++ is another language, C/C++ is a mashup train-wreck waiting to happen.
If you're coming from Java, you should really have begun with
class CircularQueue
and made all your functions members of the class.
First language I learnt was C, and after that it was C++.
I use Java most on my current job.
What I am trying to implement is SerialCommunication class for sending/receiving data via UART with a microcontroller using interrupts.
When I make sure everything works I will write a class for it.
Here is a new code, since I got it working I changed BUFFER_SIZE to power of 2 and using AND operation instead of modulus operation.
Added some new functions.
Code:
// Circular Buffer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#define BUFFER_SIZE 8
#define BUFFER_MASK (BUFFER_SIZE - 1)
struct CircularQueue
{
int first;
int last;
int validItems;
int data[BUFFER_SIZE];
};
void initiliazeQueue(CircularQueue* queue)
{
queue->validItems = 0;
queue->first = 0;
queue->last = 0;
for (int i = 0; i < BUFFER_SIZE; i++)
{
queue->data[i] = NULL;
}
}
int isEmpty(CircularQueue* queue)
{
if (queue->validItems == 0)
return 1;
else
return 0;
}
int getFreeElemNum(CircularQueue* queue)
{
return ((BUFFER_SIZE - queue->first + queue->last) & BUFFER_MASK);
}
bool availabile(CircularQueue* queue)
{
if (((BUFFER_SIZE - queue->first + queue->last) & BUFFER_MASK))
{
return true;
}
return false;
}
int getOccupiedElemNum(CircularQueue* queue)
{
return (BUFFER_SIZE - ((BUFFER_SIZE - queue->first + queue->last) & BUFFER_MASK));
}
int enqueue(CircularQueue* queue, int itemValue)
{
if (queue->validItems >= BUFFER_SIZE)
{
std::cout << "The queue is full\n";
std::cout << "You cannot add items\n";
return -1;
}
else
{
queue->validItems++;
queue->data[queue->last] = itemValue;
queue->last = (queue->last + 1) & BUFFER_MASK;
return 0;
}
}
int dequeue(CircularQueue* queue, int* itemValue)
{
if (isEmpty(queue))
{
printf("is empty\n");
return 1;
}
else
{
*itemValue = queue->data[queue->first];
queue->first = (queue->first + 1) & BUFFER_MASK;
queue->validItems--;
return 0;
}
}
void printQueue(CircularQueue* queue)
{
int aux, aux1;
aux = queue->first;
aux1 = queue->validItems;
while (aux1>0)
{
printf("Element #%d = %d\n", aux, queue->data[aux]);
aux = (aux + 1) & BUFFER_MASK;
aux1--;
}
return;
}
/*
void receive(CircularQueue* queue, int buffer[BUFFER_SIZE])
{
int* tempElement = (int*)malloc(sizeof(int));
int tempBuffer[BUFFER_SIZE];
int i = 0;
do {
if (dequeue(queue, tempElement))
break;
tempBuffer[i++] = *tempElement;
} while (true);
memcpy(buffer, tempBuffer, sizeof(tempBuffer));
}
*/
int* receive(CircularQueue* queue)
{
int* tempElement = (int*)malloc(sizeof(int));
const int TEMP_BUFFER_SIZE = getOccupiedElemNum(queue);
int* tempBuffer = (int*)malloc(TEMP_BUFFER_SIZE * sizeof(int));
int i = 0;
do {
if (dequeue(queue, tempElement))
break;
tempBuffer[i++] = *tempElement;
} while (true);
delete(tempElement);
return tempBuffer;
}
void print(int* buffer)
{
for (int i = 0; *buffer != NULL; ++i)
printf("Buffer[%d]: %d\n", i, *buffer++);
}
int main()
{
CircularQueue* circularQueue = new CircularQueue;
int* buffer = (int*)malloc(sizeof(int) * BUFFER_SIZE);
//int buffer[BUFFER_SIZE];
initiliazeQueue(circularQueue);
if (isEmpty(circularQueue))
std::cout << "Queue is empty" << endl;
enqueue(circularQueue, 1);
enqueue(circularQueue, 2);
enqueue(circularQueue, 3);
enqueue(circularQueue, 4);
enqueue(circularQueue, 5);
enqueue(circularQueue, 6);
enqueue(circularQueue, 7);
enqueue(circularQueue, 8);
enqueue(circularQueue, 9);
printQueue(circularQueue);
if (isEmpty(circularQueue) == -1)
std::cout << "Queue is empty" << endl;
dequeue(circularQueue, buffer);
std::cout << "After dequeue" << endl;
printQueue(circularQueue);
//receive(circularQueue, buffer);
buffer = receive(circularQueue);
std::cout << "After calling print function" << endl;
print(buffer);
delete(circularQueue);
return 0;
}
Code compiled without errors but console crashes, so I guess I messed something up while using pointers.
in the function receive
Code:
int* receive(CircularQueue* queue)
{
int* tempElement = (int*)malloc(sizeof(int));
const int TEMP_BUFFER_SIZE = getOccupiedElemNum(queue);
int* tempBuffer = (int*)malloc(TEMP_BUFFER_SIZE * sizeof(int));
int i = 0;
do {
if (dequeue(queue, tempElement))
break;
tempBuffer[i++] = *tempElement;
} while (true);
delete(tempElement);
return tempBuffer;
}
I tried to calculate number of occupied elements in queue with
Code:
const int TEMP_BUFFER_SIZE = getOccupiedElemNum(queue);
So I dont return unwanted values from a function.
So for example if i have in my queue[8] = {1 ,2 ,3 ,4 ,5 ,6, 7, 0}
I want to print only from 1 - 7 I don't want to print 0.
And this function wont take care of it
Code:
void print(int* buffer)
{
for (int i = 0; *buffer != NULL; ++i)
printf("Buffer[%d]: %d\n", i, *buffer++);
}
but I don't know any better solution of this so I need some help.
I wouldn't bother you guys with it but since it's a weekend I can't ask any of colleuges who are experts in C/C++.