Find the answer to your Linux question:
Results 1 to 5 of 5
according to gdb, the error accurs in the eip register. when i put in 1 for the last variable i get bus error anything else gives segmentation fault, below is ...
  1. #1
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85

    bus error c++

    according to gdb, the error accurs in the eip register.

    when i put in 1 for the last variable i get bus error anything else gives segmentation fault, below is the source code (its a small program)

    Code:
    #include <iostream>
    
    struct stack
    {
    	//-----member functions----//
    	void init();
    	int push();
    	void pop();
    	//----/member function----//
    	int value_to_be_added;
    	bool Is_Full;
    	bool Is_Empty;
    	int Stack_Size;
    	int Top_Of_Stack;
    	int The_Stack[15];
    };
    
    void stack::init()
    {
        Is_Full=false                                     ;
        Is_Empty=true                                     ;
        Stack_Size=(sizeof(The_Stack)/sizeof(int)); //i couldnt prevent a magic 
        //number in the stack object, however i think by using the sizeof i can still make it so
        //thats the only instance of the number, which should work fine. since the sizeof the array 
        //is the sizeof its data type * number of entries.
    }
    int stack::push()
    {
    //-----------local variables-----//
    char buffer[255]        ;
    value_to_be_added=0 ;
    //---------/local variables-----//
    	if(Top_Of_Stack >= Stack_Size)
            {
                Is_Full=true;
                return(1);
            }
            if (Top_Of_Stack>0)
            {
                Is_Empty=false;
            }
            std::cout << "please enter item number " << (Top_Of_Stack) << ": ";
                std::cin >> buffer; //cin to buffer
                value_to_be_added =strtol(buffer, (char **) NULL, 0); //parse to int, works like atoi but
            //you can put in a hex value too.
            Top_Of_Stack++; //top of stack is just the number of the top entry on the stack
                if(value_to_be_added != -1) //only adds values that ARENT the break out value
                {
                    The_Stack[Top_Of_Stack] = value_to_be_added;
                }
                else//if the break out value is encountered
                {
                    Top_Of_Stack--; //decrement the top of the stack to not include it.
                }
        return(0);
    }
    void stack::pop()
    {
    if(!Is_Empty)
        {
            for(int i=Top_Of_Stack; i>0; i--)
            {
                std::cout << "popping item " << The_Stack[i] << " from the top of the stack\n";
                Top_Of_Stack=i; //reduces the number for top_of_stack. in case this needs
                //to be used again somewhere else.
            }
        }
    }
    
    int main()
    {
        //-------------------------variables---------------------------
        stack Mystack;
        Mystack.init();
        char buffer[255]                                          ;
        //like before this is just a buffer with 255 characters
        Mystack.Top_Of_Stack=0                                    ;
        //-------------------------------------------------------------
        do
        {
            if(!Mystack.Is_Full)
            {
            if (Mystack.push() ==1)
    	{
    	break;
    	}
            }
            
            
    
        }
        while(Mystack.value_to_be_added !=-1 && !Mystack.Is_Full);
        Mystack.pop();
        std::cout << "all done \n";
    //    perror("something: ");
    return(0);
    }
    i think the problem is in the variables in the struct. after the program exits they arent being destroyed, or something like that. all functions exit normally.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Here's the problem.
    Code:
    The_Stack[Top_Of_Stack] = value_to_be_added;
    In your code, when you execute this line, Top_Of_Stack ranges between 1 and 15, inclusive. Array indexes in C++ (and in C) are origin zero. Since your stack size is 15, you want Top_Of_Stack to be between 0 and 14.

    As an aside, instead of:
    Code:
    Stack_Size=(sizeof(The_Stack)/sizeof(int));
    consider using:
    Code:
    Stack_Size=(sizeof(The_Stack)/sizeof(The_Stack[0]));
    That way, if you ever change the type of The_Stack to, say, long long[], you won't have to change this line in the code.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    hmm, well when i run it i get this:

    please enter item number 0: 1
    please enter item number 1: 2
    please enter item number 2: 3
    please enter item number 3: 4
    please enter item number 4: 5
    please enter item number 5: 6
    please enter item number 6: 7
    please enter item number 7: 8
    please enter item number 8: 9
    please enter item number 9: 10
    please enter item number 10: 11
    please enter item number 11: 12
    please enter item number 12: 13
    please enter item number 13: 14
    please enter item number 14: 15
    popping item 15 from the top of the stack
    popping item 14 from the top of the stack
    popping item 13 from the top of the stack
    popping item 12 from the top of the stack
    popping item 11 from the top of the stack
    popping item 10 from the top of the stack
    popping item 9 from the top of the stack
    popping item 8 from the top of the stack
    popping item 7 from the top of the stack
    popping item 6 from the top of the stack
    popping item 5 from the top of the stack
    popping item 4 from the top of the stack
    popping item 3 from the top of the stack
    popping item 2 from the top of the stack
    popping item 1 from the top of the stack
    all done
    Segmentation fault
    it shows that the array is going from 0-14 and pops 14-0 all values entered come back out but there is a segmentation fault (or bus error if number 14 is a 1 or 2)

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    it shows that the array is going from 0-14 and pops 14-0
    That's a reasonable assumption to make, but it turns out that it shows nothing of the sort. I added one line to your program, at line 49:
    Code:
    #include <iostream>
    
    struct stack
    {
            //-----member functions----//
            void init();
            int push();
            void pop();
            //----/member function----//
            int value_to_be_added;
            bool Is_Full;
            bool Is_Empty;
            int Stack_Size;
            int Top_Of_Stack;
            int The_Stack[15];
    };
    
    void stack::init()
    {
        Is_Full=false                                     ;
        Is_Empty=true                                     ;
        Stack_Size=(sizeof(The_Stack)/sizeof(int)); //i couldnt prevent a magic 
        //number in the stack object, however i think by using the sizeof i can still make it so
        //thats the only instance of the number, which should work fine. since the sizeof the array 
        //is the sizeof its data type * number of entries.
    }
    int stack::push()
    {
    //-----------local variables-----//
    char buffer[255]        ;
    value_to_be_added=0 ;
    //---------/local variables-----//
            if(Top_Of_Stack >= Stack_Size)
            {
                Is_Full=true;
                return(1);
            }
            if (Top_Of_Stack>0)
            {
                Is_Empty=false;
            }
            std::cout << "please enter item number " << (Top_Of_Stack) << ": ";
                std::cin >> buffer; //cin to buffer
                value_to_be_added =strtol(buffer, (char **) NULL, 0); //parse to int, works like atoi but
            //you can put in a hex value too.
            Top_Of_Stack++; //top of stack is just the number of the top entry on the stack
                if(value_to_be_added != -1) //only adds values that ARENT the break out value
                {
                    printf("debug: >>> about to use index %d\n",Top_Of_Stack);
                    The_Stack[Top_Of_Stack] = value_to_be_added;
                }
                else//if the break out value is encountered
                {
                    Top_Of_Stack--; //decrement the top of the stack to not include it.
                }
        return(0);
    }
    void stack::pop()
    {
    if(!Is_Empty)
        {
            for(int i=Top_Of_Stack; i>0; i--)
            {
                std::cout << "popping item " << The_Stack[i] << " from the top of the stack\n";
                Top_Of_Stack=i; //reduces the number for top_of_stack. in case this needs
                //to be used again somewhere else.
            }
        }
    }
    
    int main()
    {
        //-------------------------variables---------------------------
        stack Mystack;
        Mystack.init();
        char buffer[255]                                          ;
        //like before this is just a buffer with 255 characters
        Mystack.Top_Of_Stack=0                                    ;
        //-------------------------------------------------------------
        do
        {
            if(!Mystack.Is_Full)
            {
            if (Mystack.push() ==1)
            {
            break;
            }
            }
            
            
    
        }
        while(Mystack.value_to_be_added !=-1 && !Mystack.Is_Full);
        Mystack.pop();
        std::cout << "all done \n";
    //    perror("something: ");
    return(0);
    }
    The output I got was this:
    Code:
    please enter item number 0: 1
    debug: >>> about to use index 1
    please enter item number 1: 2
    debug: >>> about to use index 2
    please enter item number 2: 3
    debug: >>> about to use index 3
    please enter item number 3: 4
    debug: >>> about to use index 4
    please enter item number 4: 5
    debug: >>> about to use index 5
    please enter item number 5: 6
    debug: >>> about to use index 6
    please enter item number 6: 7
    debug: >>> about to use index 7
    please enter item number 7: 8
    debug: >>> about to use index 8
    please enter item number 8: 9
    debug: >>> about to use index 9
    please enter item number 9: 10
    debug: >>> about to use index 10
    please enter item number 10: 11
    debug: >>> about to use index 11
    please enter item number 11: 12
    debug: >>> about to use index 12
    please enter item number 12: 13
    debug: >>> about to use index 13
    please enter item number 13: 14
    debug: >>> about to use index 14
    please enter item number 14: 15
    debug: >>> about to use index 15
    popping item 15 from the top of the stack
    popping item 14 from the top of the stack
    popping item 13 from the top of the stack
    popping item 12 from the top of the stack
    popping item 11 from the top of the stack
    popping item 10 from the top of the stack
    popping item 9 from the top of the stack
    popping item 8 from the top of the stack
    popping item 7 from the top of the stack
    popping item 6 from the top of the stack
    popping item 5 from the top of the stack
    popping item 4 from the top of the stack
    popping item 3 from the top of the stack
    popping item 2 from the top of the stack
    popping item 1 from the top of the stack
    all done 
    Segmentation fault
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    ah, its that damn top_of_stack++...i kept having a hard time finding a good place for that.

    i managed to get it though. thanks for the help ^_^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...