Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Multi-threading/Structs in C assignment

  1. Nov 5, 2011 #1
    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 (Text):
    #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 (Text):
    #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,
                     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 (Text):
    #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. jcsd
  3. Nov 5, 2011 #2

    Filip Larsen

    User Avatar
    Gold Member

    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.
  4. Nov 5, 2011 #3
    Thanks for your help.

    Oh wow, I had overlooked forgetting to fix the pointer return. Thank you for pointing that out especially!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook