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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:15:05+00:00 2026-05-31T18:15:05+00:00

I’m currently writing a stack implementation in C. I need it to use as

  • 0

I’m currently writing a stack implementation in C. I need it to use as little memory as possible and be as fast as possible while still taking every data type I can throw at it, with multiples types in each stack. I decided to use void pointers for this, and got it working relatively quickly despite my rusty C. However, actually using it is quite ugly.

For testing the stack, I’m pushing integers with a loop. Actually passing the int as a void pointer is the issue.

My first inclination was to use &:

for (int i = 0; i < 20; i++){
    stack_push(s, &i); //s is the stack_t pointer
}

However with closer inspection this obviously wouldn’t work, since i is destructively updated every step. Every element in the stack ends up as 20.

I’ve since turned to a very ugly solution:

for (int i = 0; i < 20; i++){
    int* p = malloc(sizeof(void*));
    *p = i;
    stack_push(s, p);
}

This works, but presents two issues:

  1. It’s ugly

  2. I have to worry about memory management for every element of the stack. (and for some reason manually walking the stack and freeing every element still leaks memory…)

Is there a better way to do this without wasting unnecessary memory with a union and still being fast? Thanks.

  • 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-31T18:15:06+00:00Added an answer on May 31, 2026 at 6:15 pm

    If the types are going to be mixed, something needs to know what the type is.

    If it is the client (or user) of the stack who will cast types back to their correct form, then that’s fine, but the size still needs to be available to the stack.

    If the caller knows that too, the interface might look like:

    void push(const int size, void* value);
    void pop(const int size, void** valueptr);

    I don’t think that will be robust in general, but if you are writing a compiler then it can generate all of the correct code, in the same was as the C compiler.

    Internaly, I’d have two versions, one for debugging, which retains the value of size, and one for ‘performance’ which doesn’t. I;d wite the debugging one first.

    I’d ditch the idea of pointers because

    1. it uses extra space
    2. the space that is allocated needs to be managed by something.

    If the stack stores values, then once popped it has no responsibilty.

    An implementation:

    unsigned char space[BIG];
    unsigned char *stack = &space[0];
    int top = 0;
    void push(const int size, void* value) {
        unsigned char* valp = (char *)value;
        *(int *)stack = size;
        stack += sizeof(int);
        for (int i=0; i<size; ++i) {
           *stack++ = *val++;
        }
    }
    

    pop is the reverse,

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT

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.