-1
\$\begingroup\$

I'm trying to implement a stack data structure, but not on the heap but using the stack. Also not using any Node or Element struct but raw ints. Is this feasible? This is my code (with debug printfs) and the output is below. It doesn't seem to work atm. My general idea was:

  • Get some stack address (see create_stack()).
  • Make both top and bottom point at that address initially.
  • For push, increment the top pointer and assign a value to the memory.
  • For pop, read from top and decrement the top pointer.

.

typedef struct stack {
    int *top;
    int *bottom;
}Stack;


Stack create_stack() {
    Stack stack;
    int bottom = 5;    //This could be any value. I just need an address!
    stack.top = stack.bottom = ⊥
    printf("Created a new stack with stack.top: %p value = %d, stack.bottom: %p value = %d\n", stack.top, *stack.top, stack.bottom, *stack.bottom);
    return stack;
}


int pop(Stack *stack) {
    int result = NULL;
    printf("Popping...\nTop currently points at address %p which contains %d\n", stack->top, *stack->top);

    if (stack->bottom == stack->top) {
        perror("Stack is empty!");
    } else {
        stack->top -= sizeof(int);
    }

    return result;
}


void push(Stack *stack, int value) {
    printf("Pushing...\nTop currently points at address %p which contains %d\n", stack->top, *stack->top);
    stack->top += sizeof(int);
    *stack->top = value;
    printf("And now address %p contains %d\n", stack->top, *stack->top);
}


int main() {
    Stack stack = create_stack();
    Stack *stack_pointer = &stack;
    push(stack_pointer, 49);
    pop(stack_pointer);
    return 0;
}

This is the output:

Created a new stack with stack.top: 0x7ffc8dfcbabc value = 5, stack.bottom: 0x7ffc8dfcbabc value = 5
Pushing...
Top currently points at address 0x7ffc8dfcbabc which contains 5
And now address 0x7ffc8dfcbacc contains 49
Popping...
Top currently points at address 0x7ffc8dfcbacc which contains 32764
\$\endgroup\$

closed as off-topic by chux, Jamal Oct 17 '16 at 18:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – chux, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ So, does this code work as intended? If not, it is not a good fit for this site. \$\endgroup\$ – xDaevax Oct 17 '16 at 18:11
1
\$\begingroup\$

Basic compiler warnings:

Printing with "%p" expects a void *. Passing char * is undefined behavior, although commonly not a problem,

// printf("Popping... %p which contains %d\n", stack->top, *stack->top);
printf("Popping... %p which contains %d\n", (void *) stack->top, *stack->top);

"initialization makes integer from pointer without a cast".

// int result = NULL;
int result = (int) NULL;

Given the 2 above warnings implies OP is not compiling with warnings fully enabled. Best to do so before posting.

Function

Invalid code. The following attempts to save value, yet the memory space for it is not yet available. IOWs, OP is posting code that certainly is not functional. Sigh.

stack->top += sizeof(int);
*stack->top = value;  // UB

Wrong pointer math.

// stack->top -= sizeof(int);
(stack->top)--; 

Rather than initialize a pointer with a value the will shortly become invalid and non-testable, set to NULL (or "yuck", a static version of bottom) and adjust the printf() to not de-reference the pointers.

 // int bottom = 5;    //This could be any value. I just need an address!
 // stack.top = stack.bottom = ⊥
 stack.top = stack.bottom = NULL;

int pop(Stack *stack) only ever returns (int) NULL. I'd expect stack->top or *(stack->top) to be returned.

Style

Narrow-up excessively wide lines

printf("Created a new stack with stack.top: %p value = %d, stack.bottom: %p value = %d\n", stack.top, *stack.top, stack.bottom, *stack.bottom);

vs.

printf("Created a new stack with stack.top: %p value = %d, stack.bottom: %p value = %d\n",
    stack.top, *stack.top, stack.bottom, *stack.bottom);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.