- #1
frankfjf
- 168
- 0
Hi all,
Although my homework assignment centers around practicing multi-threading type stuff (by implementing a ring buffer), my question is more centered on trying to figure something out that has nothing to do with multi-threading but nevertheless has me stumped on the assignment itself.
Okay, so, here's my header file's code for the ring buffer.
And here's my first source file, which is intended to implement the above functions.
Alright, so, as you can see, I've only given a shot at implementing the first method, which is just supposed to initialize the ring buffer and return a pointer to the new structure. Below is the contents of my main source file, which is supposed to tie it all together.
Obviously, it's just barely interacting with the previous stuff. My problem is that I'm not getting the right values when testing out referencing individual members of the created ringbuf_t structure. The expected result of the main function is to spit back 10, the test size for the new ring buffer. But the main function isn't outputting 10, but instead outputs 108681. Why is this happening, and how do I fix my code to have it return the correct amount? Note that when I put a printf statement in the rb_init method itself to directly reference bufsiz, it gets the correct number, but not when referenced via the created pointer.
Thank you in advance.
Although my homework assignment centers around practicing multi-threading type stuff (by implementing a ring buffer), my question is more centered on trying to figure something out that has nothing to do with multi-threading but nevertheless has me stumped on the assignment itself.
Okay, so, here's my header file's code for the ring buffer.
Code:
#include <pthread.h>
struct ringbuf_t
{
pthread_mutex_t mutex;
pthread_cond_t cond_full;
pthread_cond_t cond_empty;
int bufsiz;
int front;
int back;
char* buf;
};
extern struct ringbuf_t* rb_init (int bufsiz);
extern void rb_finalize (struct ringbuf_t* rb);
extern int rb_size (struct ringbuf_t* rb);
extern int rb_is_full (struct ringbuf_t* rb);
extern int rb_is_empty (struct ringbuf_t* rb);
extern void rb_insert (struct ringbuf_t* rb, int c);
extern int rb_remove (struct ringbuf_t* rb);
And here's my first source file, which is intended to implement the above functions.
Code:
#include "ringbuf.h"
#include <stdio.h>
#include <pthread.h>
struct ringbuf_t* rb_init(int bufsiz)
{
char temp[bufsiz];
struct ringbuf_t newrb = { PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
PTHREAD_COND_INITIALIZER,
bufsiz, 0, bufsiz - 1, temp };
struct ringbuf_t *p;
return p;
}
Alright, so, as you can see, I've only given a shot at implementing the first method, which is just supposed to initialize the ring buffer and return a pointer to the new structure. Below is the contents of my main source file, which is supposed to tie it all together.
Code:
#include <stdio.h>
#include "ringbuf.h"
int main()
{
printf("\nTesting first method...");
struct ringbuf_t *p = rb_init(10);
printf("\n%i", p->bufsiz);
return 0;
}
Obviously, it's just barely interacting with the previous stuff. My problem is that I'm not getting the right values when testing out referencing individual members of the created ringbuf_t structure. The expected result of the main function is to spit back 10, the test size for the new ring buffer. But the main function isn't outputting 10, but instead outputs 108681. Why is this happening, and how do I fix my code to have it return the correct amount? Note that when I put a printf statement in the rb_init method itself to directly reference bufsiz, it gets the correct number, but not when referenced via the created pointer.
Thank you in advance.