Allocating memory with Malloc

Hey everyone so my professor wants us to use Malloc in order to allocate memory for a linked list for our project. When I did this I got an error of: C2027 use of undefined type 'Node' and I can't figure out what is wrong. Yes I would love to use 'new' but our professor strictly said we cannot use it. My code isn't anything impressive because I just started out but this is what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
  using namespace std;

  int main()
  {
	struct Node * node = (struct Node*)malloc(sizeof(struct Node));
        free(node);
        return 0;
  }

  struct Node 
  {
	char* _curr;
	int _line;
	Node *next;
  }


I have tried a combination of:
struct Node * node = (struct Node*)malloc(sizeof(struct Node));
struct Node * node = (Node*)malloc(sizeof(struct Node));
Node * node = (struct Node*)malloc(sizeof(struct Node));
Node * node = (Node*)malloc(sizeof(struct Node));
Node * node = (struct Node*)malloc(sizeof(Node));

The only one that actually works is this one:
struct Node * node = (struct Node*)malloc(sizeof(struct Node*));

but when I added new nodes and quit my program I got a HEAP CORRUPTION DETECTED while trying to use the free(node);

so yeah I am pretty stumped any help would be appreciated and thanks in advance
Last edited on
malloc simply allocate a memory chunk for you , it does not mean that you have create your Node (constructor called ? not on heap).
Whoops I forgot I took out that constructor lol
1) You do not need to add struct everywhere in C++. And you forgot a semicolon after struct.
2) type Node does not exist in main as far as compiler concerned. You need to move type definition before its use.
3) POD types constructors are no-op, so it is irrelevant if it was or not was called. You can safely use those types with malloc (and even if you use them with new, do not expect members to be zero-initialized)
It is always safe to free on pointers returned by malloc
http://en.cppreference.com/w/cpp/memory/c

There are probably another reason for crash. Post code which gives you problems.
Line 6 remove struct
Line 16 add semicolon for struct/class declaration
Line 3 add this -> struct Node; //forward declaration

then try it
Line 3 add this -> struct Node; //forward declaration
How about trying yourself first before suggesting? sizeof requires complete types. It will not work with forward declared types.

error: invalid application of 'sizeof' to an incomplete type 'struct Node'
http://coliru.stacked-crooked.com/a/1e409d3830ac1f83
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
  using namespace std;

    struct Node
  {
	char* _curr;
	int _line;
	Node *next;
  };
  int main()
  {
	Node * node = (struct Node*)malloc(sizeof(struct Node));
        free(node);
        return 0;
  }


You should read on Google about how a compiler know the size of a class or struct in order to parse it to malloc.
You should read on Google about how a compiler know the size of a class or struct
Code you posted now does not contain suggestions you posted in your previous post, which are simply leads to invalid code. Code I posted in my post contained your suggestions as is and outlined the problem.

You should read on Google about how a compiler know the size of a class or struct
Agressively denying that problem even existed when somebody points it out and ad hominem attacks are not a tolerable behavior.
it is not an attack it is a suggestion , that could prevent for further errors . There is no mean in my posts.
If it is was not directed to me, I apologise. It takes another meaning if it is directed to OP.

However if you are not answering to last post in duscussion, you should clearly state recipient of your message to avoid misunderstanding. Posting direct links instead of referring to search engine is better, as you are protecting people from accidentally stumbling upon wrong and incorrect info.
@chalupabatman here is another link
http://stackoverflow.com/questions/10078283/how-sizeofarray-works-at-runtime
it might extend what @MiiNiPaa suggest earlier.
Topic archived. No new replies allowed.