Malloc undefined

I am currently working on rewriting the linked list module and I am getting some weird errors.

In two IDEs (Netbeans and Visual Studio Express), I get a warning that malloc is undefined and that the function found in my linkedlist.c file is also undefined.

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); } 

linkedlist.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 not sure if this is a syntax problem or missing files on my computer.

+4
source share
4 answers

Where do you include <stdlib.h> - because malloc() is declared here?

Is it a compilation problem ( malloc() undeclared) or a binding problem ( malloc() undefined)?

What is an error message?


Now we read the code:

  • <cstdlib> is the C ++ header (e.g. <cstdio> ).
  • You need to include <stdlib.h> in C.
  • You need to include <stdlib.h> where the malloc() function is used - in linkedlist.c .

You also have header protectors in the wrong place in linkedlist.h . Canonical structure for the header file:

 #ifndef HEADER_H_INCLUDED #define HEADER_H_INCLUDED ...the other material in the header... ...definitions and declarations... #endif /* HEADER_H_INCLUDED */ 

#endif is the last non-comment, not an empty line in the file, not the third.


Your data structures are unusually complex even for a doubly linked list. Are you sure you will need the length so often that this ensures that a pointer to the head is stored in each node in the list? I would be surprised if you use it often. I assume that you have a pointer to the head in each node, so that when you remove an arbitrary node from the list, you can reduce the length of the list. You will probably be better off passing a pointer to a list, and the pointer to a node will be deleted than yours.

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


In addition, C and C ++ are radically different in meaning:

 struct linked_list * new_list(); 

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

In C, this means that " new_list() is a function with a completely undefined argument list that returns a struct linked_list pointer." The function is declared, but the function prototype is missing.

In C you should write:

 struct linked_list * new_list(void); 

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

+12
source

From your question, it looks like you are on a Windows machine. You may need to do:

 #include <windows.h> 

to have malloc () available.

+2
source

cstdlib and cstdio are not standard C headers. Perhaps you meant stdlib.h and stdio.h .

+1
source

you should use malloc() as below:

 for (int i = 2; i < 10; i++){ item *temp = (item*)malloc(sizeof(item)); temp->data = i; pre->next = temp; pre = temp; } 
0
source

Source: https://habr.com/ru/post/1333323/


All Articles