member access within misaligned address 0x000100000001 for type 'struct ListNode', which requires 8
2018-03-27 15:15
555 查看
LeetCode 725
问题代码:struct ListNode** lstArr = (struct ListNode**)malloc(k * sizeof(struct ListNode*) );
失败了
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
struct ListNode** splitListToParts(struct ListNode* root, int k, int* returnSize) {
// 1. handle null case
if(NULL == root) return NULL;
// 2. allocate memory
struct ListNode* lstArr = (struct ListNode*)malloc(k * sizeof(struct ListNode));
returnSize = (int *)malloc(k * sizeof(int));
for(int i=0; i<k; i++){
lstArr[i].next = NULL;
}
// 3. compute length of list, && splitted list
int nodeLen = 0, minimum = 0, rem = 0;
struct ListNode* pNode = root, *tail = NULL;
while(NULL != pNode) {
nodeLen ++;
pNode = pNode->next;
}
minimum = nodeLen/k, rem = nodeLen%k;
//printf("%d\n", rem);
for(int i=0; i<k; i++){
returnSize[i] = minimum;
}
for(int j=0; j<rem; j++){
returnSize[j] += 1;
}
// 4. split the list
pNode = root;
for(int i=0; i<k; i++){
lstArr[i].val = pNode->val;
if(NULL != pNode)
lstArr[i].next = pNode->next;
int t = returnSize[i];
tail = NULL;
while(NULL != pNode && t>0){
tail = pNode;
pNode = pNode->next;
t--;
}
if(NULL != tail) tail->next = NULL;
}
// 5. return result
return &lstArr;
}
问题代码:struct ListNode** lstArr = (struct ListNode**)malloc(k * sizeof(struct ListNode*) );
失败了
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
struct ListNode** splitListToParts(struct ListNode* root, int k, int* returnSize) {
// 1. handle null case
if(NULL == root) return NULL;
// 2. allocate memory
struct ListNode* lstArr = (struct ListNode*)malloc(k * sizeof(struct ListNode));
returnSize = (int *)malloc(k * sizeof(int));
for(int i=0; i<k; i++){
lstArr[i].next = NULL;
}
// 3. compute length of list, && splitted list
int nodeLen = 0, minimum = 0, rem = 0;
struct ListNode* pNode = root, *tail = NULL;
while(NULL != pNode) {
nodeLen ++;
pNode = pNode->next;
}
minimum = nodeLen/k, rem = nodeLen%k;
//printf("%d\n", rem);
for(int i=0; i<k; i++){
returnSize[i] = minimum;
}
for(int j=0; j<rem; j++){
returnSize[j] += 1;
}
// 4. split the list
pNode = root;
for(int i=0; i<k; i++){
lstArr[i].val = pNode->val;
if(NULL != pNode)
lstArr[i].next = pNode->next;
int t = returnSize[i];
tail = NULL;
while(NULL != pNode && t>0){
tail = pNode;
pNode = pNode->next;
t--;
}
if(NULL != tail) tail->next = NULL;
}
// 5. return result
return &lstArr;
}
相关文章推荐
- member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8
- member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment
- LeetCodeBug-member access within null pointer of type 'struct ListNode'
- qt 错误:request for member 'show' in 'showlist_ui', which is of non-class type 'ShowList*'
- Hibeinate: Could not determine type for: java.util.List, at table: address_inf, for columns: [org.hi
- 对list_entry(ptr, type, member)的理解
- 1006. insert for single link list with head node
- The method getTextContent() is undefined for the type Node
- The method getTextContent() is undefined for the type Node
- list中节点如何与迭代器关联(List中iterator begin() { return (link_type)((*node).next); }的疑问)
- The method getTextContent() is undefined for the type Node 的解决办法(eclipse)
- define a class for a linked list and write a method to delete the nth node.
- Node“getTextContent() is undefined for the type Node”处理办法
- 1007. remove for single link list with head node
- error: request for member ‘***’ in ‘***’, which is of non-class type ‘***’
- Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hos
- The method getTextContent() is undefined for the type Node
- The method list(String, Object[]) is ambiguous for the type BaseHibernateDao<M,PK>错误解决方法
- The method getTextContent() is undefined for the type Node
- The method getTextContent() is undefined for the type Node