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

  • SEARCH
  • Home
  • 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 937421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:34+00:00 2026-05-15T21:28:34+00:00

In liberal C: /* i’m using this */ struct QueueItem { QueueItem* next; void*

  • 0

In liberal C:

/* i'm using this */

struct QueueItem
    {
    QueueItem* next;
    void* data;
    }

struct Queue
    {
    QueueItem* head;
    QueueItem* tail;
    }

/*
    +---+    +---+
    |0 0|    |* *|  len 1
    +---+    +|-|+
              v v
     len     +---+
      0      |d 0|
             +---+
    
    +---+
    |* *---------...---+
    +|--+              |  len n
     v                 v
    +---+  +---+      +---+
    |a *-->|b *--...->|z 0|
    +---+  +---+      +---+
*/

This gives O(1) for all push/pop/peek and O(n) traversal, but uses 2+2n memory.
A naive array version give a minimum of 2+n (about optimal), but is usually
worse, and sometimes causes modifications to take longer (for reallocation).

struct Queue
    {
    size_t len;
    size_t head;
    void (*data)[];
    }

/*
    +-----+
    |* * *-----~~~-+
    +|-|--+        |
     v +-+         |
    +----v-----~~~-v+
    |0 0 a b c . . z|
    +----------~~~--+
*/

It looks like there is no way to improve memory usage with sacrificing preformance, but i wanted to at least put this out there in case anyone knows a way around this.

Edit (because i cant have code in comments):

struct QueueItem
{
    QueueItem* next;
    size_t len;
    void (*data)[];
    }

struct Queue
    {
    QueueItem* head;
    QueueItem* tail;
    size_t start;
    size_t end; //edit: need to know where to push data. edit: not per-item
    }

/*
    +-----+
    |* * *------------------------------+
    +|-|--+                             |
     v +---+        (2x longer)         v (much longer)
    +------v----+  +-----------+       +---------------+
    |@ 0 0 a b *-->|@ c . . h *-->...->|@ . . . x y z 0|
    +-----------+  +-----------+       +---------------+
*/
  • 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-05-15T21:28:35+00:00Added an answer on May 15, 2026 at 9:28 pm

    First, I think you neglect the cost of allocating the QueueItems. So the allocation costs between your two approaches is the same. (Someone more knowledgeable about memory allocation, please speak up if I’m wrong.)

    You can do this to alleviate the wasted space for popped/unfilled items in the array. Make a hybrid of the list and array. Let each list node contain an array of size K.

    struct QueueItem
        {
        QueueItem* next;
        void (*data)[K];
        }
    
    struct Queue
        {
        QueueItem* head;
        QueueItem* tail;
        size_t head;
        size_t end;
        }
    

    Now your performance is dictated by the size you choose for the array. The smaller it is, the less wasted space you have. The larger it is, the less your QueueItem overhead costs you as a percentage of overall space. If you knew, say, that your queue will usually be of size N, then you might choose K to be N/10. In that case, the total memory cost is N/K + 4 + N. The maximum amount of space you can waste in the arrays on unused elements is 2*K – 2.

    Usage patterns will dictate actual performance. But if you can anticipate your usage pattern, you can choose a K that works well. There might even be a way to adaptively choose K differently for each node to get even better performance, but I think that’s beyond me right now.

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

Sidebar

Related Questions

if in a literal object i try to reference a function using this inside
This is a philosophical question, actually. It's been a year I'm programming using PHP
Are these properties not considered standard CSS? I'm using something like this, which works
This is my first question in stack overflow. Please be liberal in validating it
I think that I've been used to a fairly liberal policy regarding the PHP
I'm using literal to implement css, so as to have the css class vary
I have a web application consisting of severals modules. All the modules are packaged
So, I'm developing a REST webservice using RESTeasy and Google App Engine. My question
I had this idea and my first reaction after having it was That's a
I want to look at using JSF but I'm put off by what appears

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.