1

I am new at C programming. I wrote some code for a stack exercise. The question is: one of the result is wrong, but when I debug step by step, the number changed abruptly. Could you help me solve this?

// @name mystack.c
// Created by lilei on 2017/3/10.
//
#include "mystack.h"
#include <malloc.h>
#include <stdio.h>

Stack createStack(){
    Stack stack = (Stack)malloc(sizeof(Stack));
    stack->top = -1;
    return stack;
}

int isFull(Stack s){
    if (s->top == MAXSIZE-1){
        return 1;
    } else{
        return 0;
    }
}

int push(Stack s, ElementType item){
    if(!isFull(s)){
        s->top++;
        s->data[s->top] = item;
        return 1;
    } else{
        printf("full!");
        return 0;
    }
}


int isEmpty (Stack s){
    if(s->top == -1){
        return 1;
    } else{
    return 0;
    }
}

ElementType pop(Stack s){
if(!isEmpty(s)){
    ElementType e = s->data[s->top];
    s->top--;
    return e;
}
}

void myPrintf(Stack s){
    int len = s->top;
     printf("The data are ");
   while(len >= 0){
        printf("%d ", s->data[len]);
        len--;
    }
    printf("\n");
}

int main(){
    Stack s = createStack();
    for(int i = 0; i < 7; i++){
        push(s, i*2);
    }
    myPrintf(s);

    printf("isEmpty:%d, isFull:%d\n, pop one:%d", isEmpty(s), isFull(s), pop(s));
}

The result is

enter image description here

Jens
  • 69,818
  • 15
  • 125
  • 179
cinhori
  • 11
  • 2
  • 4
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Mar 10 '17 at 09:04
  • 3
    Post text as text, not images! – too honest for this site Mar 10 '17 at 09:08
  • 4
    `Stack` appear to be a pointer typedef, so `sizeof(Stack)` probably won't do what you think. – sp2danny Mar 10 '17 at 09:10
  • There is no header file called malloc.h. Always yse `stdlib.h` instead. – Lundin Mar 10 '17 at 09:11
  • 2
    Anyway, you need to include the definition of `Stack` for anyone to be able to answer the question. Looks like the typical "I typedeffed a pointer and now my program is buggy as hell" case. – Lundin Mar 10 '17 at 09:12
  • 5
    And *that* is why we [don't generally hide pointers in `typedef`s](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). – WhozCraig Mar 10 '17 at 09:13
  • Why has this question got three close votes? What is wrong with it (aparet from the image showing the results)? – JeremyP Mar 10 '17 at 09:22
  • 1
    Please show `mystack.h`. – Jabberwocky Mar 10 '17 at 09:26
  • @JeremyP : It's not a [MCVE](http://stackoverflow.com/help/mcve) – sp2danny Mar 10 '17 at 09:29
  • @sp2danny So what? Wouldn't it be more constructive to help him improve the question rather than just vote to close it. All it needs is the definition of `Stack`. – JeremyP Mar 10 '17 at 09:37
  • Thank you all. I have got the answer. It is the `malloc()`.Tks again – cinhori Mar 10 '17 at 09:37
  • 1
    And now it has been closed as duplicate of "do I cast the result of malloc". How stupid are some people here. It certainly isn't a duplicate of that question. – JeremyP Mar 10 '17 at 09:46

1 Answers1

2

I can't see the declaration of Stack because you forgot to put it in th question, but it must be declared as a pointer to a struct e.g.

typedef struct
{
    int top;
    ElementType data[MAXSIZE];
}  *Stack;
// ^- means pointer

So a Stack is a pointer type which means that, when you malloc it, you malloc only 4 or 8 bytes depending on whether you are compiling for 32 or 64 bit. The malloc line should be

Stack stack = malloc(sizeof *stack);

You were not allocating enough space on your stack for the actual stack structure which means that you are writing into bits of the heap you do not own and other things will write into the memory you think you have allocated for your stack.

There is another problem. What does your pop() function return if you try to pop something when the stack is empty? The answer is "could be anything". You either need to abort the program or return some error value.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Thank you very much! You have solved my question clearly.Thank you ! – cinhori Mar 10 '17 at 09:44
  • 1
    @cinhori Sorry that some people have been complete idiots about closing your question. I hope it doesn't put you off using SO. – JeremyP Mar 10 '17 at 09:47