473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help debugging stack program

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);
}
Feb 6 '06 #1
1 1468
In article <43********@news1.homechoice.co.uk>,
White Spirit <wh**********@ntlworld.com> 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)
Feb 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Java script Dude | last post by:
To all Mozilla JS Guru's (IE dudes welcome), I have spent the last three years developing complex DHTML applications that must work in IE 5.5sp2+ but I use Mozilla 1.3+** to do all my...
6
by: Ken Varn | last post by:
I am trying to remote debug a C# application but the debugger is reporting the following exception: An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module....
2
by: hpoliset | last post by:
I have a debugging question w.r.t core dumps with signal 4 Illegal instruction messages. I analyzed the core file through gdb. In simple english following is the pattern observed: I have an...
3
by: Shmulik | last post by:
I have an application written in C# that creates a queue of items to process, connects via a TCP connection to a server on a Unix box, and then passes data files to the Unix box for processing...
7
by: yuanlinjiang | last post by:
I am working on converting a win32 console application from VC6 to VC7. My version is Microsoft Development Environment 2003 version 7.1.3088. The code works fine on VC6. it is a console...
4
by: pauldepstein | last post by:
I am writing code to price some financial options using recursive functions. It compiles but I get a runtime error which is caused by a memory violation. The problem is that the code is much...
2
by: gap | last post by:
I'm no c programmer, and I'm a ctypes newbie. I'll frame my problem as simply as I can. Sorry if it's too much or not enough info. I don't expect an explicit answer (but maybe), just help...
6
by: jluo | last post by:
I found a problem with my application when using "innerhtml" to replace the content from the server after making an ajax call. The display is perfectly correct, but when I use "view source" on the...
2
by: Zach | last post by:
I compiled a game client and it crashed (segmentation fault) resulting in a core file being generated. I'm trying to find out exactly what caused it to crash. Any ideas how I can do this with gdb?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.