Generic stack don't push or pop value

user3868594

I'm trying to implement generic stack in C, but I can't print value that I pushed. Here is the code

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <memory.h>

typedef struct{
    void *elems;
    int sizeOfElems;
    int allocated;
    int lenght;
}stack;

void allocate_stack(stack *s, int sizeOfElements){
    assert(sizeOfElements > 0);
    s->sizeOfElems = sizeOfElements;
    s->allocated = 2;
    s->lenght = 2;
    s->elems = malloc(sizeOfElements * s->allocated);
}

void deallocate_stack(stack *s){
    free(s->elems);
}

void push_elem(stack *s, void *elem){
    s->lenght += 1;
    if(s->allocated == s->lenght){
        realloc(s, s->lenght * s->sizeOfElems);
        s->lenght *= 2;
    }
     void *target = (char *)s->elems + s->lenght * s->sizeOfElems;
     memcpy(target, elem, s->sizeOfElems);
}
void pop_elem(stack *s, void *elemAddr){
    void *source = (char *)s->elems + (s->lenght-1) * s->sizeOfElems;
    memcpy(elemAddr, source, s->sizeOfElems);
    s->lenght -=1;
}
int main(){
    stack s;
    allocate_stack(&s, 1);
    char a = 'a';
    push_elem(&s, &a);
    char *elem = NULL;
    pop_elem(&s, elem);
    printf("%s", elem);
    deallocate_stack(&s);
    return 0;
}

The problem is that I don't get anything when I run the program, just press return to close the window. I'm programming on Linux using GCC toolkit.

shikamaru

In your push_elem:

    s->lenght += 1;
    if(s->allocated == s->lenght){
        realloc(s, s->lenght * s->sizeOfElems);
        s->lenght *= 2;
    }

It seems like you are trying to re-allocate space if you run out, but your default value of 2 for allocated is going to stay as it is, you should probably do something like:

    if (s->allocated < s->lenght) {
        realloc(s->elems, ....)
        //Increase both allocated and length to represent the current state of the stack
    }

Notice that in the realloc() call you are passing s, which is in the stack, this won't do what you want: http://pubs.opengroup.org/onlinepubs/7908799/xsh/realloc.html As per your structure you should be passing s->elems

Also, before calling pop_elem, you are assigning NULL to elem. Inside your pop_elem, this is where you are trying to do a memcpy() to. To do this you will have to allocate memory:http://pubs.opengroup.org/onlinepubs/009695399/functions/memcpy.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stack Push and Pop

From Dev

stack, push and pop in python

From Dev

Push and pop in stack

From Dev

Generic stack push error

From Dev

stack pop and push performaing on empty

From Dev

When assemby stack push and pop

From Dev

postfix notation - Stack pop and push help, incorrect return value being returned for mod or pow operator

From Dev

How to push and pop from a stack in Assembly mips?

From Dev

Why don't some of my push storyboard segues show an option for peek and pop?

From Dev

Generic push to a Stack gives compilation error

From Dev

Div don't stack vertically

From Dev

Bootstrap divs don't stack

From Dev

Pop consecutive items on a stack if value is the same

From Dev

Observable Stack<T> is not working on push

From Dev

iOS doing a pushViewController and don't pop

From Dev

How to define a LIST that can push and pop like a stack?

From Dev

How to define a LIST that can push and pop like a stack?

From Dev

How do I push and pop the matrix stack in LibGDX

From Dev

javascript interactive stack which can push and pop using browser window

From Dev

how can i pop two elements from a stack and then push in stack them back as sum of values?

From Dev

GCC inline - push address, not its value to stack

From Dev

push(int[][]) in a stack is duplicating the same value Java

From Dev

GCC inline - push address, not its value to stack

From Dev

Don't understand the stack without the setTimeout 0

From Dev

Don't receive push notifications with GCM on iOS

From Dev

Github push event signature don't match

From Dev

Can I POP a value from the stack, but put it nowhere in NASM Assembly?

From Dev

Why can't a perform pop() inside an add() for a stack

From Dev

Pop-up ' Don't show this message again ' option

Related Related

  1. 1

    Stack Push and Pop

  2. 2

    stack, push and pop in python

  3. 3

    Push and pop in stack

  4. 4

    Generic stack push error

  5. 5

    stack pop and push performaing on empty

  6. 6

    When assemby stack push and pop

  7. 7

    postfix notation - Stack pop and push help, incorrect return value being returned for mod or pow operator

  8. 8

    How to push and pop from a stack in Assembly mips?

  9. 9

    Why don't some of my push storyboard segues show an option for peek and pop?

  10. 10

    Generic push to a Stack gives compilation error

  11. 11

    Div don't stack vertically

  12. 12

    Bootstrap divs don't stack

  13. 13

    Pop consecutive items on a stack if value is the same

  14. 14

    Observable Stack<T> is not working on push

  15. 15

    iOS doing a pushViewController and don't pop

  16. 16

    How to define a LIST that can push and pop like a stack?

  17. 17

    How to define a LIST that can push and pop like a stack?

  18. 18

    How do I push and pop the matrix stack in LibGDX

  19. 19

    javascript interactive stack which can push and pop using browser window

  20. 20

    how can i pop two elements from a stack and then push in stack them back as sum of values?

  21. 21

    GCC inline - push address, not its value to stack

  22. 22

    push(int[][]) in a stack is duplicating the same value Java

  23. 23

    GCC inline - push address, not its value to stack

  24. 24

    Don't understand the stack without the setTimeout 0

  25. 25

    Don't receive push notifications with GCM on iOS

  26. 26

    Github push event signature don't match

  27. 27

    Can I POP a value from the stack, but put it nowhere in NASM Assembly?

  28. 28

    Why can't a perform pop() inside an add() for a stack

  29. 29

    Pop-up ' Don't show this message again ' option

HotTag

Archive