- #1
- 168
- 0
Hello all,
I am experiencing some odd output behavior from a program that I am working on:
Header file
First source file
Main source file
I have been using the main method to test stuff out, and currently am experiencing some odd behavior, which I think may have to do with how I am allocating memory or using pointers, I am not sure. Basically, the first time I reference anything within the created struct (in this test I referenced bufsiz), it reports the correct value the first time, and then gives junk every time afterward. Same thing for function calls or a combination of both, as if the data is only available initially and then is immediately lost as soon as it gets accessed once.
Is there something off in my code that is causing this?
Thank you in advance.
I am experiencing some odd output behavior from a program that I am working on:
Header file
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);
First source file
Code:
#include "ringbuf.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct ringbuf_t* rb_init(int bufsiz)
{
int x;
char *temp = malloc(sizeof(char) * bufsiz);
memset(temp, '\0', sizeof(temp));
//temp[bufsiz-2] = 'w';
struct ringbuf_t newrb = { PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
PTHREAD_COND_INITIALIZER,
bufsiz, 0, bufsiz - 1, temp };
struct ringbuf_t* p = malloc(sizeof(struct ringbuf_t));
p = &newrb;
return p;
}
void rb_finalize(struct ringbuf_t* rb)
{
free(rb);
}
int rb_size(struct ringbuf_t* rb)
{
int x;
for (x = 0; rb->buf[x] != '\0'; x++)
;
return x;
}
int rb_is_full(struct ringbuf_t* rb)
{
if (rb->buf[rb->back] != '\0')
return 1;
return 0;
}
int rb_is_empty(struct ringbuf_t* rb)
{
if (rb->buf[rb->front] != '\0')
return 0;
return 1;
}
Main source file
Code:
#include <stdio.h>
#include <stdlib.h>
#include "ringbuf.h"
int main()
{
printf("\nTesting first method...");
struct ringbuf_t *p = malloc(sizeof(struct ringbuf_t));
p = rb_init(10);
printf("\n%d", p->bufsiz);
printf("\n%d", p->bufsiz);
return 0;
}
I have been using the main method to test stuff out, and currently am experiencing some odd behavior, which I think may have to do with how I am allocating memory or using pointers, I am not sure. Basically, the first time I reference anything within the created struct (in this test I referenced bufsiz), it reports the correct value the first time, and then gives junk every time afterward. Same thing for function calls or a combination of both, as if the data is only available initially and then is immediately lost as soon as it gets accessed once.
Is there something off in my code that is causing this?
Thank you in advance.