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

  • SEARCH
  • Home
  • 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 8474449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:37:46+00:00 2026-06-10T17:37:46+00:00

I have encountered a bug in the following linked list function and I have

  • 0

I have encountered a bug in the following linked list function and I have no idea what to do. The crazy part (to me) is that my code has been working perfectly up to now, and still does for several calls before hitting the bug.

The goal of this function is to add a new item (PathItem, which contains a BinaryItem) to the end of a linked list of PathItems. My function is as follows

void addPathItemAtEnd(struct PathItem * pathItem, struct BinaryItem * binaryItem) {
    if (pathItem == NULL) {
        pathItem = malloc(sizeof(struct PathItem));
        pathItem->binaryItem = binaryItem;
        pathItem->next = NULL;
    } else {
        while (pathItem->next != NULL) {
            pathItem = pathItem->next;
        }
        struct PathItem * newPathItem = malloc(sizeof(struct PathItem));
        newPathItem->binaryItem = binaryItem;
        newPathItem->next = NULL;
        pathItem->next = newPathItem;        
    }
}

with the problematic line being pathItem->next = newPathItem. I get the error EXC_BAD_ACCESS in the debugger. Once again, this code has functioned fine before, so I can’t understand what is happening now to upset it…

This seems to be an issue with reassigning the pointer variable. Don’t know what’s wrong.

Any thoughts, suggestions, criticisms. If needed, I can attach more/the rest of my code. Really need your help and thanks in advance.

  • 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-06-10T17:37:48+00:00Added an answer on June 10, 2026 at 5:37 pm

    Your main problem is that pathItem is passed in as a copy so that changes to it are not reflected back in the original code. C uses a pass-by-value paradigm (C++ also uses pass by value but it at least has reference types so you don’t have to muck about with C’s obtuse pointer methods).

    I can’t see how this ever would have worked simply because, if the pointer you pass in as pathItem is initially NULL, it stays NULL when you return. In other words, the list is always empty.

    Probably the easiest fix to that is to change the prototype to:

    void addPathItemAtEnd (struct PathItem *pPathItem, blah blah blah ...
    

    and then, wherever you currently have pathItem inside that function, replace it with (*pPathItem).

    And, of course, call it with the pointer, changing (something like):

    addPathItemAtEnd (listStart, newItem);
    

    to:

    addPathItemAtEnd (&listStart, newItem);
    

    As an aside, robust code would also check the return value from malloc and act accordingly. While classwork and little personal projects may be able to get away with assuming no failures, that won’t cut it in the real world 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're currently running with php 5.2.5. We have now encountered a bug that creates
Here is the pattern: $urlpattern = '%[^http://][^https://][\w]+(-[\w]+)*(\.[\w]+(-[\w]+)*)*\.[\w]{1,6}(\.[\w]{1,6})*[^/]%'; I have encountered a strange bug that
I have a recursive function with a bug that I am having trouble with.
I think I may have encountered a bug in mysql, or is it just
I have encountered what appears to be a bug with the YAML parser. Take
I have encountered a most annoying problem that occurs on the PWD variable when
I've encountered the following bug: http://issues.apache.org/jira/browse/AXIS2-4363 It states the following: This error only occurs
This may be the most obscure bug I have yet encountered in many years
I have encountered an annoying bug with internet explorer on a javascript animation I
i have encountered a really weirdest bug in eclipse, i would love if someone

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.