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 7672531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:18:27+00:00 2026-05-31T16:18:27+00:00

My code handles Binary Search Trees, so those are the structures I a referencing

  • 0

My code handles Binary Search Trees, so those are the structures I a referencing in the code. But gdb seems to crash before running through any of my code.

/* my own recursive function, counts number of nodes in tree */
int count_nodes(bst_node_t *node)
{
  if( node == NULL )
    return 0;
  /* if this is a leaf, return 1 */
  if( node->left == NULL && node->right == NULL )
    return 1;
  /* if left side empty, return 1 + count for right subtree */ 
  else if( node->left == NULL )
    return 1 + count_nodes(node->right);
  /* if right side empty, return 1 + count for left subtree */
  else if( node->right == NULL )
    return 1 + count_nodes(node->left);
  /* otherwise, return 1 + count for both left and right subtrees */ 
  else
    return 1 + count_nodes(node->left) + count_nodes(node->right);
}

/* mallocs the header for the BST, initializes it, and returns pointer to it */
bst_t *bst_create (void)
{
  bst_t *tree = (bst_t *)malloc(sizeof(bst_t));
  tree->root = NULL;
  tree->tree_size = 0;
  tree->num_recent_key_comparisons = 0;
}

int main(void) 
{
  bst_t *tree = bst_create();
  printf("size = %d, comparisons = %d\n", tree->tree_size, tree->num_recent_key_comparisons);
  printf("size from count_nodes = %d\n", count_nodes(tree->root));
  free(tree);
}

Running this in gdb gets me the following crash:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000c
0x0000000100000cc8 in main ()

If I just run the code, it gets through the first print statement in main(), but crashes on the call to count_nodes, so I thought it was some odd pointer error, but gdb didn’t point out any line in the code, it gave e that error without printing anything out, which makes me think it didn’t get far in the code.

p.s. I only JUST installed gdb and MAKE on my mac, got it with the Xcode downloads from apple’s developer website. So there may have been some error with how I installed it.

  • 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-31T16:18:29+00:00Added an answer on May 31, 2026 at 4:18 pm

    You forgot to return the node in your bst_create

    bst_t *bst_create (void)
    {
      bst_t *tree = (bst_t *)malloc(sizeof(bst_t));
      tree->root = NULL;
      tree->tree_size = 0;
      tree->num_recent_key_comparisons = 0;
      return tree;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What we ideally need is, to know how Microsoft handles XAML generated code (Those
I have some code which handles data files and reports an error when it
I'm writing some code that handles logging xml data and I would like to
I'm a C++ amateur. I'm writing some Win32 API code and there are handles
I am working on some legacy code which opens a file and adds binary
I don't have the code available right now, but I'll describe my situation and
I've got a chunk of code that reads binary data off a string buffer
I have a server side code to process uploaded binary files: class UploadHandler(webapp.RequestHandler): def
I am trying to display a binary image through an httphandler. Here is the
I had some code before I moved to Unicode and Delphi 2009 that appended

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.