Program performs an illegal operation

This is a discussion on Program performs an illegal operation within the C Programming forums, part of the General Programming Boards category; Hi all. I am having a problem creating a stack for defined structures. I've created stacks in C before, but, ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    20

    Program performs an illegal operation

    Hi all. I am having a problem creating a stack for defined structures. I've created stacks in C before, but, until now, used them only for predefined data types like char. I want a stack for binary tree nodes, some of which possibly link to each other (i.e., the son of a node might be in the stack as well).

    First off, some structures, and initializing functions

    Code:
    typedef char BTData;
    typedef struct BinaryTreeNode BTNode;
    typedef struct LStack stack;
    typedef struct StackNode stacknode;
    typedef BTNode stackelement;
    
    /*Binary Tree Structure*/
    struct BinaryTreeNode{
    	BTNode *LeftSon;
    	BTData data;
    	BTNode *RightSon;
    };
    
    void InitBTNode(BTNode *bt, BTData dat){
    	bt->data = dat;
    	bt->LeftSon = NULL;
    	bt->RightSon = NULL;
    }
    
    /*Linked Stack Implementation*/
    struct StackNode{
    	stackelement *data;
    	stacknode *link;
    };
    
    struct LStack{
    	stacknode *top;
    };
    
    void InitStack(stack *s){
    	s->top = NULL;
    }
    Now, painfully inserting printf's in between function lines, I have determined that my push is the one doing the illegal operation (pardon me if I haven't removed the printf's yet):

    Code:
    void push(stack *s, stackelement *element){
    	stacknode *newnode;
    	printf("Declared new stacknode\n");
    	newnode = (stacknode *) malloc(sizeof(stacknode));
    	printf("malloced stacknode\n");
    	
    	if(newnode = NULL){
    		printf("newnode is null\n");
    		StackOverflow();
    		printf("Called StackOverflow\n");
    	}
    	else{
    	printf("newnode is not null\n");
    		newnode->data = element; //###THE PROBLEMATIC LINE###, as figured out from printf's
    		printf("newnode->data = element\n");
    		newnode->link = s->top;
    		printf("newnode->link = s->top\n");
    		s->top = newnode;
    		printf("s->top = newnode\n");
    	}
    }
    This push was called from another function noted here (again, pardon the debugging printf's):

    Code:
    void parse(char data[], char tag[], BTNode *finishedBT){
    	stack NodeStack;
    	printf("Declared Stack\n");
    	InitStack(&NodeStack);
    	printf("Initialized Stack\n");
    	int datalen = strlen(data);
    	printf("Computed for datalen Stack\n");
    	int taglen = strlen(tag);
    	printf("Computed for taglen\n");
    	
    	if(datalen == taglen && isValid(tag)){
    	printf("Entered if clause\n");
    		int i = 0;
    		printf("Set i to 0\n");
    		
    		while(i < taglen){
    		printf("In loop!\n");
    			BTNode thisNode;
    			printf("Declared a BTNode\n");
    			InitBTNode(&thisNode, data[i]);
    			printf("Initialized the BTNode\n");
    			
    			if(tag[i] == '0'){
    			printf("tag[i] is zero\n");
    				push(&NodeStack, &thisNode); //###PUSH WAS CALLED HERE###
    				printf("Pushed it to stack\n");
    			}
    			else{
    			printf("tag[i] is not zero\n");
    				BTNode RNode;
    				printf("Declared RNode\n");
    				pop(&NodeStack, &RNode);
    				printf("Popped something to RNode\n");
    				
    				BTNode LNode;
    				printf("Declared LNode\n");
    				pop(&NodeStack, &LNode);
    				printf("Popped something to\n");
    				
    				pointLeftTo(&thisNode, &LNode);
    				printf("Pointed the left\n");
    				pointRightTo(&thisNode, &RNode);
    				printf("Pointed the right\n");
    				
    				push(&NodeStack, &thisNode);
    				printf("Pushed created node to stack\n");
    			}
    			
    			i++;
    			printf("Incremented i\n");
    		}
    		
    		pop(&NodeStack, finishedBT);
    		printf("Popped node\n");
    	}
    	else{
    	printf("Entered else clause\n");
    		ValidTagSeq = FALSE;
    		printf("Set ValidTagSeq to FALSE\n");
    	}
    }
    Called from the main function which is

    Code:
    main(){
    	BTNode *aNode = NULL;
    	aNode = (BTNode *) malloc(sizeof(BTNode));
    	char algorithm_word[] = "ORLHMITGA";
    	char algorithm_tag[] = "001001011";
    	
    	parse(algorithm_word, algorithm_tag, aNode);
    }
    Now, I've tried variations such as making the arguments of push a stack *s and a stackelement element, the way I do for primitive data types, but they didn't seem to do any difference (I actually tried that approach first).

    Any suggestions?

    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    if(newnode = NULL){
        printf("newnode is null\n");
        StackOverflow();
        printf("Called StackOverflow\n");
    }
    else{
        printf("newnode is not null\n");
        newnode->data = element;
    = is for assignment, == is for equality testing. NULL is often defined as 0 and the result of an assignment is the value of that assignment. In the context of a true/false conditional, anything nonzero is true and anything zero is false which means that the "if" test sets newnode to NULL and then evaluates to false meaning the "else" part gets executed. Therefore you are essentially doing this:
    Code:
    newnode = NULL;
    newnode->data = element;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    20
    Oh...

    That syntax pwned me real hard

    Well, thanks hk_mp5kpdw.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Consider ramping up the warning level on your compiler. Most modern compilers will complain about that.
    Code:
    $ sed -n '40,50p' foo.c
      newnode = (stacknode *) malloc(sizeof(stacknode));
      printf("malloced stacknode\n");
      
      if(newnode = NULL){
        printf("newnode is null\n");
        StackOverflow();
        printf("Called StackOverflow\n");
      }
      else{
        printf("newnode is not null\n");
        newnode->data = element;
    $ gcc -c -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘push’:
    foo.c:43: warning: suggest brackets around assignment used as truth value
    foo.c:45: warning: implicit declaration of function ‘StackOverflow’
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. illegal operation
    By jdinger in forum Windows Programming
    Replies: 3
    Last Post: 04-07-2002, 08:14 PM
  5. Illegal operation
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21