# Debug记录（更新：Valgrind: Conditional jump or move depends on uninitialized value(s))

## Valgrind: Conditional jump or move depends on uninitialized value(s)

{% embed url="<https://bytes.usc.edu/cs104/wiki/valgrind/>" %}

里面给出的例子很易懂，不过我在写一个给链表添加节点的函数的时候遇到了一个不太明显的：

```c

Node* appendNode(Node* head, int v) {
   if (head == NULL) {
      return NULL;
   }

    Node* p = head;
    while (p->next != NULL) {
        p = p->next;
    }

    // bad style
    p->next = (Node *)malloc(sizeof(Node));
    p = p->next;
    p->data = v;
    p->next = NULL;

    return p;
}
```

这里valgrind报错是因为：给p->next分配了空间后，这个空间是没有被赋值的（即没有被初始化），然后又执行了p = p->next，相当于跳到了一个没有被初始化的地方。

应该改为：

```c
Node *node = (Node *)malloc(sizeof(Node));
    node->data = v;
    node->next = NULL;
    p->next = node;
```

## 相同的hashcode

```java
public static int hashCode(int[] array) {
        int total = 0;
        for (int element : array) {
            total = total * 256;
            total = total + element;
        }
        return total;
    }

public static void main(String[] args) {
    int[] array1 = {80, 103, 142, 91, 160, 250, 7, 7, 7, 7};
    int[] array2 = {130, 105, 209, 7, 7, 7, 7};
    int hashCode1 = hashCode(array1);
    int hashCode2 = hashCode(array2);
    System.out.println("hashCode1: " + hashCode1);//117901063
    System.out.println("hashCode2: " + hashCode2);//117901063
    }
```

这个hashCode()函数会产生相同的hashCode，256=2^8，相乘几次后会溢出，hashCode取决于数组的最后几位数字


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://linde.gitbook.io/blog/debug-ji-lu-geng-xin-valgrind-conditional-jump-or-move-depends-on-uninitialized-values.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
