C语言 malloc 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4529459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
malloc undefined
提问by FrostedPixel
I am currently working on rewriting a linked list module and I am receiving some weird errors.
我目前正在重写一个链表模块,我收到了一些奇怪的错误。
In two IDEs (Netbeans & Visual Studio Express), I am getting a warning that mallocis undefined and that a function found in my linkedlist.c file is not defined either.
在两个 IDE(Netbeans 和 Visual Studio Express)中,我收到一条警告,指出malloc未定义,并且在我的 linkslist.c 文件中找到的函数也未定义。
below are my 3 files.
下面是我的 3 个文件。
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);
}
linkedlist.h
链表.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
linkedlist.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.
任何帮助将不胜感激。我不确定这是语法问题还是我的计算机上缺少文件。
回答by Jonathan Leffler
Where do you include <stdlib.h>— because that is where malloc()is declared?
你在哪里包括<stdlib.h>- 因为那malloc()是声明的地方?
Is this a compilation problem (malloc()undeclared) or a linking problem (malloc()undefined)?
这是编译问题(malloc()未声明)还是链接问题(malloc()未定义)?
What exactly is the error message?
错误信息究竟是什么?
Now the code is readable:
现在代码是可读的:
<cstdlib>is a C++ header (so is<cstdio>).- You need to include
<stdlib.h>in C. - You need to include
<stdlib.h>where themalloc()function is used — inlinkedlist.c.
<cstdlib>是 C++ 头文件(也是<cstdio>)。- 您需要包含
<stdlib.h>在 C 中。 - 您需要包含使用
<stdlib.h>该malloc()函数的位置 - 在linkedlist.c.
You also havehad the header guards in the wrong place in linkedlist.h, but the code in the question has been updated since. Originally, the sequence was:
你也有过在错误的地方看守头linkedlist.h,但是由于是在问题的代码已被更新。最初的顺序是:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#endif
struct linked_list{
…
The canonical structure for a header file is:
头文件的规范结构是:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...the other material in the header...
...definitions and declarations...
#endif /* HEADER_H_INCLUDED */
The #endifis the last non-comment, non-blank line in the file — not the third.
这#endif是文件中的最后一个非注释、非空行——而不是第三个。
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.
即使是双向链表,您的数据结构也异常复杂。你确定你会经常需要长度以保证在列表中的每个节点中维护指向头部的指针吗?如果您经常使用它,我会感到惊讶。我假设您在每个节点中都有指向头的指针,这样当您从列表中删除任意节点时,您可以减少列表的长度。您可能最好将指向列表的指针和指向要删除的节点的指针传递给您所拥有的指针。
I can see no justification for the length = 5in the new_list()function.
我length = 5在new_list()函数中看不到任何理由。
Also, C and C++ differ radically on the meaning of:
此外,C 和 C++ 在以下含义上完全不同:
struct linked_list * new_list();
In C++, that means "new_list()is a function that takes no arguments and returns a struct linked_listpointer".
在 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_listpointer". The function is declared, but there is no prototype for the function.
在 C 中,这意味着“new_list()是一个具有完全不确定的参数列表并返回一个struct linked_list指针的函数”。该函数已声明,但没有该函数的原型。
In C, you should write:
在 C 中,你应该写:
struct linked_list * new_list(void);
Personally, I prefer to see externin 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.
就个人而言,我更喜欢extern在头文件中看到函数声明的前面;它实际上不是函数所必需的,但由于它 ( extern) 是(或应该是)在头文件中声明的变量所必需的,因此我更喜欢头文件中声明的函数的对称性。
回答by Ducain
From your question, it appears you are on a Windows machine. You may have to do:
从您的问题来看,您似乎使用的是 Windows 计算机。您可能必须执行以下操作:
#include <windows.h>
in order to have malloc() available.
为了让 malloc() 可用。
回答by Matthew Flaschen
cstdliband cstdioare not standard C headers. Perhaps you meant stdlib.hand stdio.h.
cstdlib并且cstdio不是标准的 C 头文件。也许你的意思是stdlib.h和stdio.h。
回答by Ali Sherafat
you must use malloc()like below:
你必须malloc()像下面这样使用 :
for (int i = 2; i < 10; i++){
item *temp = (item*)malloc(sizeof(item));
temp->data = i;
pre->next = temp;
pre = temp;
}

