malloc 未定义 [英] malloc undefined

查看:108
本文介绍了malloc 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重写一个链表模块,我收到了一些奇怪的错误.

I am currently working on rewriting a linked list module and I am receiving some weird errors.

在两个 IDE(Netbeans 和 Visual Studio Express)中,我收到一个警告,提示 malloc 未定义,并且在我的linkedlist.c 文件中找到的函数也未定义.

In two IDEs (Netbeans & Visual Studio Express), I am getting a warning that malloc is undefined and that a function found in my linkedlist.c file is not defined either.

以下是我的 3 个文件.

below are my 3 files.

main.c

#include <stdlib.h>  
#include <stdio.h>  
#include "linkedlist.h"  
int main(void){  
 struct linked_list * l_list;   
 l_list = new_list();  
 printf("%i", l_list->length);  
 getchar();  
 return (EXIT_SUCCESS);  
}

链表.h

#ifndef LINKEDLIST_H  
#define LINKEDLIST_H  
struct linked_list{  
 int length;  
 struct linked_list_node * head_node_ptr;  
};  
struct linked_list_node{  
 struct linked_list_node * prev_node_ptr;  
 struct linked_list_node * next_node_ptr;  
 struct linked_list_data * head_data_ptr;  
};  
struct linked_list_data{  
 struct linked_list_data * prev_data_ptr;  
 struct linked_list_data * next_data_ptr;  
 void * data;  
};  
struct linked_list * new_list();  
#endif  

链表.c

#include "linkedlist.h"  
struct linked_list * new_list(){  
 struct linked_list * temp_list = malloc(sizeof(struct linked_list));  
 temp_list->length = 5;  
 return temp_list;  
}  

任何帮助将不胜感激.我不确定这是语法问题还是我的计算机上缺少文件.

Any help would be greatly appreciated. I am unsure if this is a syntax issue or missing files on my computer.

推荐答案

你在哪里包含 <stdlib.h> - 因为那是 malloc() 在哪里宣布?

Where do you include <stdlib.h> — because that is where malloc() is declared?

这是编译问题(malloc() 未声明)还是链接问题(malloc() 未定义)?

Is this a compilation problem (malloc() undeclared) or a linking problem (malloc() undefined)?

错误信息到底是什么?

现在代码可读了:

  • <cstdlib> 是 C++ 标头(<cstdio> 也是如此).
  • 您需要在 C 中包含 <stdlib.h>.
  • 您需要在 linkedlist.c 中包含使用 malloc() 函数的 <stdlib.h>..李>
  • <cstdlib> is a C++ header (so is <cstdio>).
  • You need to include <stdlib.h> in C.
  • You need to include <stdlib.h> where the malloc() function is used — in linkedlist.c.

您还linkedlist.h 中的错误位置放置了标头保护,但问题中的代码已经更新.原来的顺序是:

You also have had the header guards in the wrong place in linkedlist.h, but the code in the question has been updated since. Originally, the sequence was:

#ifndef LINKEDLIST_H  
#define LINKEDLIST_H
#endif
struct linked_list{
…

头文件的规范结构是:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

...the other material in the header...
...definitions and declarations...

#endif /* HEADER_H_INCLUDED */

#endif 是文件中最后一个非注释非空行——不是第三个.

The #endif is the last non-comment, non-blank line in the file — not the third.

即使是双向链表,您的数据结构也异常复杂.你确定你会经常需要长度以保证在列表中的每个节点中维护指向头部的指针吗?如果您经常使用它,我会感到惊讶.我假设您在每个节点中都有指向头的指针,因此当您从列表中删除任意节点时,您可以减少列表的长度.传递一个指向列表的指针和指向要删除的节点的指针可能比你得到的更好.

Your data structures are extraordinarily complicated for even a doubly-linked list. Are you sure you're going to be needing the length so often that it warrants maintaining the pointer to the head in every node in the list? I'd be surprised if you are using it that often. I assume that you have the pointer-to-head in each node so that when you remove an arbitrary node from the list you can decrement the length of the list. You'd probably be better off passing a pointer to the list and the pointer to the node to be deleted than what you've got.

我认为 new_list() 函数中的 length = 5 没有任何理由.

I can see no justification for the length = 5 in the new_list() function.

此外,C 和 C++ 在以下含义上存在根本差异:

Also, C and C++ differ radically on the meaning of:

struct linked_list * new_list();

在 C++ 中,这意味着new_list() 是一个不带参数并返回 struct linked_list 指针的函数".

In C++, that means "new_list() is a function that takes no arguments and returns a struct linked_list pointer".

在 C 中,这意味着new_list() 是一个具有完全不确定的参数列表的函数,它返回一个 struct linked_list 指针".函数已声明,但函数没有原型.

In C, that means "new_list() is a function with a completely indeterminate argument list that returns a struct linked_list pointer". The function is declared, but there is no prototype for the function.

在 C 中,你应该写:

In C, you should write:

struct linked_list * new_list(void);

就个人而言,我更喜欢在标头中的函数声明前面看到 extern;对于函数来说它实际上不是必需的,但由于它 (extern) 是(或应该)在头文件中声明的变量所必需的,所以我更喜欢在头文件中声明的函数的对称性.

Personally, I prefer to see extern in front of function declarations in headers; it is not actually necessary for functions, but since it (extern) is (or should be) necessary for variables declared in headers, I prefer the symmetry for functions declared in headers.

这篇关于malloc 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆