GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2005, 13:59
boris boris is offline
New Member
 
Join Date: Feb 2005
Posts: 1
boris is on a distinguished road

Help with segmentation fault


Hi guys , im working on a calculator programn and i kept getting this segmentation fault when i run it , This is my code , can someone give me a hand on what is wrong with it , thank you.

stack.h
CPP / C++ / C Code:
// Stack data structure
typedef struct stackNode
{
    int value;
    struct stackNode *next;
} stackElement;

typedef stackElement *stackPtr;


// Functions
stackPtr newElement(void);
void setElement(stackPtr element, int val);
stackPtr push(stackPtr stack, stackPtr element);
stackPtr pop(stackPtr stack);
stackPtr top(stackPtr stack);
void printStack(stackPtr stack);
int isdigit(char c);
int isspace(char c);

stack.c:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

stackPtr newElement(void)
{
    stackPtr element;

    element = (stackPtr) malloc(sizeof(stackElement));
    element->next = NULL;
    return(element);
}

void setElement(stackPtr element, int val)
{
    element->value = val;
}

stackPtr push(stackPtr stack, stackPtr element)
{
    element->next = stack;
    return(element);
}

stackPtr pop(stackPtr stack)
{
    stackPtr tmp;

    if (stack == NULL)
    return(NULL);
    tmp = stack->next;
    free(stack);
    return(tmp);
}

stackPtr top(stackPtr stack)
{
    return(stack);
}

void printStack(stackPtr stack)
{
    stackPtr tmp;

    printf("The stack contains:\n");
    for(tmp = stack; tmp != NULL; tmp = tmp->next)
    printf("\tStack element: %d\n", tmp->value);
    printf("\n");
    return;
}

int isdigit(char c)
{
    return(c >= '0' && c <= '9');
}

int isspace(char c)
{
    // space and tab characters are considered 'space'
    return(c == ' ' || c == '\t');
}

calc.c: 
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

int main(void)
{
    stackPtr tmp, stack = NULL;
    char c;
    int number = 0, op1, op2;

    c = getchar();
    while(c != EOF)
    {
        while(isspace(c)) // skip whitespace
        c = getchar();
        if (c == '\n') // we're done - print the result which is at the top of the stack
        {
            printf("Expression evaluates to %d\n", top(stack)->value);
            c = getchar();
            continue;
        }
        else if(isdigit(c)) // we need to get and store the integer
        {
            number = c -'0';
            while(isdigit(c = getchar()))
                number = (number * 10) + c - '0';
            tmp = newElement();
            setElement(tmp, number);
            stack = push(stack, tmp);
        }
            else // it must be an operator - we assume correct input
        {
            // get the first two numbers from the stack and
            // perform necessary operation storing it back on stack
            op1 = top(stack)->value;
            stack = pop(stack);
            op2 = top(stack)->value;
            stack = pop(stack);
            tmp = newElement();
            switch(c)
            {
                case '+':
                    setElement(tmp, op1 + op2);
                    break;
                case '*':
                    setElement(tmp, op1 * op2);
                    break;
                case '-':
                    setElement(tmp, op2 - op1);
                    break;
            }
            stack = push(stack, tmp);
            c = getchar();
        }
    }

    return 0;
}
Last edited by LuciWiz : 26-Mar-2006 at 09:45. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 02-Feb-2005, 15:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by boris
Hi guys , im working on a calculator programn and i kept getting this segmentation fault when i run it , This is my code , can someone give me a hand on what is wrong with it , thank you.


For reasonable expectation of help, you should tell us:

1. What is the program supposed to do (I know, you said it is a calculator, but I mean specifically: what kind of expressions it is supposed to process; infix notation? postfix? --- what?)

2. What input(s) did you give it? Show us exactly what you entered.

3. What did you expect the program to do?

4. What did it do? (I know --- you said it crashed, but did it do anything before it crashed? Did it always crash, or just sometimes?)

Now, here's a method that can (sometimes) let you get to the bottom of things instead of having to wait for someone else to respond:

Put printf() statements at various places in the program. This does two things:

(a) It might let you know where the problem is. (That is how far did it get before the crash.)

(b) You can make the printf() statements tell you what the program is working on at any given step (sometimes it's not seeing what you think it is).

For example
CPP / C++ / C Code:
    while(c != EOF)
    {
        while(isspace(c)) // skip whitespace
        c = getchar();
        printf("End of whitespace, c = %c (0x%02x hex)\n", c, c);

and

CPP / C++ / C Code:
            while(isdigit(c = getchar()))
                number = (number * 10) + c - '0';
            tmp = newElement();
            printf("After newwElement(), calling setElement(%p, %d)\n", 
                    tmp, number);
            setElement(tmp, number);
            printf("After setElement(), calling push(%p, %p)\n",
                    stack, tmp);

These are just a couple of places to put the printf() statements. Put lots of printf() until you see exactly where things are going wrong.

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault in c++ rushman8282 C++ Forum 2 26-Jan-2005 04:38
Segmentation Fault? aaroncohn C++ Forum 2 02-Jan-2005 15:22
Why seg fault in char array? nusstu C Programming Language 11 24-Aug-2004 17:10
Need a help with C code-Segmentation Fault nkhambal C Programming Language 13 18-Jul-2004 15:58
child pid xxx exit signal Segmentation fault (11) bezak Apache Web Server Forum 1 24-Nov-2003 10:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.