Thread: Passing a memory location back to main

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    9

    Passing a memory location back to main

    I am working on an assignment in which I can not modify main. I am trying to create a ringbuffer module so that the main program can call multiple ringbuffers at a time (it is actually multi-threaded, but that doesn't matter yet.) I am tring to create a ringbuf_alloc function that initializes the ringbuf-t struct and passes back the pointer to the ringbuf_t struct. However I am getting stuck on passing the pointer. I can't make it a local variable, because it changes each time I call it, and I can't make it a static variable because it changes each time a new buffer calls it. Appreciate any help or any ideas:
    Code:
    Code:
    Ringbuf_t Typedef:
    typedef struct ringbuf {
        int           size;	/* # of valid elements in the buffer */
        int           head;		/* head pointer */
        int           tail;		/* tail poiner */
        int	   capacity;
        int	  *buf;
    } ringbuf_t;
    
    Main:
    ringbuf_t  *ringbuf1 = NULL;
    ringbuf_t  *ringbuf2 = NULL;
    n = 10;
        if((ringbuf1 = ringbuf_alloc(n)) == NULL)
            syserr("test 1 ringbuf_alloc 2");
        n = 20;
        if((ringbuf2 = ringbuf_alloc(n)) == NULL)
            syserr("test 1 ringbuf_alloc 3");
        if(ringbuf_get_capacity(ringbuf1) != 10 || 
    	    ringbuf_get_capacity(ringbuf2) != 20)
            printf("logic error  7\n");
    
    Ringbuf_alloc function
    static ringbuf_t p, rb;
    
    ringbuf_t 
    *ringbuf_alloc(int capacity)
    {
      if (pthread_mutex_init(&mtx, NULL))
    	  syserr("pthread_mutex_init");
      p.capacity = capacity;
      p.size = 0;
      p.head = 0;
      p.tail = 0;
      p.buf=(int *) malloc(sizeof(int)*capacity);
      return &p;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    ringbuf_t *p = malloc( sizeof *p );
    ...
    return p;

    Then you can call it as many times as you like, and each call gets a new ring buffer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    9
    This gives me an error: Invalid conversion from 'void *' to 'ringbuf_t *'

    When I add that to the code:
    [
    Code:
    ringbuf_t 
    *ringbuf_alloc(int capacity)
    {
      if (pthread_mutex_init(&mtx, NULL))
    	  syserr("pthread_mutex_init");
      ringbuf_t *p = malloc( sizeof *p );
      p->capacity = capacity;
      p->size = 0;
      p->head = 0;
      p->tail = 0;
      p->buf=(int *) malloc(sizeof(int)*capacity);
      return p;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That means you're compiling your 'C' code with a C++ compiler.

    Choose which language you want to use, and rename your files accordingly, or figure out what options you need to set to make your compiler behave as you want.

    That probably explains why you're doing this casting here
    p->buf=(int *) malloc(sizeof(int)*capacity);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    9
    Thanks for your help. I was using g++ as my prof had suggested it for catching when I forget to include necessary libraries.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be using gcc not g++. g++ is the C++ compiler. gcc is the C compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused as to how to pass back to main
    By J-Camz in forum C Programming
    Replies: 6
    Last Post: 11-28-2008, 07:21 AM
  2. Replies: 7
    Last Post: 10-15-2008, 03:38 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM
  5. Going back to a main menu from anywhere
    By Gades in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 04:22 PM