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

The Archive Base Latest Questions

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

I’m trying to implement a simple stack. It works fine on MacOSX but crashes

  • 0

I’m trying to implement a simple stack. It works fine on MacOSX but crashes on linux(Ubuntu). Can someone help me to know why?

MYStack.h

#ifndef MYSTACK_H_
#define MYSTACK_H_

typedef struct Element *element;
typedef struct Stack *stack;

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

struct Stack {
    struct Element *head;
    unsigned int count;
    void (*dump) (void *);
};

/* Define boolean type */
typedef signed char     bool;
#define YES             (bool)1
#define NO              (bool)0

/* utility macro */
#define ELEMENT_NEXT(E)     ((E) = (E)->next)
#define ELEMENT_DATA(E)     ((E)->data)
#define STACK_HEAD(S)       ((S)->head)
#define STACK_SIZE(S)       ((S)->count)

/* Functions prototypes */
bool push( stack , void * );
bool pop( stack , void ** );
bool create_stack( stack , void (*) (void*) );
bool delete_stack( stack  );
void dump_stack( stack );

#endif /* MYSTACK_H_ */

MYStack.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "MYStack.h"

/* Creating the stack */
bool create_stack( stack new_stack, void (*dump_function) (void*))
{
    new_stack->head = NULL;
    new_stack->dump = dump_function;
    new_stack->count = 0;

    return YES;
}

/* Deleting the stack */
bool delete_stack( stack this_stack )
{
    element next_element;

    while (this_stack->head) {
        next_element = this_stack->head->next;
        free(this_stack->head->next);
        this_stack->head = next_element;
    }

    return YES;
}

/* Dump the stack */
void dump_stack(stack this_stack)
{
    element e;
    int i;

    e = STACK_HEAD(this_stack);

    if (this_stack->dump ) {    
        for (i = 0; i < this_stack->count; i++) {
            (this_stack->dump) (e->data);
            if (e->next != NULL)
                e = e->next;
        }
    }
}

/* Adding element to the stack */
bool push( stack this_stack, void *data )
{
    element new_element;
    void (*temp_dump) (void *);

    this_stack->dump = temp_dump;

    new_element = (element ) malloc(sizeof(element *));

    if (new_element == NULL) {
        fprintf(stdout, "malloc() new element failed : %d\n", errno);

        return NO;
    }

    new_element->data = data;
    new_element->next = (this_stack)->head;
    (this_stack)->head = new_element;
    this_stack->dump = temp_dump;
    (this_stack)->count++;

#ifdef DEBUG
    fprintf(stdout, "Inserting at the stack in the address %p\n", new_element->data);
#endif

    return YES;
}

/* Remving element from the stack */
bool pop( stack this_stack, void **data )
{
    element delete_me;
    void (*temp_dump) (void *);

    this_stack->dump = temp_dump;
    delete_me = this_stack->head;
    if (delete_me == NULL) {
        fprintf(stdout, "stack is empty\n");
        return NO;
    }

    *data = delete_me->data;
    this_stack->head = delete_me->next;
    this_stack->dump = temp_dump;
    this_stack->count--;

    free(delete_me);

#ifdef DEBUG
    fprintf(stdout, "Removing from the stack in the address %p\n", delete_me->data);
#endif

    return YES;
}

/* Dump function test */
void dump_ints(void* data)
{
    int* int_data_ptr = (int*)data;
    printf("We are dumping an int data : %d\n", *int_data_ptr);
}

/* To test our stack */
int main (int argc, char const **argv)
{
    stack new_stack;
    void (*dump_func_ptr) (void*);
    int i;
    int stack_ints[] = {1, 2, 3, 4};
    void *deleted_item;

#ifdef DEBUG
    fprintf(stdout, "We are in debug mode...\n");
#endif

    new_stack = (stack) malloc(sizeof(stack *));
    dump_func_ptr = dump_ints;  
    create_stack( new_stack, dump_func_ptr);
    /* Insert to the stack */
    for (i = 0; i < 4; i++)
        push( new_stack, &stack_ints[i]);

    /* Print the number of elements */
    printf("Our stack contain %d elements\n", new_stack->count);
    /* Dump the stack */
    dump_stack(new_stack);
    /* Removing some data */
    pop(new_stack, &deleted_item);
    /* Dump the stack */
    dump_stack(new_stack);
    /* Deleting the stack */
    //delete_stack(new_stack);

    free(new_stack);

    return 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-25T00:23:11+00:00Added an answer on May 25, 2026 at 12:23 am

    There are a couple of incorrect allocations. They are only allocating memory the size of a pointer (4 bytes on a 32-bit system) and subsequent initialization of those pieces of memory will overwrite past the end of the allocated memory. The ones I noticed are these:

    new_element = (element ) malloc(sizeof(element *));
    

    and

    new_stack = (stack) malloc(sizeof(stack *));
    

    They should be something like this:

    new_element = (element ) malloc(sizeof(struct Element));
    new_stack = (stack) malloc(sizeof(struct Stack));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.