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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:49:37+00:00 2026-06-18T15:49:37+00:00

I trying to build a BST and insert nodes in it. However while creating

  • 0

I trying to build a BST and insert nodes in it. However while creating a new node I keep getting exc_bad access error.What can be the reason? Here is my code:

struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct     Node *rightChild, struct Node *leftChild)
{
struct Node *node = malloc(sizeof *node);
nodeKey= malloc (sizeof (bst->key_size));
nodeVal = malloc(sizeof(bst->value_size));
size_t sizeKey = sizeof(nodeKey);
memcpy(node->key, nodeKey, sizeKey);  // exc_bad access
size_t sizeVal = sizeof (nodeVal);
memcpy(node->val, nodeVal, sizeVal); // exc_bad access
node->right = rightChild;  
node->left = leftChild;

return node;

}

struct Node {
void *key;
struct Value *val;
struct Node *left;
struct Node *right;
};



struct BSTree {
size_t key_size, key_alignment;
size_t value_size, value_alignment;
int (*compare_func)(void *, void *);
struct Node *root;
// ... Maybe some other stuff.
};

struct Value {
char name[10];
int id;
};
  • 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-18T15:49:38+00:00Added an answer on June 18, 2026 at 3:49 pm

    Without looking at the Node structure, I would guess what you want to do is:

    if node is defined as

    struct Node {
        void *key;
        struct Value *val;
        struct Node *right;
        struct Node *left;
    };
    

    then

    struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct     Node *rightChild, struct Node *leftChild)
    {
    struct Node *node = malloc(sizeof *node);
    
      node->key = malloc(bst->key_size);          /* No sizeof here */
      node->val = malloc(bst->value_size);
      memcpy(node->key, nodeKey, bst->key_size);  
      memcpy(node->val, nodeVal, bst->value_size);
      node->right = rightChild;  
      node->left = leftChild;
    
      return node;
    }
    

    As you don’t check for the returns of the mallocs (which is a design choice that can e justified), you can even write it simpler that way.

    struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct     Node *rightChild, struct Node *leftChild)
    {
    struct Node *node = malloc(sizeof *node);
    
      node->key = memcpy(malloc(bst->key_size)  , nodeKey, bst->key_size); 
      node->val = memcpy(malloc(bst->value_size), nodeVal, bst->value_size);
      node->right = rightChild;  
      node->left = leftChild;
      return node;
    }
    

    There are people that cringe at this style but I prefer to not dilute my code too much on redundancies.

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

Sidebar

Related Questions

While trying to build libnfc-1.6.0-rc1 on 64 bit Windows 7 OS, I am getting
While trying to build a project using intelliJ Ultimate 12 I am getting these
I'm trying build a method which returns the shortest path from one node to
Trying to build a basic 3-layer architecture in my new application suite. For first
Trying to build a function that can insert into a SQL database regardless of
I'm trying to build an raw type BST , with Comparable<T> . The thing
I'm brand new to this and I'm trying build a simple layout of divs.
trying to build a query from a few tables here, and getting stumped on
In trying to build boost mirror with gcc 4.7.2, I ran into this error,
When trying to build I get this error: $ make CC=i686-w64-mingw32-gcc CC aes.c i686-w64-mingw32-gcc:

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.