Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8769103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:14:49+00:00 2026-06-13T17:14:49+00:00

I am getting an error which I have no idea how to fix. I

  • 0

I am getting an error which I have no idea how to fix. I am trying to work through the “producer-consumer” problem using spin-lock. I created a “Queue”-like data structure as a shared resource to put “produced” items in, and to remove items to be “consumed”. This is what my main program looks like:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Queue.h"

#define DEBUG 1

Queue_t* global_queue; // create a global queue

/* thread procedure for the producer thread */
void* producer_func(void* arg)
{
   while(1) // loops infinitely
   {
      int datum = rand() % global_queue->maximum_count;

      // spin while the queue is full
      while ((global_queue->current_count) ==
         (global_queue->maximum_count));

      enqueue(global_queue, datum);
      display(global_queue);
   }
}


/* thread procedure for the consumer thread */
void* consumer_func(void* arg)
{
   while(1) // loops infinitely
   {
      int datum = 0;

      // spin while there are no items in the queue
      while(global_queue->current_count == 0);

      datum = dequeue(global_queue);
      printf("The number consumed is %d\n");
   }
}


/* Main */
int main(int argc, char** argv)
{
   if(argc != 2)
   {
      printf("Error: wrong number of command-line arguments\n");
      printf("Usage: %s <integer>\n", argv[0]);
      exit(1);
   }

   pthread_t producer;  // create producer thread
   pthread_t consumer;  // create consumer thread

   // create the queue object, get the max queue size
   //int max_count = atoi(argv[1]);
   global_queue = construct(10);
   display(global_queue);

   // intialize the random seed generator
   srand((unsigned)time(NULL));

   // create the threads and have them execute their routines
   pthread_create(&producer, NULL, &producer_func, NULL);
   pthread_create(&consumer, NULL, &consumer_func, NULL);

   // join the threads to finish
   pthread_join(producer, NULL);
   pthread_join(consumer, NULL);

   // deallocate the queue from memory
   //destruct(global_queue);

   return 0;
}

However, when I call the pthread_create in main routine, I get a bizarre error in malloc.c:

queue_demo: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

Here is my “Queue” data structure header file:

#ifndef QUEUE_H
#define QUEUE_H

typedef struct Queue
{
   int  current_count;
   int  maximum_count;
   int  buffer[];       // queue uses an array
} Queue_t;


// routines to implement Queue-like functionality (FIFO)
// TODO: somehow encapsulate all these features in the struct itself.
//
Queue_t* construct(int buff_size);
void     destruct (Queue_t* queue);
void     display  (Queue_t* queue);
int      dequeue  (Queue_t* queue);
void     enqueue  (Queue_t* queue, const int datum);

#endif

And implementation:

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"

Queue_t* construct(int buff_size)
{
   Queue_t* queue = malloc(sizeof(Queue_t));

   assert(queue != NULL);
   queue->maximum_count = buff_size;
   queue->current_count = 0;

   int i = 0;

   for(; i < queue->maximum_count; ++i)
      queue->buffer[i] = 0;

   return queue;
}

void destruct(Queue_t* queue)
{
   assert(queue != NULL);
   free(queue);
   printf("Queue destroyed!\n");
}

void display(Queue_t* queue)
{
   int i = 0;

   for(; i < queue->maximum_count; ++i)
      printf("%d ", queue->buffer[i]);
   printf("\n");
}

void enqueue(Queue_t* queue, const int datum)
{
   assert(queue->current_count < queue->maximum_count);
   queue->buffer[queue->current_count] = datum;
   ++queue->current_count;
}


int dequeue(Queue_t* queue)
{
   int i = 1;
   int datum = queue->buffer[0];

   assert(queue->current_count > 0);

   for(; i < queue->maximum_count; ++i)
   {
      queue->buffer[i-1] = queue->buffer[i];
      queue->buffer[i] = 0;
   }

   --queue->current_count;

   return datum;
}

Clearly, I am doing something very wrong. But I have no idea what exactly that is. I suspect it may have something to do with declaring the struct globally, but I am not sure. Any ideas would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:14:50+00:00Added an answer on June 13, 2026 at 5:14 pm

    Your construct() is not allocating space for the queue item data.

    Queue_t* construct(int buff_size)
    {
       Queue_t* queue = malloc(sizeof(Queue_t));
    
       assert(queue != NULL);
       queue->maximum_count = buff_size;
       queue->current_count = 0;
    
       int i = 0;
    
       // HERE. Where is the item data space allocation ??
       for(; i < queue->maximum_count; ++i)
          queue->buffer[i] = 0;
    
       return queue;
    }
    

    Try this instead:

    Queue_t* construct(int buff_size)
    {
       // note space allocation for buffer[] bytes as well as overall structure.
       Queue_t* queue = malloc(sizeof(Queue_t) + sizeof(int) * buff_size);
    
       assert(queue != NULL);
       queue->maximum_count = buff_size;
       queue->current_count = 0;
       memset(queue->buffer, 0, sizeof(int)*buff_size);
       return queue;
    }
    

    I’ve not reviewed the rest of your code to determine if you’re properly protecting concurrent access to the queue (that pthread mention makes me suspect you should be adding, and initializing, a pthread_mutex_t to the members of your Queue_t type, and using it for guarding concurrent queue modification). Regardless, the above is most-definitely a problem and should be taken care of first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fix my code but I have no idea why I'm getting
I am trying to create full text index but I am getting error which
I keep getting this error from my code and I have no idea what
I have made my own exception class which derives from runtime_error and is getting
I am getting this error when calling a web service method which writes to
I am getting the error below, which makes no sense. * Terminating app due
I am getting the following error, which I don't understand... 06-15 22:06:49.196: W/dalvikvm(17622): threadid=1:
I am getting the following error while running my MVC application, which uses the
Good Day Everyone... I’m getting an unexpected WCF error complaining of Known Types which
I am getting following error. Restore failed for Server I have recently upgraded SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.