0

Recently I found that when I'm inserting a node in a linked list(not first of it)

void Insert(Node*head, int index, int data)
{
    Node* temp = find(head, index);//this will lead to needed position and check for Null returning
    Node* t = (Node*)malloc(sizeof(Node*));
    t->data = data;
    t->next = temp->next;
    temp->next = t;
}

I can malloc like this Node* t = (Node*)malloc(sizeof(Node*)) not like this Node* t = (Node*)malloc(sizeof(Node)) which I had saw almost in every resource code I had read.

when I checked, I was allocating 4 bytes less always but my code still was running well.

which seem confusing to me. How is this possible? or is it the right form and I had it wrong?

I have recently learnt dynamic memory allocation, so I'm sorry if it's a stupid question.

hanie
  • 1,863
  • 3
  • 9
  • 19
  • 2
    Note that this type of error often disappears if you use the style `t = malloc(sizeof *t)` Without even seeing the declaration of t, you can tell that this allocates enough memory for one object of type t. – William Pursell Mar 16 '20 at 16:24
  • 2
    by not allocating enough memory, you'll invoke [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) when you access fields in the `struct` that overrun the amount of memory you allocated. Undefined behavior doesn't mean an instant crash, it means you don't know what will happen. In your case, it appears to keep working. The results may be different on my machine, or if you use different compile flags, etc. – yano Mar 16 '20 at 16:27
  • 4
    `malloc` does not necessarily allocate or map memory from the operating system, except when needed. Primarily, it **reserves** memory for your use. It is a management system, just an allocation tracker. The memory may be there all along, and, if you use memory reserved from you, it is all yours. If you use memory that is not reserved for you, you may bump into somebody (something) else trying to use it. Or you might not. – Eric Postpischil Mar 16 '20 at 16:33
  • @WilliamPursell I use `cpp`compiler,so I can't use `malloc` without casting but even after casting I receive this error `'=': cannot convert from 'void *' to 'Node *` and `a value of type "void *" cannot be assigned to an entity of type "Node *"` is this also because of `cpp` compiler? – hanie Mar 16 '20 at 18:51
  • Why use a compiler for a language other than the language you are using? If your question is about C++, you should tag it as such. – William Pursell Mar 16 '20 at 18:59
  • @EricPostpischil maybe I said my question in a bad way,I kind of know about undefined behavior. My problem is another thing: since I'm declaring a pointer `Node* t` should I allocate for it size of pointer or should I allocate sizeof `Node` since it is also a Node ? – hanie Mar 16 '20 at 19:00
  • 1
    Always allocate memory for the thing you are creating. If `t` is declared `Node *t`, it is a pointer to a `Node`. You already have a pointer. You want something for `t` to point to, and that is a `Node`, so you want a `Node`. So allocate space for a `Node`. Also, it is preferable not to put a type name in a `sizeof`. To allocate space for a thing that `t` points to, use `t = malloc(sizeof *t);`. Or, to do it while defining `t`, use `Node *t = malloc(sizeof *t);`. – Eric Postpischil Mar 16 '20 at 19:02
  • @WilliamPursell no, this is about c but I'm student and I'm learning `c` and `c++`,I was told to use `(VS) c++` to create projects of both languages. – hanie Mar 16 '20 at 19:24
  • @EricPostpischil sorry to ask too many questions ,but can you explain why is this `t = malloc(sizeof *t)` better? – hanie Mar 16 '20 at 19:26
  • @hanie: If you write `t = malloc(size(Node));`, that can be wrong because `t` is not a pointer to `Node`, perhaps because somebody changed the code since it was written and changed `t` to be something different. If you write `t = malloc(sizeof *t);`, it always allocates enough space for whatever `t` points to, even if somebody changes the type of `t`. – Eric Postpischil Mar 16 '20 at 20:02

0 Answers0