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 8211837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:33:51+00:00 2026-06-07T10:33:51+00:00

I am implementing a stack that does not require memory allocation and can take

  • 0

I am implementing a stack that does not require memory allocation and can take any struct as long as it embeds a special struct. Similar to GNU’s List implementation.

The struct that any struct must embed to work in the stack is:

struct stack_elem {
  struct stack_elem *next;
};

The struct that I would like to be used with the stack is:

struct node {
  double number;
  char character;
  int isNumber;
  struct stack_elem elem;
};

So the stack would look like this:

node:            node:
+---------+      +---------+
|number   |      |number   |
+---------+      +---------+
|character|      |character|
+---------+      +---------+
|isNumber |      |isNumber |
+---------+      +---------+
|elem     |      |elem     |
| *next   |----->| *next   |     
+---------+      +---------+ etc....

I am writing a macro called stack_entry to convert the embedded elem struct to its container node struct. Here’s what I’ve tried so far. stack_entry will be used as follows:

struct stack_elem *top = peek(&stack);
struct node *converted_node = stack_entry(top, struct node, elem);

stack_entry will take a pointer to the stack_elem, the type of the node to be converted to and the name of the member field of the element in that node. I tried several ways to do it but none work. I realized that STACK_ELEM points to the next item and so I tried just subtracting the offset of elem from struct node and that did not work. Here are a few things I tried that did not work either:

#define stack_entry(STACK_ELEM, STRUCT, MEMBER)       \
         ((STRUCT *) &(STACK_ELEM) - offsetof (STRUCT, MEMBER))

#define stack_entry(STACK_ELEM, STRUCT, MEMBER)       \
         ((STRUCT *) ((uint8_t *) &(STACK_ELEM)->next \
         - offsetof (STRUCT, MEMBER)))

What would be the correct arithmetic? Why isn’t subtracting the offset of next, hence the offset of elem, yielding node if its the last element in the struct?

GNU List list_entry macro is defined as:

#define list_entry(LIST_ELEM, STRUCT, MEMBER)           \
        ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next     \
                     - offsetof (STRUCT, MEMBER.next)))

How does this work? Why is next involved in here when its supposed to grab the container struct within the same node, and not the next one?

  • 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-07T10:33:53+00:00Added an answer on June 7, 2026 at 10:33 am

    GNU’s list_entry works by subtracting the offset from the beginning of the struct to the next field from the address of the next field. It doesn’t actually de-reference the next pointer. There is a common macro used for this in the Linux sources called container_of, which you might find helpful. The linked article is pretty informative.

    In your second attempt to define stack_entry, you subtract the wrong offset (compare it to the GNU list_entry macro).

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

Sidebar

Related Questions

I'm enamoured with the idea of implementing my own single-entry-point gateway that does two
I've heard that trampolining is an ineffective way of implementing TCO. How does DrScheme
I'm implementing a stack application, however every time I push an item onto the
For my compsci class, I am implementing a Stack template class, but have run
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
When implementing Serializable interface, in Java, we need to set it's serialVersionUID long. Is
its when i starting trying implementing this that i got quite stuck. some business
I'm implementing a stack-based VM and I've been trying to read up on literature
I've recently been implementing a recursive directory search implementation and I'm using a Stack
What is the idea behind implementing a tagging system like Stack Overflow? This is

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.