Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6760453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:03:57+00:00 2026-05-26T14:03:57+00:00

I’m at a complete loss. I feel like there must be some glaring, stupidly

  • 0

I’m at a complete loss. I feel like there must be some glaring, stupidly easy mistake here, and my eyes are just too tired to see it. I’d really appreciate any help.

When I run testProb2, the program prints “Pushing:” and then on the next line, “Segmentation fault”. I find this really weird, because the next thing after printf(“Pushing:\n”); is another call to printf with just a static argument being passed, nothing dynamic that could be doing weird crazy things in some other method, and yet “1 ” does not get printed.

I put the call to printf for 1 and 2 in there just as a test, because I initially thought that the problem might be in my first for loop, which is commented out right now, but that’s not it. As I said, I believe the problem to be in testProb2.c, but I included stackli.h and stackli.c below it just in case. I’m compiling this with “gcc -ansi stackli.c testProb2.c -o testProb2”.

/* testProb2
 * 
 * Demonstrates a stack implementation that allocates a number of nodes at creation of the stack
 * rather than on each call to Push. All methods are O(1) except for GrowFreeList, which is O(n),
 * and CreateStack, which is O(n) because it calls GrowFreeList, and possibly Push, which will be
 * O(1) when called while there are empty nodes, but O(n) when called if there are not empty nodes.
 */

#include "stackli.h"
#include <stdio.h>

int main(void) {
    Stack S;
    int i;

    S = CreateStack(8);

    printf("Pushing:\n");
    printf("1 ");
    Push(1, S);
    printf("2\n");
    Push(2, S);
                /*
                for (i = 0; i < 10; i++) {
                    printf("%d...", i);
                    Push(i, S);
                }
                printf("]\n");
                */

    printf("Popping:\n");
    while (!IsEmpty(S)) {
        printf("%d...", Top(S));
        Pop(S);
    }
    printf("]");
    DisposeStack(S);
}
/* stackli.h */

    typedef int ElementType;

    #ifndef _Stack_h
    #define _Stack_h

    struct Node;
    struct StackRecord;
    typedef struct Node *PtrToNode;
    typedef struct StackRecord *Stack;

    Stack CreateStack( int initialSize );
    void GrowFreeList( Stack S );
    int IsEmpty( Stack S );
    int IsFull( Stack S ) ;
    void MakeEmpty( Stack S );
    void DisposeStack( Stack S );
    void Push( ElementType X, Stack S );
    ElementType Top( Stack S );
    void Pop( Stack S );


    #endif  /* _Stack_h */
/* stackli.c */

#include "stackli.h"
#include "fatal.h"
#include <stdlib.h>

struct StackRecord {
    PtrToNode ThisStack;
    PtrToNode FreeNodes;
    int Size;
};

struct Node {
    ElementType Element;
    PtrToNode   Next;
};

/* O(n) instead of O(1) because it calls GrowFreeList which is O(n) */
Stack CreateStack(int initialSize) {
    Stack S;
    S = malloc( sizeof( struct StackRecord ) );
    S->ThisStack = NULL;
    S->FreeNodes = NULL;
    S->Size = initialSize;            
    GrowFreeList(S);
    return S;
}

/* O(n) function */
void GrowFreeList(Stack S) {
    int i;
    PtrToNode temp;

    for (i = 0; i < S->Size; i++) {
        temp = malloc( sizeof( struct Node) );
        if (temp == NULL)
            FatalError("Out of space!!");
        temp->Next = S->FreeNodes;
        S->FreeNodes = temp;
    }
    S->Size = S->Size * 2;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:03:58+00:00Added an answer on May 26, 2026 at 2:03 pm

    The problem is in the last two lines of Push:

    void Push( ElementType X, Stack S ) {
        PtrToNode temp;
        ...
        temp->Next = S->ThisStack->Next;
        S->ThisStack = temp;
    }
    

    When you first call Push, the ThisStack field is null. When you try to dereference it to access its Next field, you are getting a segfault. However, since the top of the stack is in ThisStack, not ThisStack->next, fixing that problem will get rid of the segfault.

    void Push( ElementType X, Stack S ) {
        PtrToNode temp;
        ...
        temp->Next = S->ThisStack;
        S->ThisStack = temp;
    }
    

    You make the same mistake in Pop, which would cause you to entirely skip the first element. The assignment to temp should be like this:

    temp = S->ThisStack;
    

    Finally, your Size field will always be wrong. It appears that GrowFreeList is supposed to double the size of the stack when it is called, but when you call it from CreateStack, your stack’s real size is 0 although the Size field is 8 (in your example). The result is that the stack contains 8 free nodes, but its Size field is 16. In fact, the Size field will always be larger than the actual size by the initial size. This doesn’t cause any problems now, since it is only used to determine how many to add, but the fix is simple: reset the Size field after calling GrowFreeList from CreateStack:

    Stack CreateStack(int initialSize) {
        Stack S;
        ...
        S->Size = initialSize;
        GrowFreeList(S);
        S->Size = initialSize; // Reset size, since GrowFreeList changed it
        return S;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.