Multi-threading/Structs in C assignment

  • Thread starter frankfjf
  • Start date
  • Tags
    Assignment
I'll go back and fix that now. In summary, the code is failing to properly reference the created ringbuf_t structure, resulting in a different value being outputted from the main function.f
  • #1
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.

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.
 
  • #2
Your rb_init function returns an uninitialized pointer and you allocate your array on the stack instead of on the heap. You absolutely need to read up on and understand how to do dynamic memory allocation and pointer management in C.
 
  • #3
Thanks for your help.

Oh wow, I had overlooked forgetting to fix the pointer return. Thank you for pointing that out especially!
 

Suggested for: Multi-threading/Structs in C assignment

Replies
6
Views
834
Replies
7
Views
1K
Replies
6
Views
2K
Replies
2
Views
525
Replies
28
Views
677
Replies
3
Views
502
Replies
15
Views
780
Back
Top