Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help debugging stack program

Reply
Thread Tools

Help debugging stack program

 
 
White Spirit
Guest
Posts: n/a
 
      02-06-2006
The following program (code fragment but is supposed to work) which
implements three stacks segfaults when run and I can't for the life of
me think why. It worked perfectly before I changed it to use pointers,
and it doesn't get as far as printing the string in main.

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZEOFSTACK 100

struct stack
{
char value[SIZEOFSTACK];
int size;
} *controlStack, *resultStack, *memoryStack;

void pushToStack (char v, struct stack *ptrStack)
{
if (ptrStack->size < SIZEOFSTACK)
{
ptrStack->value[ptrStack->size] = v;
ptrStack->size++;
}

else

{
printf ("Error - stack out of bounds.\n");
}
}

char popFromStack (struct stack *ptrStack)
{
if (ptrStack->size == 0)
{
printf("Error - attempt to pop a value from an empty stack.");
/* Exception handling will go here */
}

else
{
return ptrStack->value[--ptrStack->size];
}
}

int main ()
{ printf ("got this far");
pushToStack('5', resultStack);
pushToStack('6', resultStack);
pushToStack('1', resultStack);
pushToStack('2', resultStack);

int i;

for (i = 1; i <=4; i++)
{
printf ("Popped %c\n", popFromStack(resultStack));
}
return (EXIT_SUCCESS);
}
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-06-2006
In article <43e7db50$(E-Mail Removed)>,
White Spirit <(E-Mail Removed)> wrote:
>The following program (code fragment but is supposed to work) which
>implements three stacks segfaults when run and I can't for the life of
>me think why. It worked perfectly before I changed it to use pointers,
>and it doesn't get as far as printing the string in main.


>#define SIZEOFSTACK 100
>
>struct stack
>{
> char value[SIZEOFSTACK];
> int size;
>} *controlStack, *resultStack, *memoryStack;


Okay, non-automatic variables, so they will be initialized to 0.
So controlStack and resultStack and memoryStack start out as
NULL pointers.

The problem should be obvious now
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Stack trace provided. Help needed in debugging rajatag Java 4 02-20-2007 12:48 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments