Thread: Programme Execution Doesnt follow C standard

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Programme Execution Doesnt follow C standard

    In my code there is a code fragment which checks to see if a certain character is contained in a character stack data structure:

    Code:
    int containsChar(struct CharStack stack, char token){
    if(!stack.stackPointer){
             fprintf(stderr, "Character Stack is empty, doesn't contain anything \n");
            exit(-1);
        }
        for(int i = 0; i <stack.stackPointer; i++){
            if(stack.stackValues[i] == token){
                return i;
            }
        }
        return -1;
    }
    If the stack doesnt contain anything I exit() the programme execution.

    What is absolutely baffling to me is that when I try this snippet on an empty stack, I expect the programme to exit with the output "Character Stack is empty, doesn't contain anything".

    Instead what it does is print "Number Stack empty" prior to exiting immediately thereafter. The only other place in my code base where this wording is contained is in pop number function:

    Code:
    double popNum(struct NumStack *stack){
    if (stack -> stackPointer > 0){
            return stack -> stackValues[--stack -> stackPointer];
        }
        else {
            fprintf(stderr, "Number Stack empty\n");
            exit(-1);
        }
    }
    When I debug this programme line by line, the last statement prior to exiting is in the containsChar function and not the popNum function. Infact the latter function is never even stepped into. Its as if the compiler is doing whatever it wants which leads me to suspect that there is undefined behaviour somewhere in here.

    Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Short, Self Contained, Correct Example
    Post something we can analyse, not your guesswork.

    > int containsChar(struct CharStack stack, char token)
    Why are you passing a copy of the stack, instead of a pointer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Salem View Post
    Short, Self Contained, Correct Example
    Post something we can analyse, not your guesswork.

    > int containsChar(struct CharStack stack, char token)
    Why are you passing a copy of the stack, instead of a pointer?
    The problem is when I make it SSCE, the problem doesnt arise so I have no clue where in the programme the problem arises.

    Im passing a copy because I do not intend to modify the stack - I only want to the function to return where in the stack the token/character is, or if not, to return -1.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Run it in the debugger.

    Put two breakpoints on the print lines in question.

    Run the code, get a stack trace, find out where it got called from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Follow up to 32/64 issue
    By jcfuller in forum Windows Programming
    Replies: 8
    Last Post: 10-11-2014, 07:12 AM
  2. why doesnt the C++ standard library committee offer any AUI related libararie?
    By Masterx in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-13-2009, 01:48 PM
  3. which path to follow??
    By wakish in forum C++ Programming
    Replies: 7
    Last Post: 10-17-2005, 02:08 PM
  4. follow up for anonytmouse
    By Thantos in forum Windows Programming
    Replies: 0
    Last Post: 09-17-2003, 09:08 PM
  5. follow up to if and then statements
    By mikezmr2 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 05:35 PM

Tags for this Thread