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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:11:01+00:00 2026-05-30T00:11:01+00:00

I created an implementation of a Stack in C. Here are the relevant definitions/methods,

  • 0

I created an implementation of a Stack in C. Here are the relevant definitions/methods, I have stripped all the error checking/reallocation so don’t comment on that:

typedef struct {
    Element *data;
    int top;
    int size;
} Stack;

typedef void* Element;

void push(Stack *s, Element e) {
    s->data[(s->top)++] = e;
}

Now in another method, I have a loop in which I call push(). Something like

int character;
Stack *s = createStack();
while((character = getchar()) != EOF) {
    int tmp = character;
    push(s, &tmp);  
}

However, this doesn’t function how I want it to. The stack receives the same address everytime, thus when each character is read, the “contents” of the entire stack change. How do I modify this to do what I want. e.g when abcde is read, the stack looks like (top-down) e,d,c,b,a.

  • 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-30T00:11:02+00:00Added an answer on May 30, 2026 at 12:11 am
    int tmp = character;
    push(s, &tmp);
    

    There you are passing the address of the local variable tmp to the function push which is storing the pointer in itself. Every time the loop is iterated, the variable is destroyed and another local variable is made (most likely on top of the old variable), so you are storing pointers to destroyed variables.

    If you want your stack to be generic and work with void*, you’ll have to make sure the lifetimes of the objects you store is longer than the lifetime of the stack. One way you can do this is allocate the variables on the heap:

    char* c = malloc(sizeof(char)); // or malloc(1) but sizeof is there in case you
                                    // change the type later
    *c = character;
    push(s, c);
    

    Then deallocate each one after the stack is no longer in use so you don’t get a memory leak.

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

Sidebar

Related Questions

Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created my own Tree implementation for various reasons and have come up
I have a berkeley db file (*.bdb) which is created by the C implementation(python
I've created an implementation of the QAbstractListModel class in Qt Jambi 4.4 and am
I created an UIViewController subclass, and figured out that the default implementation of -loadView
I've created a custom Fancybox implementation that displays the title on the left-hand side
Here's a overview of my workflow implementation: GUI thread starts worker thread worker thread
I have an unusual problem that caused my mind to stuck. I have created
I have started creating an app which uses a core data stack at the
I've been struggling to find a decent RTP stack implementation for Java . JMF

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.