版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32768743/article/details/88852429
Line 18: Char 26: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct MyListNode', which requires 8 byte alignment (solution.c)
不知道LeetCode出了啥问题
我的提交代码
struct List { int count; struct MyListNode *head; struct MyListNode *tail; }; struct MyListNode { int val; struct MyListNode *next; }; void add(struct List *list, int val) { struct MyListNode *node = (struct MyListNode *)malloc(sizeof(struct MyListNode *)); node->val = val; if (list->head == NULL) { list->head = node; list->tail = node; list->count = 0; } else { list->tail->next = node; list->tail = node; } list->count ++; } struct Stack { struct StackNode *top; }; struct StackNode { struct TreeNode *node; struct StackNode *bottom; }; void push(struct Stack *stack, struct TreeNode *node) { struct StackNode *newStackNode = (struct StackNode *)malloc(sizeof(struct StackNode)); newStackNode->bottom = stack->top; newStackNode->node = node; stack->top = newStackNode; } struct TreeNode * pop(struct Stack *stack) { struct StackNode * top = stack->top; stack->top = top->bottom; return top->node; } int isEmpty(struct Stack * stack) { if (stack->top == NULL) { return 1; } else { return 0; } } /** * 前序遍历流程 * 1. 根节点入栈 * 2. 从栈中取出一个节点 * 3. 打印节点数据 * 4. 如果右节点不为空,入栈 * 5. 如果左节点不为空,入栈 * 6. 如果栈不为空,跳转到2 * @param root * @param returnSize * @return */ int* preorderTraversal(struct TreeNode* root, int* returnSize) { if (root == NULL) { *returnSize = 0; return (int*)malloc(sizeof(int) * 0); } struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack)); struct List *list = (struct List *)malloc(sizeof(struct List)); push(stack, root); while (!isEmpty(stack)) { root = pop(stack); if (root == NULL){ break; } int val = root->val; add(list, val); if (root->right != NULL) { push(stack, root->right); } if (root->left != NULL) { push(stack, root->left); } } int *ret = (int *)malloc(sizeof(int) * list->count); struct MyListNode * it = list->head; for(int i=0; i<list->count; i++) { ret[i] = it->val; it = it->next; } *returnSize = list->count; return ret; }
在本地可以跑起来的代码
#include <stdio.h> #include <stdlib.h> /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; struct List { int count; struct MyListNode *head; struct MyListNode *tail; }; struct MyListNode { int val; struct MyListNode *next; }; void add(struct List *list, int val) { struct MyListNode *node = (struct MyListNode *)malloc(sizeof(struct MyListNode *)); node->val = val; if (list->head == NULL) { list->head = node; list->tail = node; list->count = 0; } else { list->tail->next = node; list->tail = node; } list->count ++; } struct Stack { struct StackNode *top; }; struct StackNode { struct TreeNode *node; struct StackNode *bottom; }; void push(struct Stack *stack, struct TreeNode *node) { struct StackNode *newStackNode = (struct StackNode *)malloc(sizeof(struct StackNode)); newStackNode->bottom = stack->top; newStackNode->node = node; stack->top = newStackNode; } struct TreeNode * pop(struct Stack *stack) { struct StackNode * top = stack->top; stack->top = top->bottom; return top->node; } int isEmpty(struct Stack * stack) { if (stack->top == NULL) { return 1; } else { return 0; } } /** * 前序遍历流程 * 1. 根节点入栈 * 2. 从栈中取出一个节点 * 3. 打印节点数据 * 4. 如果右节点不为空,入栈 * 5. 如果左节点不为空,入栈 * 6. 如果栈不为空,跳转到2 * @param root * @param returnSize * @return */ int* preorderTraversal(struct TreeNode* root, int* returnSize) { if (root == NULL) { *returnSize = 0; return (int*)malloc(sizeof(int) * 0); } struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack)); struct List *list = (struct List *)malloc(sizeof(struct List)); push(stack, root); while (!isEmpty(stack)) { root = pop(stack); if (root == NULL){ break; } int val = root->val; add(list, val); if (root->right != NULL) { push(stack, root->right); } if (root->left != NULL) { push(stack, root->left); } } int *ret = (int *)malloc(sizeof(int) * list->count); struct MyListNode * it = list->head; for(int i=0; i<list->count; i++) { ret[i] = it->val; it = it->next; } *returnSize = list->count; return ret; } struct TreeNode * newTreeNode(int val) { struct TreeNode * node = (struct TreeNode *)malloc(sizeof(struct TreeNode)); node->val = val; return node; } int main() { struct TreeNode *root = newTreeNode(1); struct TreeNode *right = newTreeNode(2); struct TreeNode *left = newTreeNode(3); root->right = right; right->left = left; int count = 0; int *ret = preorderTraversal(root, &count); printf("count: %d\n", count); for(int i=0;i < count; i++) { printf("%d ", ret[i]); } printf("\n"); }
转载请标明出处:runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct MyListNod
文章来源: https://blog.csdn.net/qq_32768743/article/details/88852429