Thread: weird stack output

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    Question weird stack output

    im had a similar problem as miromage did a few posts back, except my stack IS storing queues. i'm pushing null into the stack for some reason though. i didnt want to post the same problem, but after hours of trying i figured some help might be in order.
    here are my two structs:
    Code:
    struct queue
    {
        int* store;
        int count;
        int front;
        int back;
        int size;   
        
    };
    
    struct stack
    {
        struct queue* store;
        int top;
        int size; 
        
    };
    here is my queue being pushed onto the stack
    Code:
    void push_queue(struct stack* s, struct queue* q)
    {
        
        if(s->top == s->size)
        expand_stack(s);
        
        s->store[s->top] = *q ;
        s->top++;
        
            printf("\n");
    
    }
    are there any other suggestions as to why the problem is still happening?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're pushing null onto the stack, that would be because you're passing null into the function. Look at where this function is called.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i dont think the function is passing null is it?

    Code:
          //each time a new queue is added a new queue is created           
          q = new_queue(8);
            
            printf("How many numbers to enqueue?\n");
            scanf("%d", &num_enqueue);
            
            printf("Enter the numbers\n");
            
            for(i=0; i < num_enqueue; i++)
                {
                    scanf("%d", &num);
                    add_num(q, num);
    
    
                }
            
            push_queue(s, q);

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It seems unlikely at the moment, assuming you've checked all your realloc calls.

    Maybe I'll ask this: how can you tell you're pushing nulls? You're not storing pointers. Are you getting segfaults?

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i have a print function at so i can see what is being pushed. when i print s->store[s->top] i get weird figures or just simply <null>. do you think maybe it has to do with how i am initiallizing the memory? or do you think i have to add something else into the program? what's funny though is that if i push 3 queues into the stack, i'll print out 3 nulls. if i remove a queue from the stack then it will only have 2 and so on. it's pushing something, it's just something empty lol.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How are you linking the members of the queue together. For something like s->store[s->top] you need to link the queue elements together aka linked list.
    Don't see that happening. Provide more details about the problem.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would expect print s->store[s->top] to give you extremely crazy things indeed -- it's a struct! How are you supposed to print a struct?

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok here is how i am initiallizing the queue and adding a number to it:

    Code:
    struct queue* new_queue(int queue_size)
    {
        struct queue* q;
        
        q = malloc(sizeof(struct queue));
        q->store = calloc(queue_size, sizeof(int)); 
        q->front=0;
        q->back=0;
        q->size = queue_size;
        
        return q;
          
    }
    
    void add_num(struct queue* q, int num)
    {
        
        q->store[q->back]=num;
        q->back = (q->back+1) &#37; q->size;
       
       
        if(q->front == q->back)
        expand_queue(q);
        
        
    }

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    lol well how am i supposed to print the stack then? i was doing it like this:

    Code:
     int i;
        for(i=0; i < s->top; i++) 
            printf("&#37;s ", s->store[i]); 
            printf("\n");

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Have you created any objects of type struct queue or are they simply pointers to the queue data structure?
    You print out a stack by printing out each of its members.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe something like this:
    Code:
    void print_struct_queue(struct queue q) {
        int i;
        for (i = 0; i < q.size; i++) {
            printf("&#37;d ", q.store[(q.front+i) % q.size]); //or however you're indexing.
        }
        printf("\n");
    }
    which could be called as
    Code:
    print_struct_queue(s->store[i]);
    in place of your print statement.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    so you think it is stacking the right way, just not printing right? when i try and pop a queue, it erases one of the objects in the stack. i have another print statement to see what is in the queue i am going to stack. i know that is different. i want to be able to see the numbers in the stack.

    if i try to use
    Code:
    print_struct_queue(s->store[i]);
    it says incompatible type for argument 1 of print_struct_queue
    Last edited by vidioholic; 10-22-2008 at 11:47 AM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vidioholic View Post
    so you think it is stacking the right way, just not printing right? when i try and pop a queue, it erases one of the objects in the stack. i have another print statement to see what is in the queue i am going to stack. i know that is different. i want to be able to see the numbers in the stack.

    when i call print_struct_queue, i dont have an index in the main for i, so what show i be?
    Quote Originally Posted by vidioholic View Post
    Code:
     int i;
        for(i=0; i < s->top; i++) 
            printf("%s ", s->store[i]); 
            printf("\n");
    And you can't find i in this code because why?

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    oh ok ok i misunderstood you im sorry.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    do you want me to just post the entire thing so you can see what i have? i'll delete it after because it is for class. maybe there is more than one problem. i still have the same problem
    Last edited by vidioholic; 10-22-2008 at 12:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  2. weird output
    By kantze in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2006, 12:05 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. float gives weird output
    By Xarr in forum C Programming
    Replies: 4
    Last Post: 05-25-2004, 07:44 PM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 10:59 AM