0

for some reason my code works fin on my system running ubuntu 16.04 but when I run it on my schools computer (also running Ubuntu 16.04) I get a segmentation fault. The line when I run debugger claims it is coming from the print loop at the end which makes no sense as a int should not trigger a segmentation fault.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int RETURN = 0; //return value to change if error found

// check if entered value is a number
bool isNumericChar(char x)
{
    return (x >= '0' && x <= '9')? true: false;
}

// Recreated atoi to check for non ints
int myAtoi(char *str)
{
  if (*str == '\0'){
    return 0;
  }

  int res = 0; 
  int sign = 1;
  int i = 0;  

  // If number is negative, then update sign
  if (str[0] == '-'){
    sign = -1;
    i++;  
  }

  // Iterate through all digits of input string and update result
  for (; str[i] != '\0'; ++i){
    if (isNumericChar(str[i]) == false){
      RETURN = 1;
      fprintf(stderr, "must be integer\n");
      break;
      return 0;
    }
    res = res*10 + str[i] - '0';
  }

    // Return result with sign
  return sign*res;
}

// this is main struct of a node of the linked list
struct LL {
  int number;
  struct LL *next;
};

// this function allocates memory for a node and returns the
// allcoated node item
struct LL *makeNode(int num) {
  struct LL *node = (struct LL *) malloc( sizeof(struct LL) );
  node->number = num;
  return node;
}

int main() {
  char input[10];   
  struct LL * stack_head; 
  bool wasPush = false; 
  int num = 0; 
  while(EOF != scanf("%s", input )) {
    if( 0 == strcmp(input, "push") ) {   // if the word is push, then next item may be a number
      wasPush = true;
    } 
    else if( 0 == strcmp( input, "pop" ) ) { 
      wasPush = false; 
      if( stack_head != NULL ) {
        stack_head = stack_head->next;   
      }
    } 
    else{
      // probably a number
      if( !wasPush ){
        continue;   // and last word was a push
      }
      int number = myAtoi(input);   //convert this to a number

      if( stack_head == NULL ) {   //an empty linked list then this is the first node
        stack_head = makeNode(number);
        stack_head->next = NULL;
      }
      else{   // list and can have a next item in it
        struct LL * prev_head = stack_head;
        stack_head = makeNode(number);
        stack_head->next = prev_head;
      }
      wasPush = false;
    }
  }

  // we print the items on the stack now by iterating
  // from the top to the bottom of the stack
  while( stack_head != NULL ) {
    num = stack_head->number;
    printf("%d\n", num );
    stack_head = stack_head->next;
  }
  return RETURN;
}
12
  • 5
    makeNode should set node->next = NULL also initialize stack_head to NULL in your main. Commented Jul 10, 2018 at 20:34
  • 2
    Your use of scanf is quite unsafe. For a short discussion see this thread for example: stackoverflow.com/questions/1621394/… Commented Jul 10, 2018 at 20:39
  • 1
    C has isdigit so you don't need to write isNumericChar. You already have the necessary header that defines it. Commented Jul 10, 2018 at 21:04
  • Thanks all! just needed to initialize it in main. As far as scanf I know it is unsafe, unfortunately it was part of the requirement for this assignment. Commented Jul 11, 2018 at 4:14
  • regarding: break; return 0; in function: myAtoi(), the 'return' statement will never be executed because the 'break' exits the 'for()' loop so instead the statement: return sign*res; will be executed Commented Jul 11, 2018 at 14:47

1 Answer 1

4

Try initializing stack_head to NULL. Local variables are not guaranteed to be initialized, so your stack_head == NULL check may fail.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I knew it was something silly like that
Not that it is relevant to the original question, but the way you iterate through the stack at the end leaks memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.