简体   繁体   中英

Function running when it shouldn't

I have the following snippets of code:

struct TreeNode *allocate_node(char *value) {
  struct TreeNode *new_node = malloc(sizeof(struct TreeNode));
  new_node->value = value;
  new_node->sibling = NULL;
  new_node->child = NULL;
  return new_node;
}
void tree_insert(struct TreeNode *root, char **values) {

  struct TreeNode *prev = root;
  struct TreeNode *curr = prev->child;


  for (int i = 0; i < 4; i++) {
    if (!curr) {
      prev->child = allocate_node(values[i]);

      prev = prev->child;
      curr = prev->child;
    }
  }
}
int main(void) {
  struct TreeNode root;
  root.value = "";
  root.child = NULL;
  root.sibling = NULL;

  char *command1[] = {"a", "b", "c", "d"};
  char *command2[] = {"e", "f", "g", "h"};

  tree_insert(&root, command1);
  tree_insert(&root, command2);

  printf("Printing current tree values: \n");
  struct TreeNode *curr = root.child;
  while (curr) {
    printf("%s ", curr->value);
    curr = curr->child;
  }
}

Expected output:

Current tree values:
a b c d

Actual output:

Current tree values:
e f g h

After the first tree_insert is run my tree is linked like this: (root) -> a -> b -> c -> d

But when the second tree_insert runs my old tree seems to have been replaced even though tree_insert shouldn't even run. New tree is as such: (root) -> e -> f -> g -> h

Since in the second tree_insert curr is no longer NULL, how come it's still running and overwriting my tree? I'm pretty new to C so I might be missing something really obvious

edit: TreeNode is defined as such:

struct TreeNode {
    char *value;
  struct TreeNode *sibling;
  struct TreeNode *child;
};
new_node->value = value;

This line leads me to believe that you are storing the pointer value in the structure.

The problem here is that the pointer is stored in the struct. This pointer points to some other location, usually in the stack. When this variable is changed, the data in the structure also changes.

You can store the data in the structure in the form of an array or you can use dynamic memory allocation to store the data.

For Dynamic memory allocation, the function allocate_node is changed as below.

struct TreeNode *allocate_node(char *value) {
  struct TreeNode *new_node = malloc(sizeof(struct TreeNode));
  int len = strlen(value);
  new_node->value = malloc(len +1);
  strcpy(new_node->value, value);
  new_node->sibling = NULL;
  new_node->child = NULL;
  return new_node;
}

To store in an array, you need to know the maximum length of value in advance. In this case the following changes are required.

struct TreeNode {
  char value[MAXLEN + 1];
  struct TreeNode *sibling;
  struct TreeNode *child;
};

struct TreeNode *allocate_node(char *value) {
  struct TreeNode *new_node = malloc(sizeof(struct TreeNode));
  strcpy(new_node->value,value);
  new_node->sibling = NULL;
  new_node->child = NULL;
  return new_node;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM